diff --git a/assignments/week01/lab/echo_client.py b/assignments/week01/lab/echo_client.py index b8898436..09d5f562 100644 --- a/assignments/week01/lab/echo_client.py +++ b/assignments/week01/lab/echo_client.py @@ -1,16 +1,48 @@ -import socket +#! /usr/bin/python + import sys +import socket + + +class EchoClient(object): + """Interacts with Server, sending and recieving request data.""" + + def __init__(self, host='127.0.0.1', port=50000): + """Constructs the socket connection, configures the client.""" + + # Create a TCP/IP socket + self.client = socket.socket(2, 1, 0) + + # Connect the socket to the port where the server is listening + self.client.connect((host, port)) + + def MakeRequest(self, request): + # Send data to server + self.client.sendall(request) -# Create a TCP/IP socket + def UserInput(self): + value = raw_input("Add, Subtract, Multiply, or Divide two integers. (4 + 4) ") + return value -# Connect the socket to the port where the server is listening -server_address = ('localhost', 50000) + def FetchResult(self): + result = self.client.recv(1024) + return result + + def CloseSocket(self): + return self.client.close() -try: - # Send data - message = 'This is the message. It will be repeated.' - # print the response +def main(): + while True: + echo_client = EchoClient() + message = echo_client.UserInput() + try: + # send data + echo_client.MakeRequest(message) + # print the response + print echo_client.FetchResult() + finally: + print "Closing Socket" + echo_client.CloseSocket() -finally: - # close the socket to clean up +main() diff --git a/assignments/week01/lab/echo_server.py b/assignments/week01/lab/echo_server.py index e2c52fc6..ca718cf7 100644 --- a/assignments/week01/lab/echo_server.py +++ b/assignments/week01/lab/echo_server.py @@ -1,19 +1,60 @@ -import socket +#! /usr/bin/python + import sys +import socket -# Create a TCP/IP socket -# Bind the socket to the port -server_address = ('localhost', 50000) +class EchoServer(object): + """Interacts with Client, recieving and sending data.""" -# Listen for incoming connections + def __init__(self, host="127.0.0.1", port=50000, backlog=5): + """Constructs the socket connection, configures the server.""" -while True: + # Create a TCP/IP socket + self.server = socket.socket(2, 1, 0) + # Bind the socket to the port + self.server.bind((host, port)) + print 'Server started on port: ', port + # Listen for incoming connections + self.server.listen(backlog) + print("Server listening\n") + + def ProcessRequest(self): # Wait for a connection + self.con, self.cli = self.server.accept() + print 'New connection from ', self.cli + self.data = self.con.recv(1024) + print "Client Request: " + self.data + return self.data + + def ProcessResponse(self, response): + # Send data to client + self.con.sendall('The result is ' + str(response)) - try: - # Receive the data and send it back - + def DoMath(self, data): + self.data = self.data.split() + if self.data[1] == '+': + result = int(self.data[0]) + int(self.data[2]) + elif self.data[1] == '-': + result = int(self.data[0]) - int(self.data[2]) + elif self.data[1] == '*': + result = int(self.data[0]) * int(self.data[2]) + elif self.data[1] == '/': + result = int(self.data[0]) / int(self.data[2]) + return result + def CloseSocket(self): + self.con.close() + + +def main(): + echo_server = EchoServer() + while True: + data = echo_server.ProcessRequest() + try: + echo_server.ProcessResponse(echo_server.DoMath(data)) finally: - # Clean up the connection + print "Closing Socket" + echo_server.CloseSocket() + +main() diff --git a/assignments/week02/lab/http_serve1.py b/assignments/week02/lab/http_serve1.py new file mode 100644 index 00000000..3b815ee0 --- /dev/null +++ b/assignments/week02/lab/http_serve1.py @@ -0,0 +1,38 @@ +#!/usr/bin/env python + +import socket + +host = '' # listen on all connections (WiFi, etc) +port = 50000 +backlog = 5 # how many connections can we stack up +size = 1024 # number of bytes to receive at once + +http = """ + \ +

This is a header

\ +

\ + and this is some regular text \ +

\ +

\ + and some more \ +

\ + \ + """ + + +## create the socket +s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) +# set an option to tell the OS to re-use the socket +s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + +# the bind makes it a server +s.bind( (host,port) ) +s.listen(backlog) + +while True: # keep looking for new connections forever + client, address = s.accept() # look for a connection + data = client.recv(size) + if data: # if the connection was closed there would be no data + print "received: %s, sending it back"%data + client.send(http) + client.close() diff --git a/assignments/week02/lab/http_serve2.py b/assignments/week02/lab/http_serve2.py new file mode 100644 index 00000000..bf7f9cf7 --- /dev/null +++ b/assignments/week02/lab/http_serve2.py @@ -0,0 +1,82 @@ +#!/usr/bin/env python + +import socket +import httplib +import sys, os + + + +class EchoServer(object): + """Interacts with Client, recieving and sending data.""" + + def __init__(self, host='', port=50000, backlog=5): + """Constructs the socket connection, configures the server.""" + + # Create a TCP/IP socket + self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + # Bind the socket to the port + self.server.bind((host, port)) + print 'Server started on port: ', port + # Listen for incoming connections + self.server.listen(backlog) + print("Server listening\n") + ## create the socket + # set an option to tell the OS to re-use the socket + self.server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + + self.size = 1024 # number of bytes to receive at once + + def ProcessRequest(self, size): + # Wait for a connection + self.con, self.cli = self.server.accept() + print 'New connection from ', self.cli + self.data = self.con.recv(size) + print "Client Request: " + self.data + return self.data + + def ProcessResponse(self, data): + # Send data to client + self.con.send(data) + + def CloseSocket(self): + self.con.close() + + def Payload(self): + self.body = """ + \ +

This is a header

\ +

\ + and this is some regular text \ +

\ +

\ + and some more \ +

\ + \ + """ + return self.body + + def Response(self): + body = self.Payload() + ok = "HTTP/1.1 200 OK" + empty = "" + resp = "\r\n".join([ok, empty, body]) + return resp + + def ParseRequest(data): + for line in data: + if "GET" in line: + print "FOUND GET" + + +def main(): + echo_server = EchoServer() + while True: + data = echo_server.ProcessRequest(echo_server.size) + try: + if data: # if the connection was closed there would be no data + echo_server.ProcessResponse(echo_server.Response()) + finally: + print "Closing Socket" + echo_server.CloseSocket() + +main() diff --git a/assignments/week02/lab/http_serve3.py b/assignments/week02/lab/http_serve3.py new file mode 100644 index 00000000..9df4441a --- /dev/null +++ b/assignments/week02/lab/http_serve3.py @@ -0,0 +1,85 @@ +#!/usr/bin/env python + +import socket +import httplib +import sys, os + + +class EchoServer(object): + """Interacts with Client, recieving and sending data.""" + + def __init__(self, host='', port=50001, backlog=5): + """Constructs the socket connection, configures the server.""" + + # Create a TCP/IP socket + self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + # Bind the socket to the port + self.server.bind((host, port)) + print 'Server started on port: ', port + # Listen for incoming connections + self.server.listen(backlog) + print("Server listening\n") + ## create the socket + # set an option to tell the OS to re-use the socket + self.server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + + self.size = 1024 # number of bytes to receive at once + + def ProcessRequest(self, size): + # Wait for a connection + self.con, self.cli = self.server.accept() + print 'New connection from ', self.cli + self.data = self.con.recv(size) + return self.data + + def ProcessResponse(self, data): + # Send data to client + self.con.send(data) + + def CloseSocket(self): + self.con.close() + + def ParseRequest(self, request): + i = request.split() + (verb, http) = i[0], i[2] + return (verb, http) + + def Payload(self, body): + body = """ + \ +

""" + body + """

