Difference between revisions of "Module 5"

From CSE330 Wiki
Jump to: navigation, search
(Grading)
(Requirements)
 
(78 intermediate revisions by 11 users not shown)
Line 1: Line 1:
In Module 6, you will learn about python, a programming language, and Django, a web framework.
+
__NOTOC__
 +
In Module 5, you will learn JavaScript, the dominant client-side web language.  JavaScript is the third and final programming language you will learn in CSE 330.
  
This article contains your assignments for Module 6.
+
This article contains your assignments for Module 5.
 +
 
 +
'''Warning:'''
 +
 
 +
''This module takes students a while. It is strongly recommended that you start early and ask questions as they come up.''
 +
 
 +
 
 +
== Reading ==
 +
 
 +
The following articles on the online class wiki textbook contain information that will help you complete the assignments.
 +
 
 +
* [[JavaScript]]
 +
* [[AJAX and JSON]]
 +
* [[jQuery]]
 +
* [[Web Application Security, Part 3]]
 +
* [[FAQ - Mod 5]]
  
 
== Individual Assignments ==
 
== Individual Assignments ==
  
 +
'''''IMPORTANT:'' Both of the individual assignments must be completed ''without'' using a JavaScript library like jQuery.'''  However, you may use jQuery for the group portion.
 +
 +
=== JavaScript Calculator ===
 +
 +
In Module 2, you made a calculator using PHP.  Now you will be making one using JavaScript.
 +
 +
* The web page should have two input fields and a radio button group for the operation, with the 4 basic math operations represented (add, subtract, multiply, divide).
 +
* JavaScript should monitor all three fields and display the current result whenever the user changes any value in any field, without refreshing the page.
 +
*: '''Tip 1:''' Consider using either the ''change'' or the ''keyup'' event on the text fields, and either the ''change'' or the ''click'' event on the radio buttons.
 +
*: '''Tip 2:''' Re-read [[JavaScript|the JavaScript guide]] if you need help figuring our how to bind callback functions to events, or how to determine which radio button is currently selected.  (There are examples for both of these!)
 +
* The calculator should be completely self-contained; i.e., you should not be making any requests to any other server-side or client-side scripts or web pages after the initial page load.
 +
 +
'''Tip:''' You can embed JavaScript code into an HTML document like this:
  
=== Python Tutorial ===
+
<source lang="html4strict">
 +
<script>
 +
// your code here
 +
</script>
 +
</source>
  
====Install Python Tools====
+
'''Tip:''' If your code isn't working the way you expect, use a JavaScript error console.  In Chrome, for example, press Ctrl-Shift-I (or Cmd-Option-I) to open the WebKit inspector.
*<code>sudo yum install python-setuptools</code><br>
 
*<code>sudo yum install python-devel</code>
 
====Python Assignment====
 
