CS作业代写 CMT302 EC &Innovation

Lab Objectives
Cardiff University
School of Computer Science & Informatics Dr
CMT302 EC &Innovation

Copyright By PowCoder代写 加微信 powcoder

SPRING – SESSION 3 LAB
Form Validation. Cart. Deployment.
Objectives of this lab are to complete developing essential functionality for our Online Bookstore, by:
• adding validation to user authentication functionality; • implementing Shopping Cart functionality;
• deploying our website on OpenShift
NB: As for all labs, it is advisable to do all the work with your virtual environment activated.
General Comments
As before, this exercise is not assessed. If you do not manage to finish all the tasks in the lab, please attempt to finish them in your own time.
Use the suggested resources and links, provided throughout this document and on the last page, to help you understand the code. A snapshots of the state of the project is available on Learning Central (FLASK_labs_snapshot_4.zip). You can download this to help you if you are stuck, to check your progress (and, perhaps, at times to save your typing *from scratch*). However, please make sure you understand each line of the code!
If you get stuck – raise your hand and ask for advice from the lecturer and TA. It is also okay to discuss the solutions with your peers (these labs are not assessed!), however, make sure you understand everything by yourself.
IMPORTANT!! The labs will give you some basic understanding of how to develop a website in Flask. To get more comprehensive understanding of how this ’stuff’ works, it is strongly advised that you read the recommended book, suggested documentation and ’quickstarts’, as well as complete few tutorials. However, the suggested resources are just suggestions and the list is non-exhaustive! There are lots of other tutorials and resources on the Web.
Abbreviations used in this document
• dir – directory (folder) • db – database
• NB – Nota bene

FORM VALIDATION 1
In our previous lab we practice developing Flask forms. However, we assumed that the users would only provide input that is valid. If they don’t, the system will behave unexpectedly and possibly crash. For example, if a user tries to register with a username that already exist, we would get a SQLAlchemy’s ’database integrity error’. To avoid issues like this, we need to validate the users’ input and provide them with hints and help, e.g. we want to reinforce the rule that the username and email is unique and inform the user if this username and email are already taken.
In this section, we will work on the following files: forms.py to define functions for user input validation, appropriate templates (e.g. register.html to tell the server how to render the content), and routes.py to bind specific URLs to our functions.
1. Let’s check username already exists. In forms.py, we would specify this rule as:
def validate_username(self, username):
user = User.query.filter_by(username=username.data).first() if user:
raise ValidationError(‘Username already exist. \
Please choose a different one.’)
NB: The above code requires an import of ValidationError from wtforms.validators. 2. Still in forms.py, create a similar validation for the users’ emails, i.e.
def validate_email(self, email):
3. We could also specify certain rules for passwords. Suppose we want to reinforce the following rule: a password must be between 6 and 8 characters long (any characters, e.g. abcde1 will be valid, but not abc1).2
We can use a regular expression (regex) for this. Update password in forms.py, as follows:
password = PasswordField(‘Password’, validators=[DataRequired(),
Regexp(‘^.{6,8}$’, message=’Your password \ should be between 6 and 8 characters long.’)])
NB: Don’t forget to import Regexp from wtforms.validators.
NB: Consult the lecture slides for a range of regular expressions. For more in- formation and practice, visit: https://www.w3schools.com/python/python_regex.asp
4. Next, implement appropriate validation for the log in functionality.
1 NB: In this lab, we will be using Flask to validate the forms. Alternatively, you might want to look into using
JavaScript to achieve this – see the examples given in the lecture.
2 This, of course, is a simple requirement for the password. If we want to make the rule more complicate, we would need to use a more complicated regex. For instance if we want to make sure that a user’s password must be between 6 and 8 characters long AND contain at least one numeric digit, the regex for this would be: ^(?=.*\d).{6,8}$

5. After we specified the logic, we need to change our code to check the form is valid when it is submitted, so in routes.py instead of using
if request.method == ‘POST’: we need to to use:
if form.validate_on_submit():
in the appropriate @app.route(..) decorators.
Error Messages
Any good system should provide its user with feedback. We have already encountered that we can specify a message to the user during form validation (Task 3). We can also use a messaging system provided by Flask, called ’flashing system’.
NB: This functionality requires an import of flash from flask in routes.py.
6. The following is an example of a flash message, which we can add to routes.py: …
flash(‘Invalid username or password.’) …
7. To enable the ’flashing system’, we need to add code to the templates to instruct the server to display the messages:
(a) site-wide, by adding the following to layout.html:
{% with messages = get_flashed_messages() %} {% if messages %}

    {% for message in messages %}

  • {{ message }}
  • {% endfor %}
    {% endif %} {% endwith %}

(b) and on a specific page, e.g. in register.html:
{% for error in form.username.errors %}
[{{ error }}] {% endfor %}
NB: More information on Message Flashing in Flask is found at: https://flask. palletsprojects.com/en/1.1.x/patterns/flashing/ .