\ + """ + return body + + def ClientErrorResponse(self): + bad = "HTTP/1.1 400 Bad Request" + body = self.Payload(bad) + empty = "" + resp = "\r\n".join([bad, empty, body]) + return resp + + def Response(self): + ok = "HTTP/1.1 200 OK" + body = self.Payload(ok) + empty = "" + resp = "\r\n".join([ok, empty, body]) + return resp + + +def main(): + echo_server = EchoServer() + while True: + data = echo_server.ProcessRequest(echo_server.size) + try: + if data: # if the connection was closed there would be no data + (verb, http) = echo_server.ParseRequest(data) + if verb == 'GET' and http == "HTTP/1.1": + echo_server.ProcessResponse(echo_server.Response()) + else: + echo_server.ProcessResponse(echo_server.ClientErrorResponse()) + raise ValueError('%s %s is a bad request' % (verb, http)) + finally: + print "Closing Socket" + echo_server.CloseSocket() + +main() diff --git a/assignments/week02/lab/http_serve4.py b/assignments/week02/lab/http_serve4.py new file mode 100644 index 00000000..11733162 --- /dev/null +++ b/assignments/week02/lab/http_serve4.py @@ -0,0 +1,102 @@ +#!/usr/bin/env python + +import socket +import httplib +import os + + +class EchoServer(object): + """Interacts with Client, recieving and sending data.""" + + def __init__(self, host='', port=50001, backlog=5): + """Constructs the socket connection, configures the server.""" + + # Create a TCP/IP socket + self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + # Bind the socket to the port + self.server.bind((host, port)) + print 'Server started on port: ', port + # Listen for incoming connections + self.server.listen(backlog) + print("Server listening\n") + ## create the socket + # set an option to tell the OS to re-use the socket + self.server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + + self.size = 1024 # number of bytes to receive at once + + def ProcessRequest(self, size): + # Wait for a connection + self.con, self.cli = self.server.accept() + print 'New connection from ', self.cli + self.data = self.con.recv(size) + return self.data + + def ProcessResponse(self, data): + # Send data to client + self.con.send(data) + + def CloseSocket(self): + self.con.close() + + def ParseRequest(self, request): + i = request.split() + print i + (verb, path, http) = i[0], i[1], i[2] + return (verb, path, http) + + def Payload(self, header, body): + body = """ + \ +

""" + header + """

\ +

""" + body + """

