From 12f9d87b34d355d5780df8abccb5c68a08de72b2 Mon Sep 17 00:00:00 2001 From: Nitish Date: Tue, 16 Oct 2012 01:37:20 +0530 Subject: [PATCH 01/15] project addition and viewing --- uplusprofiles/app.yaml | 2 +- uplusprofiles/controllers/profile.py | 45 ++++++++--- uplusprofiles/models/Project.py | 15 ++++ uplusprofiles/models/User.py | 3 +- .../templates/profile/add_project.html | 79 +++++++++++++++++++ .../templates/profile/edit_project.html | 79 +++++++++++++++++++ uplusprofiles/templates/profile/profile.html | 67 ++++++++-------- 7 files changed, 244 insertions(+), 46 deletions(-) create mode 100644 uplusprofiles/models/Project.py create mode 100644 uplusprofiles/templates/profile/add_project.html create mode 100644 uplusprofiles/templates/profile/edit_project.html diff --git a/uplusprofiles/app.yaml b/uplusprofiles/app.yaml index 278edf1..713b99a 100644 --- a/uplusprofiles/app.yaml +++ b/uplusprofiles/app.yaml @@ -1,4 +1,4 @@ -application: uplusprofiles +application: devn-uplusprofiles version: 1 runtime: python27 api_version: 1 diff --git a/uplusprofiles/controllers/profile.py b/uplusprofiles/controllers/profile.py index 1c8ca0f..5efa4c3 100644 --- a/uplusprofiles/controllers/profile.py +++ b/uplusprofiles/controllers/profile.py @@ -2,7 +2,9 @@ from jinja_custom.helpers import get_gravatar from models.Course import Course from models.User import User +from models.Project import Project from datetime import date, timedelta +import logging class ProfilePage(BaseHandler): @@ -10,17 +12,17 @@ class ProfilePage(BaseHandler): def get(self, username): """display profile of user with username, if None, display logged in user """ - mode, = self.get_params(['mode']) + mode = self.request.get('mode') - - if mode != 'edit': - template = 'profile/profile.html' + if mode == 'add_project': + template = 'profile/add_project.html' else: - template = 'profile/edit.html' + template = 'profile/profile.html' + user = User.get_user(username) if not user: - user = User.save(username, 'some long password', '{}@someplace.com'.format(username)) + user = User.save(username, '{}@someplace.com'.format(username), 'some long password') dob = user.created - 13 * timedelta(days=365) @@ -30,14 +32,20 @@ def get(self, username): courses = Course.query() - if user: + project_ids = user.projects + projects = [] + if project_ids: + for project_id in project_ids: + projects.append(Project.get_by_id(project_id)) + if user: context = {'user': user, 'dob': dob, 'username': username, 'gravatar': gravatar, 'friends': friends, 'friend_btn': False, - 'courses': courses} + 'courses': courses, + 'projects': projects} self.render(template, context) else: @@ -45,5 +53,24 @@ def get(self, username): #@Authentication.do def post(self, username): + mode = self.request.get('mode') + if mode == 'add_project': + title = self.request.get('title') + screenshot = self.request.get('screenshot') + url = self.request.get('url') + description = self.request.get('description') + # TODO: validation - pass + project_id = Project.add_project(title=title, screenshot=screenshot, + url=url, description=description) + user = User.get_user(username) + projects = user.projects + if projects: + projects.append(project_id) + else: + projects = [project_id] + user.projects = projects + user.put() + else: + pass + self.redirect('/'+username) diff --git a/uplusprofiles/models/Project.py b/uplusprofiles/models/Project.py new file mode 100644 index 0000000..4bb0e74 --- /dev/null +++ b/uplusprofiles/models/Project.py @@ -0,0 +1,15 @@ +#from google.appengine.ext.ndb import Key +from google.appengine.ext import ndb + + +class Project(ndb.Model): + title = ndb.StringProperty() + screenshot = ndb.StringProperty() + url = ndb.StringProperty() + description = ndb.TextProperty() + + @classmethod + def add_project(cls, **kwargs): + p = Project(**kwargs) + p_key = p.put() + return p_key.id() \ No newline at end of file diff --git a/uplusprofiles/models/User.py b/uplusprofiles/models/User.py index 9254a6e..18468aa 100644 --- a/uplusprofiles/models/User.py +++ b/uplusprofiles/models/User.py @@ -22,7 +22,7 @@ # bcrypt.gensalt() is used for creating tokens # tokens in database are hashed with bcrypt # new token is generated with every new login -from appengine.ext.ndb import QueryOptions +from google.appengine.ext.ndb import QueryOptions from google.appengine.ext import ndb from google.appengine.ext.ndb.key import Key @@ -59,6 +59,7 @@ class User(ndb.Model): profile_link = ndb.StructuredProperty(Details.ExternalProfileLink, repeated=True) location = ndb.StructuredProperty(Details.Location) + projects = ndb.JsonProperty() # TODO: upload to a static directory? avatar = ndb.BlobProperty() avatar_url = ndb.StringProperty(default="/img/defaultavatar.png") diff --git a/uplusprofiles/templates/profile/add_project.html b/uplusprofiles/templates/profile/add_project.html new file mode 100644 index 0000000..1ac1dbe --- /dev/null +++ b/uplusprofiles/templates/profile/add_project.html @@ -0,0 +1,79 @@ +{% extends "base.html" %} +{% block stylesheets %} + {{ super() }} + + +{% endblock stylesheets %} +{% block usercontent %} +
+
+
+ Add Project +
+ +
+ +
+ +

+
+
+ + + +
+ +
+ +

+
+
+ +
+ +
+ +

+
+
+ +
+ +
+
+ +
+
+
+ + +
+ + + +
+ +
+
+ +
+
+ + +{% endblock usercontent%} +{% block js %} + {{ super() }} + + + +{% endblock %} \ No newline at end of file diff --git a/uplusprofiles/templates/profile/edit_project.html b/uplusprofiles/templates/profile/edit_project.html new file mode 100644 index 0000000..2c6abe6 --- /dev/null +++ b/uplusprofiles/templates/profile/edit_project.html @@ -0,0 +1,79 @@ +{% extends "base.html" %} +{% block stylesheets %} + {{ super() }} + + +{% endblock stylesheets %} +{% block usercontent %} +
+
+
+ Add Project +
+ +
+ +
+ +

+
+
+ +
+ +
+ +

+
+
+ +
+ +
+ +

+
+
+ +
+ +
+ +

+
+
+ +
+ +
+
+ +
+
+
+ + +
+ + + +
+ +
+
+ +
+
+ + +{% endblock usercontent%} +{% block js %} + {{ super() }} + + + +{% endblock %} \ No newline at end of file diff --git a/uplusprofiles/templates/profile/profile.html b/uplusprofiles/templates/profile/profile.html index 26f57aa..3bf52ec 100644 --- a/uplusprofiles/templates/profile/profile.html +++ b/uplusprofiles/templates/profile/profile.html @@ -46,49 +46,46 @@

User

+

User

Hi, I like programming and learning new things on Udacity.

- -

Here is a selection of projects that I'm working on.

-
- -
-