From 875f3feb01390b2bc5d3f9c21e920d6ed7522994 Mon Sep 17 00:00:00 2001 From: Michelle Yu Date: Tue, 19 Jan 2016 01:15:58 -0800 Subject: [PATCH 1/4] homework 2 complete --- resources/session02/homework/http_server.py | 27 +++++++++++++++++---- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/resources/session02/homework/http_server.py b/resources/session02/homework/http_server.py index 84ceffe1..51eef087 100644 --- a/resources/session02/homework/http_server.py +++ b/resources/session02/homework/http_server.py @@ -1,14 +1,16 @@ import socket import sys +import os +import mimetypes def response_ok(body=b"this is a pretty minimal response", mimetype=b"text/plain"): """returns a basic HTTP response""" resp = [] resp.append(b"HTTP/1.1 200 OK") - resp.append(b"Content-Type: text/plain") + resp.append(b"Content-Type: " + mimetype) resp.append(b"") - resp.append(b"this is a pretty minimal response") + resp.append(body) return b"\r\n".join(resp) @@ -22,7 +24,10 @@ def response_method_not_allowed(): def response_not_found(): """returns a 404 Not Found response""" - return b"" + resp = [] + resp.append("HTTP/1.1 404 Not Found") + resp.append("") + return "\r\n".join(resp).encode('utf8') def parse_request(request): @@ -35,7 +40,19 @@ def parse_request(request): def resolve_uri(uri): """This method should return appropriate content and a mime type""" - return b"still broken", b"text/plain" + home = os.getcwd() + "/webroot" + path = home + uri + if os.path.isfile(path): + mime, encoding = mimetypes.guess_type(path) + with open(path, 'rb') as f: + content = f.read() + elif os.path.isdir(path): + mime = "text/plain" + files = os.listdir(path) + content = "\n".join(files).encode('utf8') + else: + raise NameError + return content, mime.encode('utf8') def server(log_buffer=sys.stderr): @@ -43,7 +60,7 @@ def server(log_buffer=sys.stderr): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) print("making a server on {0}:{1}".format(*address), file=log_buffer) - sock.bind(address) + sock.bind(address) sock.listen(1) try: From 09b32fafbc37a5c618d6846e0e50e91cde92dd9c Mon Sep 17 00:00:00 2001 From: Michelle Yu Date: Mon, 25 Jan 2016 18:56:45 -0800 Subject: [PATCH 2/4] assignment 3 complete --- resources/session03/wsgi/calculator.py | 66 ++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 resources/session03/wsgi/calculator.py diff --git a/resources/session03/wsgi/calculator.py b/resources/session03/wsgi/calculator.py new file mode 100644 index 00000000..a43af7ed --- /dev/null +++ b/resources/session03/wsgi/calculator.py @@ -0,0 +1,66 @@ +import re + + +def resolve_path(path): + urls = [(r'^$', welcome), + (r'^add/(\d+)/(\d+)$', add), + (r'^subtract/(\d+)/(\d+)$', subtract), + (r'^divide/(\d+)/(\d+)$', divide), + (r'^multiply/(\d+)/(\d+)$', multiply)] + + matchpath = path.lstrip('/') + for regexp, func in urls: + match = re.match(regexp, matchpath) + if match is None: + continue + args = match.groups([]) + return func, args + # we get here if no url matches + raise NameError + +def welcome(): + return "

welcome

" + +def multiply(v1, v2): + r = int(v1) * int(v2) + return "

result from multiplication: %d

" %r + +def add(v1, v2): + r = int(v1) + int(v2) + return "

result from addition: %d

" % r + +def subtract(v1, v2): + r = int(v1) - int(v2) + return "

result from subtraction: %d

" % r + +def divide(v1, v2): + r = int(v1) / int(v2) + return "

result from division: %d

" % r + +def application(environ, start_response): + headers = [("Content-type", "text/html")] + try: + path = environ.get('PATH_INFO', None) + if path is None: + raise NameError + func, args = resolve_path(path) + body = func(*args) + status = "200 OK" + except NameError: + status = "404 Not Found" + body = "

Not Found

" + except ZeroDivisionError: + status = "400 Bad Request" + body = "

Bad Request

" + except Exception: + status = "500 Internal Server Error" + body = "

Internal Server Error

" + finally: + headers.append(('Content-length', str(len(body)))) + start_response(status, headers) + return [body.encode('utf8')] + +if __name__ == '__main__': + from wsgiref.simple_server import make_server + srv = make_server('localhost', 8080, application) + srv.serve_forever() From a4d1505da101ca4cc5164d6066e7afc9025c032e Mon Sep 17 00:00:00 2001 From: Michelle Yu Date: Tue, 1 Mar 2016 00:55:09 -0800 Subject: [PATCH 3/4] hw8 complete --- .../session08/mysite_stage_3/myblog/admin.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/resources/session08/mysite_stage_3/myblog/admin.py b/resources/session08/mysite_stage_3/myblog/admin.py index 310e7294..73ae535c 100644 --- a/resources/session08/mysite_stage_3/myblog/admin.py +++ b/resources/session08/mysite_stage_3/myblog/admin.py @@ -4,5 +4,17 @@ from myblog.models import Post -admin.site.register(Category) -admin.site.register(Post) +class CategoryInline(admin.TabularInline): + model = Category.posts.through + +class PostAdmin(admin.ModelAdmin): + inlines = [ + CategoryInline, + ] + +class CategoryAdmin(admin.ModelAdmin): + exclude = ('posts', ) + + +admin.site.register(Category, CategoryAdmin) +admin.site.register(Post, PostAdmin) From d05697a22120a3685be24f4be42dc4195f59b0df Mon Sep 17 00:00:00 2001 From: Michelle Yu Date: Tue, 1 Mar 2016 19:30:50 -0800 Subject: [PATCH 4/4] added registration form --- .../templates/registration/registration_form.html | 10 ++++++++++ resources/session08/mysite_stage_3/myblog/urls.py | 3 ++- 2 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 resources/session08/mysite_stage_3/myblog/templates/registration/registration_form.html diff --git a/resources/session08/mysite_stage_3/myblog/templates/registration/registration_form.html b/resources/session08/mysite_stage_3/myblog/templates/registration/registration_form.html new file mode 100644 index 00000000..d0483093 --- /dev/null +++ b/resources/session08/mysite_stage_3/myblog/templates/registration/registration_form.html @@ -0,0 +1,10 @@ +{% extends "base.html" %} + +{% block content %} +

My Blog Registration form

+
{% csrf_token %} + {{ form.as_p }} +

+
+{% endblock %} + diff --git a/resources/session08/mysite_stage_3/myblog/urls.py b/resources/session08/mysite_stage_3/myblog/urls.py index 5caacf17..be4bf801 100644 --- a/resources/session08/mysite_stage_3/myblog/urls.py +++ b/resources/session08/mysite_stage_3/myblog/urls.py @@ -1,4 +1,4 @@ -from django.conf.urls import url +from django.conf.urls import include, url from myblog.views import stub_view from myblog.views import list_view @@ -12,4 +12,5 @@ url(r'^posts/(?P\d+)/$', detail_view, name='blog_detail'), + url(r'^accounts/', include('registration.backends.simple.urls')), ]