SHOPPING CART
Lastly, let’s provide a ’shopping cart’ functionality. This functionality can be implemented in many ways. In particular, one of the decisions a programmer would need to make is where to store the cart: e.g. in a session/ cookie or database? (An interesting overview of this issue is here: https://www.wiliam.com.au/wiliam-blog/ where- should- you- store- your- cart ).
In this lab, we will implement a basic shopping cart, which will be stored as a dictionary in the session3.
The basic functionality includes:
• adding a product to the cart
• displaying the cart contents
• removing a product from the cart
NB: As usual, the complete code is provided in the snapshot of the project (see the zip file(s) on Learning Central). If you are stuck, please consult these files.
Adding a Product
8. Adding a product to the cart is implemented by coding add_to_cart() function – in routes.py:
@app.route(“/add_to_cart/“) def add_to_cart(book_id):
if “cart” not in session: session[“cart”] = []
session[“cart”].append(book_id)
flash(“The book is added to your shopping cart!”) return redirect(“/cart”)
NB: The above code needs an import of session from flask. 9. The ’add a product’ option is added to the book.html as href4.
Add to Cart
10. When the user clicks on the link, a product is added to the cart and the user is redirected to cart.html (implemented in Task 12).
3 More information on Flask sessions: https://flask.palletsprojects.com/en/1.1.x/quickstart/ #sessions
4 This is, perhaps, the most basic way to do it. Alternative and, perhaps, a better way to do it would be to implement a ’submit’ button with a POST or GET request.

Displaying Cart Contents
11. To display the cart contents, we need to create cart_display() function in routes.py. Here, we reiterate through the dictionary in the current session, retrieve the book’s details and calculate the total quantity and price:
@app.route(“/cart”, methods=[‘GET’, ‘POST’]) def cart_display():
if “cart” not in session:
flash(‘There is nothing in your cart.’)
return render_template(“cart.html”, display_cart = {}, total = 0)
items = session[“cart”] cart = {}
total_price = 0 total_quantity = 0 for item in items:
book = Book.query.get_or_404(item) total_price += book.price
if book.id in cart:
cart[book.id][“quantity”] += 1 else:
cart[book.id] = {“quantity”:1, “title”: book.title, \ “price”:book.price}
total_quantity = sum(item[‘quantity’] for item in cart.values())
return render_template(“cart.html”, title=’Your Shopping Cart’, \ display_cart = cart, total = total_price, \
total_quantity = total_quantity) return render_template(‘cart.html’)
12. We then create cart.html in templates dir. The template again reiterates through the dictionary in the current session and displays the book(s) added to the cart by the user:
{% for key, value in display_cart.items() %}

{{ value[“title”] }} {{ value[“quantity”] }} {{ value[“price”] }} {{ (value[“quantity”] * value[“price”]) }}

Deleting a Product
13. To delete a product we need to specify delete_book(book_id) function in routes.py:
@app.route(“/delete_book/“, methods=[‘POST’]) def delete_book(book_id):
if “cart” not in session: session[“cart”] = []
session[“cart”].remove(book_id)
flash(“The book has been removed from your shopping cart!”) session.modified = True
return redirect(“/cart”)
and provide a button for the user to choose to delete a product – in cart.html: …


NB: In the above code, we are accessing a particular book by its int, hence we need this conversion: ’book_id|int’ (see Jinja Template documentation for more information on built in filters, expressions, etc.: https://jinja.palletsprojects.com/ en/2.11.x/templates/ ).
WEBSITE DEPLOYMENT on OpenShift
14. See the separate handout. CONCLUDING REMARKS
This completes the series of labs on Flask, and you should now have a basic online store. You could use the lab work for your coursework, but you will need to carry on and implement additional functionality, which you have identify as essential and appropriate for the chosen business, e.g. checkout, payment, etc. You would also need to continue working on styling your website to create a ’look and feel’ you want your website to have.
Due to the time and scope constraints, the work we produced in these labs has several limitations, e.g. with regard to enhanced security and functionality. This constitutes ’future work’, e.g. in other modules or your summer project.

Flask Website: Flask Tutorial:
Flask-WTF home page: Flask-WTF Quickstart:
WTForms Form Validation Python RegEx
Book on Flask
Plenty of video tutorials on YouTube (as usual, in no par- ticular order!), e.g.:
Useful resources
https://flask.palletsprojects.com/en/1.1.x/
https://flask.palletsprojects.com/en/1.1.x/
https://flask- wtf.readthedocs.io/en/stable/ https://flask- wtf.readthedocs.io/en/stable/ quickstart.html https://flask.palletsprojects.com/en/1.1.x/ patterns/wtforms/
https://www.w3schools.com/python/python_regex.asp
M. Grinberg’s (2014). “Flask web development”, O’ .
[1], [2] (these links are to the first lessons in a series; other episodes cover the flask forms).
A number of cheat sheets could be found on the web.

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com