Anonymous avatar Anonymous committed 4caf572

add user login and details for poll

Comments (0)

Files changed (2)

gaepuv/puv/poll/views.py

 from datetime import datetime
 # from google.appengine.api import memcache
 from django.views.decorators.cache import cache_page
+from google.appengine.api import users
 
 APP_ID = os.environ['APPLICATION_ID'] 
 VERSION = os.environ.get('CURRENT_VERSION_ID','0') 
 def create(request):
     """ Create a poll form """
     country, initial = start_location(request)
+    user = users.get_current_user()
+    if user:
+        instance = models.Poll(created_by = user.nickname(),
+                               email = user.email(),
+                               user = user,
+                               anonymous = False)
+        login = None
+        logout = ("Welcome, %s! (<a href=\"%s\">sign out</a>)" %
+                   (user.nickname(), users.create_logout_url("/")))
+
+    else:
+        login = ("<a href=\"%s\">Sign in or register</a>." %
+                    users.create_login_url("/create/"))
+        logout = None
+        instance = None
     try:
         startlat = str(initial['location'].lat)
         startlng = str(initial['location'].lon)
     if request.method == 'GET':
         locations = NUM_LOCATIONS
         dates = NUM_DATES
-        pollform = PollForm()
+        pollform = PollForm(instance=instance)
         for i in range(locations):
             choiceforms.append(ChoiceForm(prefix = 'f%s'%i, initial = initial))
     # Write posted poll
              'choiceforms' : choiceforms,
              'locations' : locations,
              'dates' : dates,
-             'genjs' : js
+             'genjs' : js,
+             'login' : login,
+             'logout' : logout
               }
     return render_to_response('create.html', props)
 

gaepuv/puv/templates/create.html

 {% if pollform.errors %}
 <div class="error">{{ pollform.errors }}</div>
 {% endif %}
+
 <fieldset>
+<legend>Your details</legend>
+{% if login %} 
+<p>Please {{ login|safe }} or fill in your name and email<br />
+(Note: that registered users can retain a set of polls they have created 
+and re-edit or reuse them)
+</p>
+{% endif %}
+
 <label id="id_created_by_label">Your name:</label>
 <span class="error" id="id_created_by_error"></span>
 {{ pollform.created_by }}<br />
 <label id="id_email_label">Email:</label>
 <span class="error" id="id_email_error"></span>
 {{ pollform.email }}<br />
+</fieldset>
+<br />
+
+<fieldset>
+<legend>Poll</legend>
 <label id="id_question_label">Question:</label><br />
 <span class="error" id="id_question_error"></span>
 {{ pollform.question }}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.