This shows you the differences between two versions of the page.
ii:labs:s2:01:tasks:03 [2022/04/01 13:03] florin.stancu created |
ii:labs:s2:01:tasks:03 [2024/03/15 13:30] (current) florin.stancu [03. [20p] Making the list] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ==== 03. Authentication ==== | + | ==== 03. [25p] Making the list ==== |
- | Now it's the time to add authentication to our website. | + | Solve these subtasks all in the same file (use ''print()'' statements to display the results): |
- | We will use the built-in Flask client-side encrypted sessions feature, for which will need to take the following steps to enable: | + | Use the following list: <code python> |
+ | sample = ["a", 90, 8, "X55", "1z", 102, "asdf", 65, 10, "word", "567", 567] | ||
+ | </code> | ||
+ | |||
+ | Well: | ||
- | * Import the ''session'' field from ''Flask''; | + | * Sort the list (does it work? if not, why?); |
- | * Configure a secret encryption key for the session cookies:<code python> | + | * Reverse the list (this again? note: save the original one!); |
- | # insert after initializing `app` | + | * Filter out all non-numbers from the list (i.e., only ''int'' type values should remain)... can you make it using **a single line of code**? now it's the time to introduce [[https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions|list comprehensions]]; |
- | app.config["SECRET_KEY"] = "<type some secret here>" | + | <note> |
+ | Don't know how to check the type of a variable? | ||
+ | [[https://www.google.com/search?q=python+check+type+is+int|LMGTFY]]! | ||
+ | </note> | ||
+ | * Print the original list (remember the first 2 subtasks? [[https://stackoverflow.com/questions/2612802/how-do-i-clone-a-list-so-that-it-doesnt-change-unexpectedly-after-assignment|read this answer!]]); | ||
+ | * Define a function that builds a matrix filled with numbers from 1 to n like the following example:<code python> | ||
+ | m = [[1, 2, 3, 4], | ||
+ | [4, 1, 2, 3], | ||
+ | [3, 4, 1, 2], | ||
+ | [2, 3, 4, 1]] | ||
</code> | </code> | ||
- | * Write the Flask functions for ''login.html'' and ''logout.html'' with the appropriate checks / actions. | ||
- | * Use the ''authenticated'' variable inside the Jinja template and conditionally display the user's status. | ||