CS1116/CS5018 Lab 4

Step 1: Download files

In your public_html/cgi-bin directory, create a new directory with the correct permissions called lab4 (no spaces, all in lowercase!!!!!!). Place your work for this lab sheet into this new directory.

Download the following files (right-click and choose 'Save link as') and save them in your lab4 directory:

Do not modify these three files in any way, except as specified below.

Change the name of display_leaderboard.txt to display_leaderboard.py, and similarly for display_voting_form.txt. Make sure their permissions are 700.

Use pycharm to edit both files: find the parameters to the db.connect function and replace them with your own database credentials.

Q: Why did I give these two files the 'wrong' name in the first place?

Step 2: Create a database

As in the previous lab, use DBeaver to login to the MySQL DBMS on cs1.ucc.ie.

When you are connected to the DBMS, still using DBeaver, right-click in the SQL editor and choose 'Load SQL Script' from the menu. Use the file chooser to open the election.sql file that you copied above.

If all of that worked OK, then you now have a new database table called candidates, which has the following columns: candidate_name and total_votes.

If you want to check, then type:

select * from candidates;

You should see a table of election candidates.

You can now close down DBeaver.

Step 3: Write a program

In a file called insert_candidate.py in your lab4 directory, write a Python server-side program, as follows.

The program implements a self-processing page:

At any time, you can run my display_leaderboard.py program that you downloaded in Step 1 to see a list of the candidates, so you can check whether new candidates have been inserted.

Step 4: Write another program

In a file called update_vote.py in your lab4 directory, write a Python server-side program, as follows.

The user will run my display_voting_form.py program that you downloaded in Step 1 to choose a candidate. On submit, the user's choice will be sent to your update_vote.py program. Your program will increment the votes for the chosen candidate.

Obviously, your program should guard againt a user who tries to vote for a non-existent candidate.

Furthermore, your program should use cookies to prevent a user from voting more than once. In this case, it does not matter what data is in the cookie, since you are simply using it to detect whether this is a repeat visit or not. Just choose any sensible name and value.

Q: How can a technically-literate user vote more than once?

Submission

This lab sheet does not count towards the continuous assessment of this module. However, we intend collecting this week's work and also lab2/lengths.py and giving you feedback on them.

Deadline: 10am, Friday 28th February 2020.

Use Canvas to submit lengths.py, insert_candidate.py and update_vote.py.