CS1116/CS5018 Lab 4

Step 1: Download files

In your public_html/cgi-bin directory, you should find a directory called lab4. 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 you did in CS1106/CS5021, 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. It does not matter what data is in 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?


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 2nd March 2018.

You don't have to do anything. We have a program that will copy your work from your lab2 and lab4 directories.