*You will write a python script that reads a set of student grades in from a file and does some basic parsing and processing. [http://docs.python.org/tutorial/inputoutput.html#reading-and-writing-files here] is the section of the python tutorial on reading and writing files.
 
*An example grades file is [http://research.engineering.wustl.edu/~todd/cse330/grades.txt here]. For the sake of simplicity you can assume that the file name is always going to be grades.txt.
 
*The first line of the file is of the form: NUM_LABS,NUM_EXAMS,LAB_WEIGHT
 
*All of the other lines in the file are of the form: FIRSTNAME LASTNAME|GRADE|TYPE
 
*The FIRSTNAME and LASTNAME fields are the student's full name (always only a first and last name), the GRADE is one grade for the student, and the TYPE describes what kind of assignment the grade was for, in this case either a 'lab' or an 'exam'.
 
*Your script should read in a grades file with the above format and perform the following:
 
*# Compute the final grade for every student, given that there were a total of NUM_LABS labs, NUM_EXAMS exams, and that the labs account for a total of LAB_WEIGHT percent of the grade.
 
*# Compute the final letter grade based on the final numeric grade (A = 90 or above, B = 80 - 90, C = 70 - 80, D = 60 - 70, F = below 60). 
 
*# Ignore any blank lines.
 
*# Sort the students by last name, and print out final grades in that sorted order.
 
*# No names should be hard coded into the script (you will run your script on another file with different student names when we grade your assignment).
 
*# Finally, the script should take one optional command line argument, which is a string to match against student names.  Only names that match the string in full or in part, should be printed out with their final grades.
 
*#*For example, with the argument 'John' grades for both John Smith and John Locke should be printed (if those are the only two Johns in the grades.txt file)
 
  
=== Django Tutorial===
+
'''Important:''' It is best practice to fully separate the content, appearance, and behavior.  In previous modules, you may have lost points for using HTML tags that change the appearance of things <nowiki>(like <center> and <font>).</nowiki> In this module, make sure that you define your event listeners in JavaScript as in <code>document.getElementById("xyz").addEventListener(...)</code> and '''''not''''' in the HTML like <code><button onclick="badExample();">Bad Example</button></code>.
====Install Djgano====
 
<p> This procedure assumes you are using and Amazon AMI Linux on an ec2 instance. An in-depth install guide can be found on the [https://docs.djangoproject.com/en/dev/topics/install/# Django website], but a quick version is below. </p>
 
<ul>
 
<li>Install Distribute (prerequisite for pip):</li>
 
<code>wget http://python-distribute.org/distribute_setup.py</code><br><code>sudo python distribute_setup.py </code>
 
<li>Install pip:</li>
 
<code>wget https://raw.github.com/pypa/pip/master/contrib/get-pip.py </code><br><code>sudo python get-pip.py</code>
 
<li><p>Install Django using pip:</p></li>
 
<code>sudo pip install Django</code>
 
</ul>
 
<p> That's it. </p>
 
  
====Django Assignment====
+
=== Weather Widget ===
<p>Complete the [https://docs.djangoproject.com/en/1.4/intro/tutorial01/ tutorial] on the django website. You will create a Polls app. There are four parts to this tutorial.</p>
 
<p>It is important you do all the steps in the tutorial to get comfortable defining models, views, templates, and using the interactive shell and the admin app. Understanding the concepts will make the project go much quicker.</p>
 
<ul>
 
<li><h5>[https://docs.djangoproject.com/en/1.4/intro/tutorial01/ Part 1]</h5>
 
<ul>
 
<li>Create a project</li>
 
<li>Start the development server</li>
 
<li>Setup an SQLite database</li>
 
<li>Create the Polls app and define the models</li>
 
<li>Interact with the database using the interactive python shell</li>
 
</ul>
 
</li>
 
<li>
 
<h5>[https://docs.djangoproject.com/en/1.4/intro/tutorial02/ Part 2]</h5>
 
<ul>
 
<li>Activate the admin site</li>
 
<li>Add the poll app to the admin site</li>
 
<li>Modify the database using the admin site</li>
 
</ul>
 
</li>
 
<li>
 
<h5>[https://docs.djangoproject.com/en/1.4/intro/tutorial03/ Part 3]</h5>
 
<ul>
 
<li>Configure URL handling</li>
 
<li>Create some views</li>
 
</ul>
 
</li>
 
<li>
 
<h5>[https://docs.djangoproject.com/en/1.4/intro/tutorial04/ Part 4]</h5>
 
<ul>
 
<li>Write a form that modifies the database</li>
 
<li>Refactor to use generic views</li>
 
</ul>
 
  
<h5>Switch to Apache Webserver</h5>
+
In this section, you will make a web page that displays the weather forecast using AJAX requests to a weather server.
<ul>
+
 
<li>run <code> sudo yum install mod_wsgi</code> to install the apache and the module necessary for serving your django app.</li>
+
# Make an empty HTML document; name it '''weather.html'''
<li> Add these lines to /etc/httpd/conf/httpd.conf
+
#: Refer to the [[HTML and CSS]] guide for the skeleton of an HTML document
<source lang="apache">
+
# Define a function in JavaScript; call it '''fetchWeather()'''.  You may write your JavaScript in an embedded script in your HTML document.
Alias /static /var/www/mysite/static
+
# Inside your '''fetchWeather()''' function, make an AJAX request to the weather server.
<Directory /var/www/mysite/static>
+
#: We have a server that outputs the current weather in JSON format.  The format is documented in the [[#JSON Structure]] section below.
  Order deny,allow
+
#: '''URL:''' https://classes.engineering.wustl.edu/cse330/content/weather_json.php
  Allow from all
+
#* '''IMPORTANT:''' Under normal circumstances, AJAX requests ''cannot'' be performed cross-domain for security reasons.  We have set the <code>Access-Control-Allow-Origin</code> header on our server to allow requests from your EC2 instances, or from localhost.  '''You will therefore need to upload your code to your EC2, or access it via localhost, in order for the AJAX requests to work.'''  This means that you cannot, for example, complete this part of the individual portion in JSFiddle.
</Directory>
+
# In your callback, process the JSON and use JavaScript to manipulate the HTML DOM to display the following information on your page:
WSGIScriptAlias / /var/www/mysite/apache/django.wsgi
+
#* Location
 +
#** City, in a <nowiki><strong></nowiki> tag
 +
#** State, not in any tag
 +
#* Humidity
 +
#* Current Temperature
 +
#* Image for Tomorrow's Forecast (see [[#Weather Condition Images]] below for more information)
 +
#* Image for the Day After Tomorrow's Forecast
 +
# Finally, bind '''fetchWeather()''' to the DOMContentLoaded event so that your weather widget is automatically initialized when the page is loaded:
 +
#: <source lang="javascript">document.addEventListener("DOMContentLoaded", fetchWeather, false);</source>
 +
# In addition, add a button that runs your ''fetchWeather'' function when clicked.  This should update your widget with the current weather conditions.
 +
 
 +
Use the following HTML:
 +
 
 +
<source lang="html4strict">
 +
<div class="weather" id="weatherWidget">
 +
<div class="weather-loc"></div>
 +
<div class="weather-humidity"></div>
 +
<div class="weather-temp"></div>
 +
<img class="weather-tomorrow" />
 +
<img class="weather-dayaftertomorrow" />
 +
</div>
 
</source>
 
</source>
</li>
 
<li>Move your "mysite" project (the top level mysite directory) to /var/www and make the directory /var/www/mysite/apache. In that directory create the file django.wsgi and inside that file put:
 
<source lang="python">
 
import os
 
import sys
 
sys.path.append('/var/www/mysite')
 
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
 
import django.core.handlers.wsgi
 
application = django.core.handlers.wsgi.WSGIHandler()
 
</source></li>
 
</ul>
 
</li>
 
<li>Be sure to change any references to old paths in the django "mysite" settings.py file (such as the location of the database if using sqlite) and restart the webserver. </li>
 
  
<li>Also make sure the permissions are setup properly on the mysite directory under /var/www for apache to access, along with the database file (if using sqlite3). </li>
+
Include the CSS file from here: https://classes.engineering.wustl.edu/cse330/content/weather.css
  
<li>To fix the issue with the CSS not loading on the admin page you need to create a directory under the static folder (/var/www/mysite/static/admin/css/) and copy the files from /usr/lib/python2.6/site-packages/django/contrib/admin/static/admin/css/ into it.
+
When everything is working, the weather widget should look something like this:
</li>
 
  
 +
[[File:WeatherWidget.png]]
  
</ul>
+
'''Important:''' The widget in this section needs to work in only Firefox and Chrome.  It does '''''not''''' need to work Internet Explorer.
 +
 
 +
==== Tips ====
 +
 
 +
===== JSON Structure =====
 +
 
 +
The JSON from our server looks like this:
 +
 
 +
<source lang="javascript">
 +
{
 +
  "updated": "Thu, 11 Oct 2012 5:54 pm CDT",
 +
  "location": {
 +
      "city": "St. Louis",
 +
      "state": "MO"
 +
  },
 +
  "wind": {
 +
      "chill": "62",
 +
      "direction": "150",
 +
      "speed": "3 mph"
 +
  },
 +
  "atmosphere": {
 +
      "humidity": "50",
 +
      "visibility": "10",
 +
      "pressure": "30.12 in"
 +
  },
 +
  "current": {
 +
      "code": "28",
 +
      "text": "Mostly Cloudy",
 +
      "temp": "62°F",
 +
      "date": "Thu, 11 Oct 2012 5:54 pm CDT"
 +
  },
 +
  "tomorrow": {
 +
      "code": "29",
 +
      "text": "Clouds Early/Clearing Late",
 +
      "low": "45°F",
 +
      "high": "61°F"
 +
  },
 +
  "dayafter": {
 +
      "code": "30",
 +
      "text": "Partly Cloudy",
 +
      "low": "53°F",
 +
      "high": "65°F"
 +
  },
 +
  "credit": "http://us.rd.yahoo.com/dailynews/rss/weather/St._Louis__MO/*http://weather.yahoo.com/forecast/USMO0170_f.html"
 +
}
 +
</source>
 +
 
 +
===== Weather Condition Images =====
 +
 
 +
Each day's forecast has a code.  There are images associated with these codes.
 +
 
 +
One place to get the images is from here: <nowiki>http://us.yimg.com/i/us/nws/weather/gr/</nowiki>'''##'''<nowiki>ds.png</nowiki>
 +
 
 +
Replace the '''##''' with the forecast code.  For example, for code 32, the URL would be: http://us.yimg.com/i/us/nws/weather/gr/32ds.png
  
 
== Group Project ==
 
== Group Project ==
  
You may work in pairs on this project. Your group will create an image tagging and browsing site using Django as described below.
+
I forgot if I already mentioned this, but '''start early on this project!''' It will take longer than you think, I promise!
  
=== Image Tagging/Browsing Site ===
+
You are encouraged to use the advanced version control practices you learned in Module 5 when completing this assignment.
  
A site that lets users upload images, apply multiple tags to them, and browse through images of a certain tag.
+
=== Calendar ===
When looking at an image, it lists all associated tags so you can switch albums.
+
 
 +
Build a simple calendar that allows users to add and remove events dynamically.
 +
 
 +
You will use JavaScript to process user interactions at the web browser, without ever refreshing the browser after the initial web page load.  You may use a JavaScript library of your choice, including Ext JS, so long as that JavaScript library does not defeat the purpose of the assignment. If you have questions about this, ask. In particular, libraries like FullCalendar are unacceptable.
 +
 
 +
Your application should utilize AJAX to run server-side scripts that query your database to save and retrieve information, including user accounts and events.
 +
 
 +
We have also written some functions to help you get started: [[JavaScript Calendar Library]]
 +
 
 +
==== Examples ====
 +
 
 +
* https://calendar.google.com/
 +
* http://www.zoho.com/calendar/
 +
 
 +
==== Server Side Language ====
 +
 
 +
 
 +
Additionally, you may use a database of your choice.  MySQL is probably the logical choice since you have been using it since Module 2, but you are free to explore other databases if you like.
  
 
==== Requirements ====
 
==== Requirements ====
  
 +
* Support a month-by-month view of the calendar.
 +
*: Show one month at a time, with buttons to move forward or backward.
 +
*: There should be no limit to how far forward or backward the user can go.
 +
* Users can register and log in to the website.
 +
*: You may leverage your MySQL project code and database from module 3 to get started.
 +
* Unregistered users should see no events on the calendar.
 +
* Registered users can add events.
 +
*: All events should have a date and time, but do not need to have a duration.
 +
*: You do ''not'' need to support recurring events (where you add an event that repeats, for example, every monday).
 +
* Registered users see only events that they have added.
 +
*: Your AJAX should not ask the server for events from a certain username.  Instead, your AJAX should ask the server for events, and the server should respond with the events for only the currently-logged-in user (from the session).  Can you think of why?
 +
* Registered users can edit and delete their own events, but not the events of others.
 +
*: Again, the server should edit and delete events based on the username present in the session, not based on a username or event ID provided by the user.  (If it deletes only based on an Event ID, an attacker could feed any arbitrary Event ID to your server, even if he/she didn't own that event.)
 +
* All user and event data should be kept in a database.
 +
* At no time should the main page need to be reloaded.
 +
*: User registration, user authentication, event addition, and event deletion should all be handled by JavaScript and AJAX requests to your server.
 +
* Your page needs to work in the versions of Firefox and Chrome installed on the lab computers.
 +
 +
'''Tip:''' Run your database schema by a TA before implementing it.
  
 +
=== Web Security and Validation ===
  
<ul>
+
Your project needs to demonstrate that thought was put into web security and best practice. For more information, see this week's Web Application Security guide: [[Web Application Security, Part 3]]
<li>Make a database that stores images and tags. An image can have multiple tags, and each tag can have multiple images.</li>
 
<li>The home page should allow users to find tags that they want to view. (e.g. lolcat).</li>
 
<li>After clicking a link to a tag, it should take you to a page with the newest image for that tag.</li>
 
<li>Clicking on the current image should take to to the next image according to date added. When at the last image, link to the first.</li>
 
<li>Make it clear which tag you are currently browsing. Beside or below the current image, also display the full list of tags that belong to the current image (e.g. lolcat, meme, internet, funny). You should be able to click on any of the tags to begin looking through that collection of images.</li>
 
<li>Provide a way for users who are logged in to add new images and new tags.</li>
 
<li>Validate user input</li>
 
<li>Provide a way for users to register, login, and change their password.</li>
 
<li>Have a link that sends an email to a user with a link for resetting their password in case they forget it.</li>
 
<li>Style the site with statically-served CSS (i.e. link to static .css files and don't write CSS inside the html templates) </li>
 
</ul>
 
  
==== Notes ====
+
In particular:
<ul>
 
<li>You may store images as external web links. </li>
 
<li>Try to find and figure out as many Django shortcuts as you can. You do not need to write your own url parsing, validation, login/registration/reset forms etc. For these common tasks, there is almost always built-in functionality that will do what you want in very few lines.</li>
 
<li>The admin interface and the interactive python shell will be very helpful when getting started with the database.</li>
 
  
</ul>
+
* '''Your application needs to prevent XSS attacks.'''  Be careful when transmitting data over JSON that will be reflected in an event title!  (Note: JSON data should be sanitized on the client side, not the server side.)
 +
* '''Perform precautionary measures to prevent session hijacking attacks.'''
 +
*: You should specify your session cookie to be HTTP-Only.  However, for the means of this module, you need not test for user agent consistency.
  
== Grading ==
+
You should continue the practices that you have learned in past weeks:
  
<span style="font-size:2em; line-height:2em;">'''Due Date: Wednesday December 5th by 1pm (both individual and group)'''</span>
+
* Pass tokens in forms to prevent CSRF attacks.
 +
*: '''Hint:''' You will need to send your CSRF tokens in your AJAX requests.  Remember that AJAX still submits forms and runs server-side scripts, just like the vanilla forms you've been using in Modules 2 and 3.
 +
* Use prepared queries to prevent SQL Injection attacks.
 +
* If storing passwords in a database, always store them salted and encrypted.
 +
* Your page should validate with no errors or warnings through the W3C validator.
  
{|
+
== Grading ==
!Python Assignment (Individual)
 
!Points
 
|-
 
|Compute Final Grades
 
|0.25
 
|-
 
|Compute Letter Grade
 
|0.25
 
|-
 
|Sort by Last Name
 
|0.25
 
|-
 
|Accept Command Line Argument
 
|0.25
 
|-
 
!colspan="2"|Django Assignment (Individual)
 
|-
 
|Models defined, database set up     
 
|0.25
 
|-
 
|Admin app enabled and working
 
|0.25
 
|-
 
|URLs matched to correct views
 
|0.25
 
|-
 
|Templates for viewing and voting using generic views
 
|0.25
 
|-
 
|Apache serving the site
 
|0.5
 
|-
 
!colspan="2"|Group Portion:
 
|-
 
|Models defined and database set up
 
|1
 
|-
 
|View of single image with current tag and list of all tags
 
|1
 
|-
 
|Navigation between images and tags
 
|0.5
 
|-
 
|Main page displays all tags sorted by number of images
 
|0.5
 
|-
 
|User registration, login/logout and change password
 
|0.5
 
|-
 
|Forgot password sends email with working reset link
 
|0.5
 
|-
 
|Validation of URLs submitted by user
 
|0.5
 
|-
 
|Add new images and tags, only if logged in
 
|0.5
 
|-
 
|Static CSS applied to some templates
 
|0.5
 
|-
 
|Creative Portion
 
|2
 
|-
 
|}
 
  
  
Total Points = 10
+
We will be grading the following aspects of your work.  There are 100 points in total.
  
Turn in after 1pm on the 5th = 1 point off
+
'''Assignments (including code) must be committed to Github by the end of class on the due date (commit early and often). Failing to commit by the end of class on the due date will result in a 0. '''
  
Turn in on the 6th = 2 points off
+
If you do not include a link to your individual and group portion running on your instance in the respective README.md, you will receive a 0 for the missing portion
  
Turn in on the 7th = 3 points off
+
_________
  
All projects should be completed by the last day of class, Friday the 7thAny modules that have not been demo'd at that time will be given a grade of zero. You are responsible for making sure you can attend TA hours to demo if you are late (or for contacting a TA to try and arrange a session)You cannot demo by e-mail.
+
# '''JavaScript Calculator (10 Points):'''
 +
#* Calculator successfully adds, subtracts, multiplies, and divides numbers (3 points)
 +
#* The result is automatically computed when a value in either field is changed (3 points)
 +
#* Calculator is written entirely in pure, conventional JavaScript with no external libraries (4 points)*
 +
#*: ''Note: be sure to fully separate content, appearance, and behaviorThis means that you should '''not''' use the event attributes on the HTML elements like onclick, onchange, and onkeyup.''
 +
# '''Weather Widget (15 Points):'''
 +
#* Widget performs an AJAX request to fetch the current weather (4 points)
 +
#* The HTML template is filled with the correct information, including an image (4 points)
 +
#* Button to reload the weather widget (2 points)
 +
#* Widget is written entirely in pure, conventional JavaScript with no external libraries (5 points)*
 +
# '''AJAX Calendar (60 Points):'''
 +
#* '''''Calendar View (10 Points):'''''
 +
#** The calendar is displayed as a table grid with days as the columns and weeks as the rows, one month at a time (5 points)
 +
#** The user can view different months as far in the past or future as desired (5 points)
 +
#* '''''User and Event Management (25 Points):'''''
 +
#** Events can be added, modified, and deleted (5 points)
 +
#** Events have a title, date, and time (2 points)
 +
#** Users can log into the site, and they cannot view or manipulate events associated with other users (8 points)
 +
#**: ''Don't fall into the Abuse of Functionality trap!  Check user credentials on the server side as well as on the client side.''
 +
#** All actions are performed over AJAX, without ever needing to reload the page (7 points)
 +
#** Refreshing the page does not log a user out (3 points)
 +
#* '''''Best Practices (20 Points):'''''
 +
#** Code is well formatted and easy to read, with proper commenting (2 points)
 +
#** If storing passwords, they are stored salted and hashed (2 points)
 +
#** All AJAX requests that either contain sensitive information or modify something on the server are performed via POST, not GET (3 points)
 +
#** Safe from XSS attacks; that is, all content is escaped on output (3 points)
 +
#** Safe from SQL Injection attacks (2 points)
 +
#** CSRF tokens are passed when adding/editing/deleting events (3 points)
 +
#** Session cookie is HTTP-Only (3 points)
 +
#** Page passes the W3C validator (2 points)
 +
#* '''''Usability (5 Points):'''''
 +
#** Site is intuitive to use and navigate (4 points)
 +
#** Site is visually appealing (1 point)
 +
# '''Creative Portion (15 Points)'''
 +
#* '''Additional Calendars Features (worth 15 points):''' Develop some additional features for the calendar, a few examples are provided below.
 +
#**  Users can tag an event with a particular category and enable/disable those tags in the calendar view. (5 points)
 +
#**  Users can share their calendar with additional users. (5 points)
 +
#**  Users can create group events that display on multiple users calendars (5 points)
 +
#*  '''Make sure to save a description of your creative portion, and a link to your server in your README.md file.'''
 +
''* These points can be earned only if at least 3 other points have been earned in that categoryThis is to prevent these points from being "free."''
  
[[Category:Module 6]]
+
[[Category:Module 5]]
 
[[Category:Modules]]
 
[[Category:Modules]]

Latest revision as of 15:23, 11 March 2021

In Module 5, you will learn JavaScript, the dominant client-side web language. JavaScript is the third and final programming language you will learn in CSE 330.

This article contains your assignments for Module 5.

Warning:

This module takes students a while. It is strongly recommended that you start early and ask questions as they come up.


Reading

The following articles on the online class wiki textbook contain information that will help you complete the assignments.

Individual Assignments

IMPORTANT: Both of the individual assignments must be completed without using a JavaScript library like jQuery. However, you may use jQuery for the group portion.

JavaScript Calculator

In Module 2, you made a calculator using PHP. Now you will be making one using JavaScript.

  • The web page should have two input fields and a radio button group for the operation, with the 4 basic math operations represented (add, subtract, multiply, divide).
  • JavaScript should monitor all three fields and display the current result whenever the user changes any value in any field, without refreshing the page.
    Tip 1: Consider using either the change or the keyup event on the text fields, and either the change or the click event on the radio buttons.
    Tip 2: Re-read the JavaScript guide if you need help figuring our how to bind callback functions to events, or how to determine which radio button is currently selected. (There are examples for both of these!)
  • The calculator should be completely self-contained; i.e., you should not be making any requests to any other server-side or client-side scripts or web pages after the initial page load.

Tip: You can embed JavaScript code into an HTML document like this:

<script>
// your code here
</script>

Tip: If your code isn't working the way you expect, use a JavaScript error console. In Chrome, for example, press Ctrl-Shift-I (or Cmd-Option-I) to open the WebKit inspector.

Important: It is best practice to fully separate the content, appearance, and behavior. In previous modules, you may have lost points for using HTML tags that change the appearance of things (like <center> and <font>). In this module, make sure that you define your event listeners in JavaScript as in document.getElementById("xyz").addEventListener(...) and not in the HTML like <button onclick="badExample();">Bad Example</button>.

Weather Widget

In this section, you will make a web page that displays the weather forecast using AJAX requests to a weather server.

  1. Make an empty HTML document; name it weather.html
    Refer to the HTML and CSS guide for the skeleton of an HTML document
  2. Define a function in JavaScript; call it fetchWeather(). You may write your JavaScript in an embedded script in your HTML document.
  3. Inside your fetchWeather() function, make an AJAX request to the weather server.
    We have a server that outputs the current weather in JSON format. The format is documented in the #JSON Structure section below.
    URL: https://classes.engineering.wustl.edu/cse330/content/weather_json.php
    • IMPORTANT: Under normal circumstances, AJAX requests cannot be performed cross-domain for security reasons. We have set the Access-Control-Allow-Origin header on our server to allow requests from your EC2 instances, or from localhost. You will therefore need to upload your code to your EC2, or access it via localhost, in order for the AJAX requests to work. This means that you cannot, for example, complete this part of the individual portion in JSFiddle.
  4. In your callback, process the JSON and use JavaScript to manipulate the HTML DOM to display the following information on your page:
    • Location
      • City, in a <strong> tag
      • State, not in any tag
    • Humidity
    • Current Temperature
    • Image for Tomorrow's Forecast (see #Weather Condition Images below for more information)
    • Image for the Day After Tomorrow's Forecast
  5. Finally, bind fetchWeather() to the DOMContentLoaded event so that your weather widget is automatically initialized when the page is loaded:
    document.addEventListener("DOMContentLoaded", fetchWeather, false);
    
  6. In addition, add a button that runs your fetchWeather function when clicked. This should update your widget with the current weather conditions.

Use the following HTML:

<div class="weather" id="weatherWidget">
	<div class="weather-loc"></div>
	<div class="weather-humidity"></div>
	<div class="weather-temp"></div>
	<img class="weather-tomorrow" />
	<img class="weather-dayaftertomorrow" />
</div>

Include the CSS file from here: https://classes.engineering.wustl.edu/cse330/content/weather.css

When everything is working, the weather widget should look something like this:

WeatherWidget.png

Important: The widget in this section needs to work in only Firefox and Chrome. It does not need to work Internet Explorer.

Tips

JSON Structure

The JSON from our server looks like this:

{
   "updated": "Thu, 11 Oct 2012 5:54 pm CDT",
   "location": {
      "city": "St. Louis",
      "state": "MO"
   },
   "wind": {
      "chill": "62",
      "direction": "150",
      "speed": "3 mph"
   },
   "atmosphere": {
      "humidity": "50",
      "visibility": "10",
      "pressure": "30.12 in"
   },
   "current": {
      "code": "28",
      "text": "Mostly Cloudy",
      "temp": "62°F",
      "date": "Thu, 11 Oct 2012 5:54 pm CDT"
   },
   "tomorrow": {
      "code": "29",
      "text": "Clouds Early/Clearing Late",
      "low": "45°F",
      "high": "61°F"
   },
   "dayafter": {
      "code": "30",
      "text": "Partly Cloudy",
      "low": "53°F",
      "high": "65°F"
   },
   "credit": "http://us.rd.yahoo.com/dailynews/rss/weather/St._Louis__MO/*http://weather.yahoo.com/forecast/USMO0170_f.html"
}
Weather Condition Images

Each day's forecast has a code. There are images associated with these codes.

One place to get the images is from here: http://us.yimg.com/i/us/nws/weather/gr/##ds.png

Replace the ## with the forecast code. For example, for code 32, the URL would be: http://us.yimg.com/i/us/nws/weather/gr/32ds.png

Group Project

I forgot if I already mentioned this, but start early on this project! It will take longer than you think, I promise!

You are encouraged to use the advanced version control practices you learned in Module 5 when completing this assignment.

Calendar

Build a simple calendar that allows users to add and remove events dynamically.

You will use JavaScript to process user interactions at the web browser, without ever refreshing the browser after the initial web page load. You may use a JavaScript library of your choice, including Ext JS, so long as that JavaScript library does not defeat the purpose of the assignment. If you have questions about this, ask. In particular, libraries like FullCalendar are unacceptable.

Your application should utilize AJAX to run server-side scripts that query your database to save and retrieve information, including user accounts and events.

We have also written some functions to help you get started: JavaScript Calendar Library

Examples

Server Side Language

Additionally, you may use a database of your choice. MySQL is probably the logical choice since you have been using it since Module 2, but you are free to explore other databases if you like.

Requirements

  • Support a month-by-month view of the calendar.
    Show one month at a time, with buttons to move forward or backward.
    There should be no limit to how far forward or backward the user can go.
  • Users can register and log in to the website.
    You may leverage your MySQL project code and database from module 3 to get started.
  • Unregistered users should see no events on the calendar.
  • Registered users can add events.
    All events should have a date and time, but do not need to have a duration.
    You do not need to support recurring events (where you add an event that repeats, for example, every monday).
  • Registered users see only events that they have added.
    Your AJAX should not ask the server for events from a certain username. Instead, your AJAX should ask the server for events, and the server should respond with the events for only the currently-logged-in user (from the session). Can you think of why?
  • Registered users can edit and delete their own events, but not the events of others.
    Again, the server should edit and delete events based on the username present in the session, not based on a username or event ID provided by the user. (If it deletes only based on an Event ID, an attacker could feed any arbitrary Event ID to your server, even if he/she didn't own that event.)
  • All user and event data should be kept in a database.
  • At no time should the main page need to be reloaded.
    User registration, user authentication, event addition, and event deletion should all be handled by JavaScript and AJAX requests to your server.
  • Your page needs to work in the versions of Firefox and Chrome installed on the lab computers.

Tip: Run your database schema by a TA before implementing it.

Web Security and Validation

Your project needs to demonstrate that thought was put into web security and best practice. For more information, see this week's Web Application Security guide: Web Application Security, Part 3

In particular:

  • Your application needs to prevent XSS attacks. Be careful when transmitting data over JSON that will be reflected in an event title! (Note: JSON data should be sanitized on the client side, not the server side.)
  • Perform precautionary measures to prevent session hijacking attacks.
    You should specify your session cookie to be HTTP-Only. However, for the means of this module, you need not test for user agent consistency.

You should continue the practices that you have learned in past weeks:

  • Pass tokens in forms to prevent CSRF attacks.
    Hint: You will need to send your CSRF tokens in your AJAX requests. Remember that AJAX still submits forms and runs server-side scripts, just like the vanilla forms you've been using in Modules 2 and 3.
  • Use prepared queries to prevent SQL Injection attacks.
  • If storing passwords in a database, always store them salted and encrypted.
  • Your page should validate with no errors or warnings through the W3C validator.

Grading

We will be grading the following aspects of your work. There are 100 points in total.

Assignments (including code) must be committed to Github by the end of class on the due date (commit early and often). Failing to commit by the end of class on the due date will result in a 0.

If you do not include a link to your individual and group portion running on your instance in the respective README.md, you will receive a 0 for the missing portion

_________

  1. JavaScript Calculator (10 Points):
    • Calculator successfully adds, subtracts, multiplies, and divides numbers (3 points)
    • The result is automatically computed when a value in either field is changed (3 points)
    • Calculator is written entirely in pure, conventional JavaScript with no external libraries (4 points)*
      Note: be sure to fully separate content, appearance, and behavior. This means that you should not use the event attributes on the HTML elements like onclick, onchange, and onkeyup.
  2. Weather Widget (15 Points):
    • Widget performs an AJAX request to fetch the current weather (4 points)
    • The HTML template is filled with the correct information, including an image (4 points)
    • Button to reload the weather widget (2 points)
    • Widget is written entirely in pure, conventional JavaScript with no external libraries (5 points)*
  3. AJAX Calendar (60 Points):
    • Calendar View (10 Points):
      • The calendar is displayed as a table grid with days as the columns and weeks as the rows, one month at a time (5 points)
      • The user can view different months as far in the past or future as desired (5 points)
    • User and Event Management (25 Points):
      • Events can be added, modified, and deleted (5 points)
      • Events have a title, date, and time (2 points)
      • Users can log into the site, and they cannot view or manipulate events associated with other users (8 points)
        Don't fall into the Abuse of Functionality trap! Check user credentials on the server side as well as on the client side.
      • All actions are performed over AJAX, without ever needing to reload the page (7 points)
      • Refreshing the page does not log a user out (3 points)
    • Best Practices (20 Points):
      • Code is well formatted and easy to read, with proper commenting (2 points)
      • If storing passwords, they are stored salted and hashed (2 points)
      • All AJAX requests that either contain sensitive information or modify something on the server are performed via POST, not GET (3 points)
      • Safe from XSS attacks; that is, all content is escaped on output (3 points)
      • Safe from SQL Injection attacks (2 points)
      • CSRF tokens are passed when adding/editing/deleting events (3 points)
      • Session cookie is HTTP-Only (3 points)
      • Page passes the W3C validator (2 points)
    • Usability (5 Points):
      • Site is intuitive to use and navigate (4 points)
      • Site is visually appealing (1 point)
  4. Creative Portion (15 Points)
    • Additional Calendars Features (worth 15 points): Develop some additional features for the calendar, a few examples are provided below.
      • Users can tag an event with a particular category and enable/disable those tags in the calendar view. (5 points)
      • Users can share their calendar with additional users. (5 points)
      • Users can create group events that display on multiple users calendars (5 points)
    • Make sure to save a description of your creative portion, and a link to your server in your README.md file.

* These points can be earned only if at least 3 other points have been earned in that category. This is to prevent these points from being "free."