\ + """ + return body + + def ClientErrorResponse(self): + bad = "HTTP/1.1 400 Bad Request" + body = self.Payload(bad) + empty = "" + resp = "\r\n".join([bad, empty, body]) + return resp + + def NotFoundResponse(self): + notfound = "HTTP/1.1 404 Not Found" + body = self.Payload(notfound) + empty = "" + resp = "\r\n".join([notfound, empty, body]) + return resp + + def Response(self, data=''): + ok = "HTTP/1.1 200 OK" + body = self.Payload(ok, data) + empty = "" + resp = "\r\n".join([ok, empty, body]) + return resp + + def ResolveUri(self, path): + if os.path.realpath.exists(path): + content = os.listdir(path) + return content + +def main(): + echo_server = EchoServer() + while True: + data = echo_server.ProcessRequest(echo_server.size) + try: + if data: # if the connection was closed there would be no data + (verb, path, http) = echo_server.ParseRequest(data) + resolve_uri = echo_server.ResolveUri(path) + if verb == 'GET' and http == "HTTP/1.1": + print echo_server.ResolveUri(path) + echo_server.ProcessResponse(echo_server.Response()) +# return echo_server.NotFoundResponse() +# raise ValueError('%s was not found' % (path)) + else: + echo_server.ProcessResponse(echo_server.ClientErrorResponse()) + raise ValueError('%s %s is a bad request' % (verb, http)) + finally: + print "Closing Socket" + echo_server.CloseSocket() + +main() diff --git a/assignments/week05/athome/README.txt b/assignments/week05/athome/README.txt new file mode 100644 index 00000000..77717591 --- /dev/null +++ b/assignments/week05/athome/README.txt @@ -0,0 +1,27 @@ +Submittin Your Work + +* Try to get your code running on your VM + I was able to finally get flaskr working on my vm: + http://block647052-gfz.blueboxgrid.com + +* Add a README.txt file that discusses the experience. + I decided to try and port the flaskr app to Bottle + Project: flaskr_to_bottler + + I started working on getting this all installed to my vm with a new virtualenv for Bottle. I was able to test the functionality by adding a simple Hello_World to confirm that it was working. I then copied the flaskr project over and began picking apart the differences. + +Templates: +I attempted to use jinja2_template from bottle in order to utilize the existing templates. Unfortunately, no matter where I place my *.html (views, templates), I get a 404 stating "Not found: '/'" +I did try to use the TEMPLATE_PATH.append("./templates") to force my app to use that directory but no dice. +Used the FlashPlugin: Imported bottle_flash +Used the SQLitePlugin: This allow for a call to be made to the db any time a route requires it. Handy instead of using 'g' + +Resources: +You must the resources.add_path() method to define where files like schema.sql will live. This was a gotchya when I tried to reinitialize a new bottler.db. After doing this, I still found that my entries table was not created, I had have to perform this manually by using the sqlite3 command to open a connection to bottler.db and create the table. + +* Commit your changes to your fork of the class repository and send me a pull + request + + + + diff --git a/assignments/week05/athome/flaskr_to_bottler/._flaskr.py b/assignments/week05/athome/flaskr_to_bottler/._flaskr.py new file mode 100755 index 00000000..19167555 Binary files /dev/null and b/assignments/week05/athome/flaskr_to_bottler/._flaskr.py differ diff --git a/assignments/week05/athome/flaskr_to_bottler/._flaskr_tests.py b/assignments/week05/athome/flaskr_to_bottler/._flaskr_tests.py new file mode 100755 index 00000000..354eb3bb Binary files /dev/null and b/assignments/week05/athome/flaskr_to_bottler/._flaskr_tests.py differ diff --git a/assignments/week05/athome/flaskr_to_bottler/._schema.sql b/assignments/week05/athome/flaskr_to_bottler/._schema.sql new file mode 100755 index 00000000..8801ca5d Binary files /dev/null and b/assignments/week05/athome/flaskr_to_bottler/._schema.sql differ diff --git a/assignments/week05/athome/flaskr_to_bottler/bottler.py b/assignments/week05/athome/flaskr_to_bottler/bottler.py new file mode 100755 index 00000000..0b4ca2d5 --- /dev/null +++ b/assignments/week05/athome/flaskr_to_bottler/bottler.py @@ -0,0 +1,93 @@ +import sqlite3 +from contextlib import closing + +from bottle import Bottle +from bottle import default_app +from bottle import error +from bottle import get +from bottle import install +from bottle import post +from bottle import redirect +from bottle import request +from bottle import static_file +from bottle import TEMPLATE_PATH +from bottle import url +from bottle import jinja2_template as template + +from bottle_sqlite import SQLitePlugin +from bottle_flash import FlashPlugin + +# configuration goes here +DATABASE = '/tmp/bottler.db' +SECRET_KEY = 'development key' +USERNAME = 'admin' +PASSWORD = 'default' + +app = Bottle(__name__) + +# Resource configuration (relative to current file) +app.resources.add_path('./', base=__file__) +TEMPLATE_PATH.append("./templates") + +app.install(SQLitePlugin(dbfile='/tmp/bottler.db')) +app.install(FlashPlugin(secret='COOKIE_SECRET')) + +def connect_db(): + return sqlite3.connect('/tmp/bottler.db') + + +def init_db(): + with closing(connect_db()) as db: + with app.resources.open('schema.sql') as f: + db.cursor().executescript(f.read()) + db.commit() + +#@app.before_request +#def before_request(): +# g.db = connect_db() + + +#@app.teardown_request +#def teardown_request(exception): +# g.db.close() + +def write_entry(title, text): + db.execute('insert into entries (title, text) values (?, ?)', + [title, text]) + db.commit() + + +def get_all_entries(): + db = connect_db() + cur = db.execute('select title, text from entries order by id desc') + entries = [dict(title=row[0], text=row[1]) for row in cur.fetchall()] + return entries + +@app.route('/') +def show_entries(): + db = connect_db() + cur = db.execute('select title, text from entries order by id desc') + entries = [dict(title=row[0], text=row[1]) for row in cur.fetchall()] + return template('show_entries.html', entries=entries) + +############################################################## +# TEST PASS +# +#@app.route('/') +#@app.route('/hello/') +#def greet(name='Stranger'): +# return template('home.html', name=name) +############################################################## + +@app.route('/add', methods=['POST']) +def add_entry(): + try: + write_entry(request.form['title'], request.form['text']) + flash('New entry was successfully posted') + except sqlite3.Error as e: + flash('There was an error: %s' % e.args[0]) + return redirect(url('show_entries')) + + +if __name__ == '__main__': + app.run(debug=True) diff --git a/assignments/week05/athome/flaskr_to_bottler/schema.sql b/assignments/week05/athome/flaskr_to_bottler/schema.sql new file mode 100755 index 00000000..71fe0588 --- /dev/null +++ b/assignments/week05/athome/flaskr_to_bottler/schema.sql @@ -0,0 +1,6 @@ +drop table if exists entries; +create table entries ( + id integer primary key autoincrement, + title string not null, + text string not null +); diff --git a/assignments/week05/athome/flaskr_to_bottler/static/._style.css b/assignments/week05/athome/flaskr_to_bottler/static/._style.css new file mode 100755 index 00000000..b3be1bcd Binary files /dev/null and b/assignments/week05/athome/flaskr_to_bottler/static/._style.css differ diff --git a/assignments/week05/athome/flaskr_to_bottler/static/style.css b/assignments/week05/athome/flaskr_to_bottler/static/style.css new file mode 100755 index 00000000..f53d77f7 --- /dev/null +++ b/assignments/week05/athome/flaskr_to_bottler/static/style.css @@ -0,0 +1,17 @@ +body { font-family: sans-serif; background: #eee; } +a, h1, h2 { color: #377BA8; } +h1, h2 { font-family: 'Georgia', serif; margin: 0; } +h1 { border-bottom: 2px solid #eee; } +h2 { font-size: 1.2em; } +.page { margin: 2em auto; width: 35em; border: 5px solid #ccc; + padding: 0.8em; background: white; } +.entries { list-style: none; margin: 0; padding: 0; } +.entries li { margin: 0.8em 1.2em; } +.entries li h2 { margin-left: -1em; } +.add-entry { font-size: 0.9em; border-bottom: 1px solid #ccc; } +.add-entry dl { font-weight: bold; } +.metanav { text-align: right; font-size: 0.8em; padding: 0.3em; + margin-bottom: 1em; background: #fafafa; } +.flash { background: #CEE5F5; padding: 0.5em; + border: 1px solid #AACBE2; } +.error { background: #F0D6D6; padding: 0.5em; } \ No newline at end of file diff --git a/assignments/week05/athome/flaskr_to_bottler/templates/._layout.html b/assignments/week05/athome/flaskr_to_bottler/templates/._layout.html new file mode 100755 index 00000000..df483305 Binary files /dev/null and b/assignments/week05/athome/flaskr_to_bottler/templates/._layout.html differ diff --git a/assignments/week05/athome/flaskr_to_bottler/templates/._login.html b/assignments/week05/athome/flaskr_to_bottler/templates/._login.html new file mode 100755 index 00000000..5c63ad94 Binary files /dev/null and b/assignments/week05/athome/flaskr_to_bottler/templates/._login.html differ diff --git a/assignments/week05/athome/flaskr_to_bottler/templates/._show_entries.html b/assignments/week05/athome/flaskr_to_bottler/templates/._show_entries.html new file mode 100755 index 00000000..56053a41 Binary files /dev/null and b/assignments/week05/athome/flaskr_to_bottler/templates/._show_entries.html differ diff --git a/assignments/week05/athome/flaskr_to_bottler/templates/base.html b/assignments/week05/athome/flaskr_to_bottler/templates/base.html new file mode 100755 index 00000000..77024c71 --- /dev/null +++ b/assignments/week05/athome/flaskr_to_bottler/templates/base.html @@ -0,0 +1,9 @@ + + + Bottler + + + + {% block content %} {% endblock %} + + diff --git a/assignments/week05/athome/flaskr_to_bottler/templates/hello_template.tpl b/assignments/week05/athome/flaskr_to_bottler/templates/hello_template.tpl new file mode 100644 index 00000000..176d989f --- /dev/null +++ b/assignments/week05/athome/flaskr_to_bottler/templates/hello_template.tpl @@ -0,0 +1,7 @@ +%if name == 'World': +

Hello {{name}}!

+

This is a test.

+%else: +

Hello {{name.title()}}!

+

How are you?

+%end diff --git a/assignments/week05/athome/flaskr_to_bottler/templates/home.html b/assignments/week05/athome/flaskr_to_bottler/templates/home.html new file mode 100755 index 00000000..0262b17c --- /dev/null +++ b/assignments/week05/athome/flaskr_to_bottler/templates/home.html @@ -0,0 +1,7 @@ +{% extends "base.html" %} + +{% block content %} + + Hello {{ name }}! How are you? + +{% endblock %} diff --git a/assignments/week05/athome/flaskr_to_bottler/templates/layout.html b/assignments/week05/athome/flaskr_to_bottler/templates/layout.html new file mode 100755 index 00000000..5a464e7b --- /dev/null +++ b/assignments/week05/athome/flaskr_to_bottler/templates/layout.html @@ -0,0 +1,22 @@ + + + + Bottler + + + +

Flaskr

+ {% if not session.logged_in %} + log in + {% else %} + log_out + {% endif %} + + {% for message in get_flashed_messages() %} +
{{ message }}
+ {% endfor %} +
+ {% block body %}{% endblock %} +
+ + diff --git a/assignments/week05/athome/flaskr_to_bottler/templates/layout.tpl b/assignments/week05/athome/flaskr_to_bottler/templates/layout.tpl new file mode 100755 index 00000000..4b595312 --- /dev/null +++ b/assignments/week05/athome/flaskr_to_bottler/templates/layout.tpl @@ -0,0 +1,22 @@ + + + + Flaskr + + + +

Flaskr

+ {% if not session.logged_in %} + log in + {% else %} + log_out + {% endif %} + + {% for message in get_flashed_messages() %} +
{{ message }}
+ {% endfor %} +
+ {% block body %}{% endblock %} +
+ + \ No newline at end of file diff --git a/assignments/week05/athome/flaskr_to_bottler/templates/login.html b/assignments/week05/athome/flaskr_to_bottler/templates/login.html new file mode 100755 index 00000000..5889ca39 --- /dev/null +++ b/assignments/week05/athome/flaskr_to_bottler/templates/login.html @@ -0,0 +1,20 @@ +{% extends "layout.html" %} +{% block body %} +

Login

+ {% if error -%} +

Error {{ error }} + {%- endif %} +

+
+ + +
+
+ + +
+
+ +
+
+{% endblock %} diff --git a/assignments/week05/athome/flaskr_to_bottler/templates/show_entries.html b/assignments/week05/athome/flaskr_to_bottler/templates/show_entries.html new file mode 100755 index 00000000..07738258 --- /dev/null +++ b/assignments/week05/athome/flaskr_to_bottler/templates/show_entries.html @@ -0,0 +1,29 @@ +{% extends "layout.html" %} +{% block body %} +
+
+ + +
+
+ + +
+
+ +
+
+

Posts

+ +{% endblock %} diff --git a/assignments/week05/athome/flaskr_to_bottler/templates/show_entries.tpl b/assignments/week05/athome/flaskr_to_bottler/templates/show_entries.tpl new file mode 100755 index 00000000..16fc56af --- /dev/null +++ b/assignments/week05/athome/flaskr_to_bottler/templates/show_entries.tpl @@ -0,0 +1,31 @@ +{% extends "layout.html" %} +{% block body %} + {% if session.logged_in %} +
+
+ + +
+
+ + +
+
+ +
+
+ {% endif %} +

Posts

+ +{% endblock %} diff --git a/assignments/week05/athome/flaskr_to_bottler/views/._layout.html b/assignments/week05/athome/flaskr_to_bottler/views/._layout.html new file mode 100755 index 00000000..df483305 Binary files /dev/null and b/assignments/week05/athome/flaskr_to_bottler/views/._layout.html differ diff --git a/assignments/week05/athome/flaskr_to_bottler/views/._login.html b/assignments/week05/athome/flaskr_to_bottler/views/._login.html new file mode 100755 index 00000000..5c63ad94 Binary files /dev/null and b/assignments/week05/athome/flaskr_to_bottler/views/._login.html differ diff --git a/assignments/week05/athome/flaskr_to_bottler/views/._show_entries.html b/assignments/week05/athome/flaskr_to_bottler/views/._show_entries.html new file mode 100755 index 00000000..56053a41 Binary files /dev/null and b/assignments/week05/athome/flaskr_to_bottler/views/._show_entries.html differ diff --git a/assignments/week05/athome/flaskr_to_bottler/views/base.html b/assignments/week05/athome/flaskr_to_bottler/views/base.html new file mode 100755 index 00000000..77024c71 --- /dev/null +++ b/assignments/week05/athome/flaskr_to_bottler/views/base.html @@ -0,0 +1,9 @@ + + + Bottler + + + + {% block content %} {% endblock %} + + diff --git a/assignments/week05/athome/flaskr_to_bottler/views/hello_template.tpl b/assignments/week05/athome/flaskr_to_bottler/views/hello_template.tpl new file mode 100644 index 00000000..176d989f --- /dev/null +++ b/assignments/week05/athome/flaskr_to_bottler/views/hello_template.tpl @@ -0,0 +1,7 @@ +%if name == 'World': +

Hello {{name}}!

+

This is a test.

+%else: +

Hello {{name.title()}}!

+

How are you?

+%end diff --git a/assignments/week05/athome/flaskr_to_bottler/views/home.html b/assignments/week05/athome/flaskr_to_bottler/views/home.html new file mode 100755 index 00000000..0262b17c --- /dev/null +++ b/assignments/week05/athome/flaskr_to_bottler/views/home.html @@ -0,0 +1,7 @@ +{% extends "base.html" %} + +{% block content %} + + Hello {{ name }}! How are you? + +{% endblock %} diff --git a/assignments/week05/athome/flaskr_to_bottler/views/layout.html b/assignments/week05/athome/flaskr_to_bottler/views/layout.html new file mode 100755 index 00000000..4b595312 --- /dev/null +++ b/assignments/week05/athome/flaskr_to_bottler/views/layout.html @@ -0,0 +1,22 @@ + + + + Flaskr + + + +

Flaskr

+ {% if not session.logged_in %} + log in + {% else %} + log_out + {% endif %} + + {% for message in get_flashed_messages() %} +
{{ message }}
+ {% endfor %} +
+ {% block body %}{% endblock %} +
+ + \ No newline at end of file diff --git a/assignments/week05/athome/flaskr_to_bottler/views/login.html b/assignments/week05/athome/flaskr_to_bottler/views/login.html new file mode 100755 index 00000000..ed4554db --- /dev/null +++ b/assignments/week05/athome/flaskr_to_bottler/views/login.html @@ -0,0 +1,20 @@ +{% extends "layout.html" %} +{% block body %} +

Login

+ {% if error -%} +

Error {{ error }} + {%- endif %} +

+
+ + +
+
+ + +
+
+ +
+
+{% endblock %} diff --git a/assignments/week05/athome/flaskr_to_bottler/views/make_table.tpl b/assignments/week05/athome/flaskr_to_bottler/views/make_table.tpl new file mode 100755 index 00000000..e9e1bb20 --- /dev/null +++ b/assignments/week05/athome/flaskr_to_bottler/views/make_table.tpl @@ -0,0 +1,11 @@ +%#template to generate a HTML table from a list of tuples (or list of lists, or tuple of tuples or ...) +

The open items are as follows:

+ +%for row in rows: + + %for col in row: + + %end + +%end +
{{col}}
diff --git a/assignments/week05/athome/flaskr_to_bottler/views/show_entries.html b/assignments/week05/athome/flaskr_to_bottler/views/show_entries.html new file mode 100755 index 00000000..f44fd92b --- /dev/null +++ b/assignments/week05/athome/flaskr_to_bottler/views/show_entries.html @@ -0,0 +1,31 @@ +{% extends "layout.html" %} +{% block body %} + {% if session.logged_in %} +
+
+ + +
+
+ + +
+
+ +
+
+ {% endif %} +

Posts

+ +{% endblock %} \ No newline at end of file diff --git a/assignments/week05/athome/flaskr_to_bottler/views/show_entries.tpl b/assignments/week05/athome/flaskr_to_bottler/views/show_entries.tpl new file mode 100755 index 00000000..f44fd92b --- /dev/null +++ b/assignments/week05/athome/flaskr_to_bottler/views/show_entries.tpl @@ -0,0 +1,31 @@ +{% extends "layout.html" %} +{% block body %} + {% if session.logged_in %} +
+
+ + +
+
+ + +
+
+ +
+
+ {% endif %} +

Posts

+ +{% endblock %} \ No newline at end of file diff --git a/assignments/week06/athome/djangoenv/bin/newsite/classlab/.models.py.swp b/assignments/week06/athome/djangoenv/bin/newsite/classlab/.models.py.swp new file mode 100644 index 00000000..b7fcbd95 Binary files /dev/null and b/assignments/week06/athome/djangoenv/bin/newsite/classlab/.models.py.swp differ diff --git a/assignments/week06/athome/djangoenv/bin/newsite/classlab/__init__.py b/assignments/week06/athome/djangoenv/bin/newsite/classlab/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/assignments/week06/athome/djangoenv/bin/newsite/classlab/__init__.pyc b/assignments/week06/athome/djangoenv/bin/newsite/classlab/__init__.pyc new file mode 100644 index 00000000..2d09e603 Binary files /dev/null and b/assignments/week06/athome/djangoenv/bin/newsite/classlab/__init__.pyc differ diff --git a/assignments/week06/athome/djangoenv/bin/newsite/classlab/admin.py b/assignments/week06/athome/djangoenv/bin/newsite/classlab/admin.py new file mode 100644 index 00000000..e4d2f54e --- /dev/null +++ b/assignments/week06/athome/djangoenv/bin/newsite/classlab/admin.py @@ -0,0 +1,17 @@ +from django.contrib import admin +from classlab.models import Poll, Choice + +class ChoiceInline(admin.TabularInline): + model = Choice + extra = 3 + ordering = ('choice', ) + +class PollAdmin(admin.ModelAdmin): + list_display = ('pub_date', 'question', + 'published_today') + list_filter = ('pub_date', ) + ordering = ('pub_date', ) + inlines = (ChoiceInline, ) + +admin.site.register(Choice) +admin.site.register(Poll, PollAdmin) diff --git a/assignments/week06/athome/djangoenv/bin/newsite/classlab/admin.pyc b/assignments/week06/athome/djangoenv/bin/newsite/classlab/admin.pyc new file mode 100644 index 00000000..bddc2298 Binary files /dev/null and b/assignments/week06/athome/djangoenv/bin/newsite/classlab/admin.pyc differ diff --git a/assignments/week06/athome/djangoenv/bin/newsite/classlab/models.py b/assignments/week06/athome/djangoenv/bin/newsite/classlab/models.py new file mode 100644 index 00000000..39085e4c --- /dev/null +++ b/assignments/week06/athome/djangoenv/bin/newsite/classlab/models.py @@ -0,0 +1,25 @@ +from django.db import models +from django.utils import timezone + +class Poll(models.Model): + question = models.CharField(max_length=200) + pub_date = models.DateTimeField('date published') + + def __unicode__(self): + return self.question + + def published_today(self): + now = timezone.now() + time_delta = now - self.pub_date + return time_delta.days == 0 + + published_today.boolean = True + published_today.short_description = "Published Today?" + +class Choice(models.Model): + poll = models.ForeignKey(Poll) + choice = models.CharField(max_length=200) + votes = models.IntegerField(default=0) + + def __unicode__(self): + return self.choice diff --git a/assignments/week06/athome/djangoenv/bin/newsite/classlab/models.pyc b/assignments/week06/athome/djangoenv/bin/newsite/classlab/models.pyc new file mode 100644 index 00000000..071d92fd Binary files /dev/null and b/assignments/week06/athome/djangoenv/bin/newsite/classlab/models.pyc differ diff --git a/assignments/week06/athome/djangoenv/bin/newsite/classlab/templates/base.html b/assignments/week06/athome/djangoenv/bin/newsite/classlab/templates/base.html new file mode 100644 index 00000000..b49a18d0 --- /dev/null +++ b/assignments/week06/athome/djangoenv/bin/newsite/classlab/templates/base.html @@ -0,0 +1,20 @@ + + + + New Site + + +
+
+ {% if messages %} + {% for message in messages %} +

+ {{ message }}

+ {% endfor %} + {% endif %} + {% block content %} + {% endblock %} +
+
+ + diff --git a/assignments/week06/athome/djangoenv/bin/newsite/classlab/templates/polls/detail.html b/assignments/week06/athome/djangoenv/bin/newsite/classlab/templates/polls/detail.html new file mode 100644 index 00000000..a842f41c --- /dev/null +++ b/assignments/week06/athome/djangoenv/bin/newsite/classlab/templates/polls/detail.html @@ -0,0 +1,21 @@ +{% extends "base.html" %} + +{% block content %} +

{{ poll }}

+{% if poll.choice_set.count > 0 %} +
+ {% csrf_token %} + {% for choice in poll.choice_set.all %} +
+ +
+ {% endfor %} + +
+{% else %} +

No choices are available for this poll

+{% endif %} +{% endblock %} \ No newline at end of file diff --git a/assignments/week06/athome/djangoenv/bin/newsite/classlab/templates/polls/list.html b/assignments/week06/athome/djangoenv/bin/newsite/classlab/templates/polls/list.html new file mode 100644 index 00000000..3a00ee5d --- /dev/null +++ b/assignments/week06/athome/djangoenv/bin/newsite/classlab/templates/polls/list.html @@ -0,0 +1,11 @@ +{% extends "base.html" %} + +{% block content %} +

Latest Polls

+{% for poll in polls %} +
+

{{ poll }}

+

Total Votes: {{ classlab.total_votes }}

+
+{% endfor %} +{% endblock %} diff --git a/assignments/week06/athome/djangoenv/bin/newsite/classlab/templates/polls/result.html b/assignments/week06/athome/djangoenv/bin/newsite/classlab/templates/polls/result.html new file mode 100644 index 00000000..8937985a --- /dev/null +++ b/assignments/week06/athome/djangoenv/bin/newsite/classlab/templates/polls/result.html @@ -0,0 +1,11 @@ +{% extends "base.html" %} + +{% block content %} +

{{ poll }}

+ +Back to the polls, please +{% endblock %} \ No newline at end of file diff --git a/assignments/week06/athome/djangoenv/bin/newsite/classlab/tests.py b/assignments/week06/athome/djangoenv/bin/newsite/classlab/tests.py new file mode 100644 index 00000000..f5a8883a --- /dev/null +++ b/assignments/week06/athome/djangoenv/bin/newsite/classlab/tests.py @@ -0,0 +1,41 @@ +""" +This file demonstrates writing tests using the unittest module. These will pass +when you run "manage.py test". + +Replace this with more appropriate tests for your application. +""" + +from datetime import timedelta + +from django.test import TestCase +from django.utils import timezone + +from classlab.models import Poll + +class PollTest(TestCase): + def setUp(self): + self.expected_question = "what is the question?" + self.expected_choice = "do you like spongecake?" + self.poll = Poll.objects.create( + question=self.expected_question, + pub_date=timezone.now()) + self.choice = self.poll.choice_set.create( + choice=self.expected_choice) + + def test_poll_display(self): + self.assertEquals(unicode(self.poll), self.expected_question) + new_question = "What is the answer?" + self.poll.question = new_question + self.assertEquals(unicode(self.poll), new_question) + + def test_choice_display(self): + self.assertEquals(unicode(self.choice), self.expected_choice) + new_choice = "is left better than right?" + self.choice.choice = new_choice + self.assertEquals(unicode(self.choice), new_choice) + + def test_published_today(self): + self.assertTrue(self.poll.published_today()) + delta = timedelta(hours=26) + self.poll.pub_date = self.poll.pub_date - delta + self.assertFalse(self.poll.published_today()) diff --git a/assignments/week06/athome/djangoenv/bin/newsite/classlab/tests.pyc b/assignments/week06/athome/djangoenv/bin/newsite/classlab/tests.pyc new file mode 100644 index 00000000..c2cd0b80 Binary files /dev/null and b/assignments/week06/athome/djangoenv/bin/newsite/classlab/tests.pyc differ diff --git a/assignments/week06/athome/djangoenv/bin/newsite/classlab/urls.py b/assignments/week06/athome/djangoenv/bin/newsite/classlab/urls.py new file mode 100644 index 00000000..f6aefd1a --- /dev/null +++ b/assignments/week06/athome/djangoenv/bin/newsite/classlab/urls.py @@ -0,0 +1,32 @@ +from django.conf.urls import patterns, url +from django.http import HttpResponse +from django.views.generic import ListView, DetailView + +from classlab.models import Poll + +def stub(request, *args, **kwargs): + return HttpResponse('stub view', mimetype="text/plain") + +urlpatterns = patterns('', + url(r'^$', + ListView.as_view( + queryset=Poll.objects.order_by('-pub_date')[:5], + context_object_name='polls', + template_name="polls/list.html" + ), + name="poll_list"), + url(r'^(?P\d+)/$', + DetailView.as_view( + model=Poll, + template_name="polls/detail.html" + ), + name="poll_detail"), + url(r'^(?P\d+)/vote/$', + 'classlab.views.vote_view', + name="poll_vote"), + url(r'^(?P\d+)/result/$', + DetailView.as_view( + model=Poll, + template_name="polls/result.html"), + name="poll_result") +) diff --git a/assignments/week06/athome/djangoenv/bin/newsite/classlab/urls.pyc b/assignments/week06/athome/djangoenv/bin/newsite/classlab/urls.pyc new file mode 100644 index 00000000..41ff59e6 Binary files /dev/null and b/assignments/week06/athome/djangoenv/bin/newsite/classlab/urls.pyc differ diff --git a/assignments/week06/athome/djangoenv/bin/newsite/classlab/views.py b/assignments/week06/athome/djangoenv/bin/newsite/classlab/views.py new file mode 100644 index 00000000..50a98866 --- /dev/null +++ b/assignments/week06/athome/djangoenv/bin/newsite/classlab/views.py @@ -0,0 +1,26 @@ +from django.core.urlresolvers import reverse +from django.shortcuts import get_object_or_404 +from django.contrib import messages +from django.http import HttpResponseRedirect +from classlab.models import Poll, Choice + +def vote_view(request, pk): + poll = get_object_or_404(Poll, pk=pk) + if request.method == "POST": + try: + choice = poll.choice_set.get( + pk=request.POST.get('choice', 0)) + except Choice.DoesNotExist: + msg = "Ooops, pick a choice that exists, please" + messages.add_message(request, messages.ERROR, msg) + url = reverse('poll_detail', args=[pk, ]) + else: + choice.votes += 1 + choice.save() + messages.add_message(request, messages.INFO, + "You voted for %s" % choice) + url = reverse('poll_result', args=[pk]) + else: + url = reverse('poll_detail', args=[pk, ]) + + return HttpResponseRedirect(url) diff --git a/assignments/week06/athome/djangoenv/bin/newsite/classlab/views.pyc b/assignments/week06/athome/djangoenv/bin/newsite/classlab/views.pyc new file mode 100644 index 00000000..fd8e5d7a Binary files /dev/null and b/assignments/week06/athome/djangoenv/bin/newsite/classlab/views.pyc differ diff --git a/assignments/week06/athome/djangoenv/bin/newsite/djangor/__init__.py b/assignments/week06/athome/djangoenv/bin/newsite/djangor/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/assignments/week06/athome/djangoenv/bin/newsite/djangor/__init__.pyc b/assignments/week06/athome/djangoenv/bin/newsite/djangor/__init__.pyc new file mode 100644 index 00000000..497367d2 Binary files /dev/null and b/assignments/week06/athome/djangoenv/bin/newsite/djangor/__init__.pyc differ diff --git a/assignments/week06/athome/djangoenv/bin/newsite/djangor/admin.py b/assignments/week06/athome/djangoenv/bin/newsite/djangor/admin.py new file mode 100644 index 00000000..e34eb49e --- /dev/null +++ b/assignments/week06/athome/djangoenv/bin/newsite/djangor/admin.py @@ -0,0 +1,11 @@ +from django.contrib import admin +from djangor.models import Post + + +class PostAdmin(admin.ModelAdmin): + list_display = ('pub_date', 'title', + 'published_today') + list_filter = ('pub_date', ) + ordering = ('pub_date', ) + +admin.site.register(Post, PostAdmin) diff --git a/assignments/week06/athome/djangoenv/bin/newsite/djangor/admin.pyc b/assignments/week06/athome/djangoenv/bin/newsite/djangor/admin.pyc new file mode 100644 index 00000000..0f600adc Binary files /dev/null and b/assignments/week06/athome/djangoenv/bin/newsite/djangor/admin.pyc differ diff --git a/assignments/week06/athome/djangoenv/bin/newsite/djangor/models.py b/assignments/week06/athome/djangoenv/bin/newsite/djangor/models.py new file mode 100644 index 00000000..c88695a5 --- /dev/null +++ b/assignments/week06/athome/djangoenv/bin/newsite/djangor/models.py @@ -0,0 +1,21 @@ +from django.db import models +from django.utils import timezone +from django.contrib.auth.models import User + +class Post(models.Model): + title = models.CharField(max_length=200) + body = models.TextField() + created = models.DateTimeField(auto_now_add=True) + pub_date = models.DateTimeField('date published') + owner = models.ForeignKey(User) + + def __unicode__(self): + return self.title + + def published_today(self): + now = timezone.now() + time_delta = now - self.pub_date + return time_delta.days == 0 + + published_today.boolean = True + published_today.short_description = "Published Today?" diff --git a/assignments/week06/athome/djangoenv/bin/newsite/djangor/models.pyc b/assignments/week06/athome/djangoenv/bin/newsite/djangor/models.pyc new file mode 100644 index 00000000..a419088f Binary files /dev/null and b/assignments/week06/athome/djangoenv/bin/newsite/djangor/models.pyc differ diff --git a/assignments/week06/athome/djangoenv/bin/newsite/djangor/static/style.css b/assignments/week06/athome/djangoenv/bin/newsite/djangor/static/style.css new file mode 100644 index 00000000..f53d77f7 --- /dev/null +++ b/assignments/week06/athome/djangoenv/bin/newsite/djangor/static/style.css @@ -0,0 +1,17 @@ +body { font-family: sans-serif; background: #eee; } +a, h1, h2 { color: #377BA8; } +h1, h2 { font-family: 'Georgia', serif; margin: 0; } +h1 { border-bottom: 2px solid #eee; } +h2 { font-size: 1.2em; } +.page { margin: 2em auto; width: 35em; border: 5px solid #ccc; + padding: 0.8em; background: white; } +.entries { list-style: none; margin: 0; padding: 0; } +.entries li { margin: 0.8em 1.2em; } +.entries li h2 { margin-left: -1em; } +.add-entry { font-size: 0.9em; border-bottom: 1px solid #ccc; } +.add-entry dl { font-weight: bold; } +.metanav { text-align: right; font-size: 0.8em; padding: 0.3em; + margin-bottom: 1em; background: #fafafa; } +.flash { background: #CEE5F5; padding: 0.5em; + border: 1px solid #AACBE2; } +.error { background: #F0D6D6; padding: 0.5em; } \ No newline at end of file diff --git a/assignments/week06/athome/djangoenv/bin/newsite/djangor/templates/base.html b/assignments/week06/athome/djangoenv/bin/newsite/djangor/templates/base.html new file mode 100644 index 00000000..b49a18d0 --- /dev/null +++ b/assignments/week06/athome/djangoenv/bin/newsite/djangor/templates/base.html @@ -0,0 +1,20 @@ + + + + New Site + + +
+
+ {% if messages %} + {% for message in messages %} +

+ {{ message }}

+ {% endfor %} + {% endif %} + {% block content %} + {% endblock %} +
+
+ + diff --git a/assignments/week06/athome/djangoenv/bin/newsite/djangor/templates/blog/bbase.html b/assignments/week06/athome/djangoenv/bin/newsite/djangor/templates/blog/bbase.html new file mode 100644 index 00000000..1005f6ce --- /dev/null +++ b/assignments/week06/athome/djangoenv/bin/newsite/djangor/templates/blog/bbase.html @@ -0,0 +1,29 @@ + + + + {% block title %}Djangor{% endblock %} + + + +
+ + +
+ {% block content %}{% endblock %} +
+
+ + + diff --git a/assignments/week06/athome/djangoenv/bin/newsite/djangor/templates/blog/list.html b/assignments/week06/athome/djangoenv/bin/newsite/djangor/templates/blog/list.html new file mode 100644 index 00000000..a5adab50 --- /dev/null +++ b/assignments/week06/athome/djangoenv/bin/newsite/djangor/templates/blog/list.html @@ -0,0 +1,38 @@ +{% extends "bbase.html" %} + +{% block content %} +
+ + +
    + {% for post in posts.object_list %} +
    {{ post.title }}
    +
      +
      {{ post.created }}
      +
      {{ post.body|linebreaks }}
      +
    + {% endfor %} +
+ + + {% if posts.object_list and posts.paginator.num_pages > 1 %} + + {% endif %} + +
+ +{% endblock %} diff --git a/assignments/week06/athome/djangoenv/bin/newsite/djangor/templates/flaskr/layout.html b/assignments/week06/athome/djangoenv/bin/newsite/djangor/templates/flaskr/layout.html new file mode 100644 index 00000000..4b595312 --- /dev/null +++ b/assignments/week06/athome/djangoenv/bin/newsite/djangor/templates/flaskr/layout.html @@ -0,0 +1,22 @@ + + + + Flaskr + + + +

Flaskr

+ {% if not session.logged_in %} + log in + {% else %} + log_out + {% endif %} + + {% for message in get_flashed_messages() %} +
{{ message }}
+ {% endfor %} +
+ {% block body %}{% endblock %} +
+ + \ No newline at end of file diff --git a/assignments/week06/athome/djangoenv/bin/newsite/djangor/templates/flaskr/login.html b/assignments/week06/athome/djangoenv/bin/newsite/djangor/templates/flaskr/login.html new file mode 100644 index 00000000..ed4554db --- /dev/null +++ b/assignments/week06/athome/djangoenv/bin/newsite/djangor/templates/flaskr/login.html @@ -0,0 +1,20 @@ +{% extends "layout.html" %} +{% block body %} +

Login

+ {% if error -%} +

Error {{ error }} + {%- endif %} +

+
+ + +
+
+ + +
+
+ +
+
+{% endblock %} diff --git a/assignments/week06/athome/djangoenv/bin/newsite/djangor/templates/flaskr/show_entries.html b/assignments/week06/athome/djangoenv/bin/newsite/djangor/templates/flaskr/show_entries.html new file mode 100644 index 00000000..f44fd92b --- /dev/null +++ b/assignments/week06/athome/djangoenv/bin/newsite/djangor/templates/flaskr/show_entries.html @@ -0,0 +1,31 @@ +{% extends "layout.html" %} +{% block body %} + {% if session.logged_in %} +
+
+ + +
+
+ + +
+
+ +
+
+ {% endif %} +

Posts

+
    + {% for entry in entries %} +
  • +

    {{ entry.title }}

    +
    + {{ entry.text|safe }} +
    +
  • + {% else %} +
  • No entries here so far
  • + {% endfor %} +
+{% endblock %} \ No newline at end of file diff --git a/assignments/week06/athome/djangoenv/bin/newsite/djangor/templates/polls/detail.html b/assignments/week06/athome/djangoenv/bin/newsite/djangor/templates/polls/detail.html new file mode 100644 index 00000000..a842f41c --- /dev/null +++ b/assignments/week06/athome/djangoenv/bin/newsite/djangor/templates/polls/detail.html @@ -0,0 +1,21 @@ +{% extends "base.html" %} + +{% block content %} +

{{ poll }}

+{% if poll.choice_set.count > 0 %} +
+ {% csrf_token %} + {% for choice in poll.choice_set.all %} +
+ +
+ {% endfor %} + +
+{% else %} +

No choices are available for this poll

+{% endif %} +{% endblock %} \ No newline at end of file diff --git a/assignments/week06/athome/djangoenv/bin/newsite/djangor/templates/polls/list.html b/assignments/week06/athome/djangoenv/bin/newsite/djangor/templates/polls/list.html new file mode 100644 index 00000000..3a00ee5d --- /dev/null +++ b/assignments/week06/athome/djangoenv/bin/newsite/djangor/templates/polls/list.html @@ -0,0 +1,11 @@ +{% extends "base.html" %} + +{% block content %} +

Latest Polls

+{% for poll in polls %} +
+

{{ poll }}

+

Total Votes: {{ classlab.total_votes }}

+
+{% endfor %} +{% endblock %} diff --git a/assignments/week06/athome/djangoenv/bin/newsite/djangor/templates/polls/result.html b/assignments/week06/athome/djangoenv/bin/newsite/djangor/templates/polls/result.html new file mode 100644 index 00000000..8937985a --- /dev/null +++ b/assignments/week06/athome/djangoenv/bin/newsite/djangor/templates/polls/result.html @@ -0,0 +1,11 @@ +{% extends "base.html" %} + +{% block content %} +

{{ poll }}

+
    + {% for choice in poll.choice_set.all %} +
  • {{ choice }} ({{choice.votes}} votes)
  • + {% endfor %} +
+Back to the polls, please +{% endblock %} \ No newline at end of file diff --git a/assignments/week06/athome/djangoenv/bin/newsite/djangor/tests.py b/assignments/week06/athome/djangoenv/bin/newsite/djangor/tests.py new file mode 100644 index 00000000..501deb77 --- /dev/null +++ b/assignments/week06/athome/djangoenv/bin/newsite/djangor/tests.py @@ -0,0 +1,16 @@ +""" +This file demonstrates writing tests using the unittest module. These will pass +when you run "manage.py test". + +Replace this with more appropriate tests for your application. +""" + +from django.test import TestCase + + +class SimpleTest(TestCase): + def test_basic_addition(self): + """ + Tests that 1 + 1 always equals 2. + """ + self.assertEqual(1 + 1, 2) diff --git a/assignments/week06/athome/djangoenv/bin/newsite/djangor/urls.py b/assignments/week06/athome/djangoenv/bin/newsite/djangor/urls.py new file mode 100644 index 00000000..b15cf7e9 --- /dev/null +++ b/assignments/week06/athome/djangoenv/bin/newsite/djangor/urls.py @@ -0,0 +1,11 @@ +from django.conf.urls import patterns, url +from django.http import HttpResponse + +from classlab.models import Poll + +def stub(request, *args, **kwargs): + return HttpResponse('stub view', mimetype="text/plain") + +urlpatterns = patterns('djangor.views', + (r"", "main"), +) diff --git a/assignments/week06/athome/djangoenv/bin/newsite/djangor/urls.pyc b/assignments/week06/athome/djangoenv/bin/newsite/djangor/urls.pyc new file mode 100644 index 00000000..62b73087 Binary files /dev/null and b/assignments/week06/athome/djangoenv/bin/newsite/djangor/urls.pyc differ diff --git a/assignments/week06/athome/djangoenv/bin/newsite/djangor/views.py b/assignments/week06/athome/djangoenv/bin/newsite/djangor/views.py new file mode 100644 index 00000000..ec1c4a5a --- /dev/null +++ b/assignments/week06/athome/djangoenv/bin/newsite/djangor/views.py @@ -0,0 +1,19 @@ +from django.core.paginator import Paginator, InvalidPage, EmptyPage +from django.core.urlresolvers import reverse + +from djangor.models import * + +def main(request): + """Main listing.""" + posts = Post.objects.all().order_by("-created") + paginator = Paginator(posts, 2) + + try: page = int(request.GET.get("page", '1')) + except ValueError: page = 1 + + try: + posts = paginator.page(page) + except (InvalidPage, EmptyPage): + posts = paginator.page(paginator.num_pages) + + return render_to_response("list.html", dict(posts=posts, user=request.user)) diff --git a/assignments/week06/athome/djangoenv/bin/newsite/djangor/views.pyc b/assignments/week06/athome/djangoenv/bin/newsite/djangor/views.pyc new file mode 100644 index 00000000..7534fdc9 Binary files /dev/null and b/assignments/week06/athome/djangoenv/bin/newsite/djangor/views.pyc differ diff --git a/assignments/week06/athome/djangoenv/bin/newsite/manage.py b/assignments/week06/athome/djangoenv/bin/newsite/manage.py new file mode 100644 index 00000000..cff4e28e --- /dev/null +++ b/assignments/week06/athome/djangoenv/bin/newsite/manage.py @@ -0,0 +1,10 @@ +#!/usr/bin/env python +import os +import sys + +if __name__ == "__main__": + os.environ.setdefault("DJANGO_SETTINGS_MODULE", "newsite.settings") + + from django.core.management import execute_from_command_line + + execute_from_command_line(sys.argv) diff --git a/assignments/week06/athome/djangoenv/bin/newsite/newsite/__init__.py b/assignments/week06/athome/djangoenv/bin/newsite/newsite/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/assignments/week06/athome/djangoenv/bin/newsite/newsite/__init__.pyc b/assignments/week06/athome/djangoenv/bin/newsite/newsite/__init__.pyc new file mode 100644 index 00000000..b7bf324a Binary files /dev/null and b/assignments/week06/athome/djangoenv/bin/newsite/newsite/__init__.pyc differ diff --git a/assignments/week06/athome/djangoenv/bin/newsite/newsite/settings.py b/assignments/week06/athome/djangoenv/bin/newsite/newsite/settings.py new file mode 100644 index 00000000..424d7293 --- /dev/null +++ b/assignments/week06/athome/djangoenv/bin/newsite/newsite/settings.py @@ -0,0 +1,153 @@ +# Django settings for newsite project. + +DEBUG = True +TEMPLATE_DEBUG = DEBUG + +ADMINS = ( + # ('Your Name', 'your_email@example.com'), +) + +MANAGERS = ADMINS + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'. + 'NAME': 'djangor_db', # Or path to database file if using sqlite3. + 'USER': 'root', # Not used with sqlite3. + 'PASSWORD': 'django', # Not used with sqlite3. + 'HOST': '', # Set to empty string for localhost. Not used with sqlite3. + 'PORT': '', # Set to empty string for default. Not used with sqlite3. + } +} + +# Local time zone for this installation. Choices can be found here: +# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name +# although not all choices may be available on all operating systems. +# In a Windows environment this must be set to your system time zone. +TIME_ZONE = 'America/Chicago' + +# Language code for this installation. All choices can be found here: +# http://www.i18nguy.com/unicode/language-identifiers.html +LANGUAGE_CODE = 'en-us' + +SITE_ID = 1 + +# If you set this to False, Django will make some optimizations so as not +# to load the internationalization machinery. +USE_I18N = True + +# If you set this to False, Django will not format dates, numbers and +# calendars according to the current locale. +USE_L10N = True + +# If you set this to False, Django will not use timezone-aware datetimes. +USE_TZ = True + +# Absolute filesystem path to the directory that will hold user-uploaded files. +# Example: "/home/media/media.lawrence.com/media/" +MEDIA_ROOT = '' + +# URL that handles the media served from MEDIA_ROOT. Make sure to use a +# trailing slash. +# Examples: "http://media.lawrence.com/media/", "http://example.com/media/" +MEDIA_URL = '' + +# Absolute path to the directory static files should be collected to. +# Don't put anything in this directory yourself; store your static files +# in apps' "static/" subdirectories and in STATICFILES_DIRS. +# Example: "/home/media/media.lawrence.com/static/" +STATIC_ROOT = '' + +# URL prefix for static files. +# Example: "http://media.lawrence.com/static/" +STATIC_URL = '/static/' + +# Additional locations of static files +STATICFILES_DIRS = ( + # Put strings here, like "/home/html/static" or "C:/www/django/static". + # Always use forward slashes, even on Windows. + # Don't forget to use absolute paths, not relative paths. +) + +# List of finder classes that know how to find static files in +# various locations. +STATICFILES_FINDERS = ( + 'django.contrib.staticfiles.finders.FileSystemFinder', + 'django.contrib.staticfiles.finders.AppDirectoriesFinder', +# 'django.contrib.staticfiles.finders.DefaultStorageFinder', +) + +# Make this unique, and don't share it with anybody. +SECRET_KEY = '@p973=vyp)^!*&iq(y1xk!+ni^0csryp6^6-lc!a$%iv_b-lhr' + +# List of callables that know how to import templates from various sources. +TEMPLATE_LOADERS = ( + 'django.template.loaders.filesystem.Loader', + 'django.template.loaders.app_directories.Loader', +# 'django.template.loaders.eggs.Loader', +) + +MIDDLEWARE_CLASSES = ( + 'django.middleware.common.CommonMiddleware', + 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.middleware.csrf.CsrfViewMiddleware', + 'django.contrib.auth.middleware.AuthenticationMiddleware', + 'django.contrib.messages.middleware.MessageMiddleware', + # Uncomment the next line for simple clickjacking protection: + # 'django.middleware.clickjacking.XFrameOptionsMiddleware', +) + +ROOT_URLCONF = 'newsite.urls' + +# Python dotted path to the WSGI application used by Django's runserver. +WSGI_APPLICATION = 'newsite.wsgi.application' + +TEMPLATE_DIRS = ( + # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates". + # Always use forward slashes, even on Windows. + # Don't forget to use absolute paths, not relative paths. +) + +INSTALLED_APPS = ( + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.sites', + 'django.contrib.messages', + 'django.contrib.staticfiles', + # Uncomment the next line to enable the admin: + 'django.contrib.admin', + # Uncomment the next line to enable admin documentation: + # 'django.contrib.admindocs', + 'classlab', + 'djangor', +) + +# A sample logging configuration. The only tangible logging +# performed by this configuration is to send an email to +# the site admins on every HTTP 500 error when DEBUG=False. +# See http://docs.djangoproject.com/en/dev/topics/logging for +# more details on how to customize your logging configuration. +LOGGING = { + 'version': 1, + 'disable_existing_loggers': False, + 'filters': { + 'require_debug_false': { + '()': 'django.utils.log.RequireDebugFalse' + } + }, + 'handlers': { + 'mail_admins': { + 'level': 'ERROR', + 'filters': ['require_debug_false'], + 'class': 'django.utils.log.AdminEmailHandler' + } + }, + 'loggers': { + 'django.request': { + 'handlers': ['mail_admins'], + 'level': 'ERROR', + 'propagate': True, + }, + } +} diff --git a/assignments/week06/athome/djangoenv/bin/newsite/newsite/settings.pyc b/assignments/week06/athome/djangoenv/bin/newsite/newsite/settings.pyc new file mode 100644 index 00000000..e4c18f48 Binary files /dev/null and b/assignments/week06/athome/djangoenv/bin/newsite/newsite/settings.pyc differ diff --git a/assignments/week06/athome/djangoenv/bin/newsite/newsite/urls.py b/assignments/week06/athome/djangoenv/bin/newsite/newsite/urls.py new file mode 100644 index 00000000..5ea50747 --- /dev/null +++ b/assignments/week06/athome/djangoenv/bin/newsite/newsite/urls.py @@ -0,0 +1,19 @@ +from django.conf.urls import patterns, include, url + +# Uncomment the next two lines to enable the admin: +from django.contrib import admin +admin.autodiscover() + +urlpatterns = patterns('', + # Examples: + # url(r'^$', 'newsite.views.home', name='home'), + # url(r'^newsite/', include('newsite.foo.urls')), + + # Uncomment the admin/doc line below to enable admin documentation: + # url(r'^admin/doc/', include('django.contrib.admindocs.urls')), + + # Uncomment the next line to enable the admin: + url(r'^admin/', include(admin.site.urls)), + url(r'^classlab/', include('classlab.urls')), + url(r'^djangor/', include('djangor.urls')), +) diff --git a/assignments/week06/athome/djangoenv/bin/newsite/newsite/urls.pyc b/assignments/week06/athome/djangoenv/bin/newsite/newsite/urls.pyc new file mode 100644 index 00000000..33b3a13d Binary files /dev/null and b/assignments/week06/athome/djangoenv/bin/newsite/newsite/urls.pyc differ diff --git a/assignments/week06/athome/djangoenv/bin/newsite/newsite/wsgi.py b/assignments/week06/athome/djangoenv/bin/newsite/newsite/wsgi.py new file mode 100644 index 00000000..3303022f --- /dev/null +++ b/assignments/week06/athome/djangoenv/bin/newsite/newsite/wsgi.py @@ -0,0 +1,28 @@ +""" +WSGI config for newsite project. + +This module contains the WSGI application used by Django's development server +and any production WSGI deployments. It should expose a module-level variable +named ``application``. Django's ``runserver`` and ``runfcgi`` commands discover +this application via the ``WSGI_APPLICATION`` setting. + +Usually you will have the standard Django WSGI application here, but it also +might make sense to replace the whole Django WSGI application with a custom one +that later delegates to the Django one. For example, you could introduce WSGI +middleware here, or combine a Django application with an application of another +framework. + +""" +import os + +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "newsite.settings") + +# This application object is used by any WSGI server configured to use this +# file. This includes Django's development server, if the WSGI_APPLICATION +# setting points here. +from django.core.wsgi import get_wsgi_application +application = get_wsgi_application() + +# Apply WSGI middleware here. +# from helloworld.wsgi import HelloWorldApplication +# application = HelloWorldApplication(application) diff --git a/assignments/week06/athome/djangoenv/bin/newsite/newsite/wsgi.pyc b/assignments/week06/athome/djangoenv/bin/newsite/newsite/wsgi.pyc new file mode 100644 index 00000000..4e3657e6 Binary files /dev/null and b/assignments/week06/athome/djangoenv/bin/newsite/newsite/wsgi.pyc differ