Commits

Ivan Melnikov committed 71acb1c Draft

Approve users

Comments (0)

Files changed (4)

template/static/js/user.js

+function approveUser(user_id) {
+	$.ajax({ 
+        type: 'POST', 
+        url: '/.user/all', 
+        data: 'user_id=' + user_id,
+        cache: false, 
+        success: function(message) 
+        { 
+            console.log('User '+user_id+' approved');
+            $('#'+user_id).remove()
+        } 
+    }); 
+}

template/user/index.html

 <!DOCTYPE html>
 <html>
+<head>
+	<script type="text/javascript" charset="utf-8" src="/_static/js/user.js"></script>
+	<script src="/_static/js/jquery.js"></script>
+</head>
 
 <body>
 

template/user/list.html

 <h1>Пользователи</h1>
 
 {% for user in users %}
-<div>
+<div id="{{ user.key().id() }}">
 {{ user.email }}
+<a href="#" onclick="approveUser( {{ user.key().id() }} )">Подтвердить</a>
 </div>
 
 {% endfor %}
 
 
 {# add current user to list #}
-<form method="post">
+<form method="post" action="/.user">
 <input type="submit" value="Войти"/>
 </form>
 
 import logging
 import jinja2
 import os
+from datetime import datetime
 
 from google.appengine.ext import db
 from google.appengine.api import users
 	name = db.TextProperty()
 	email = db.StringProperty()
 	role = db.StringProperty()
-	created = db.DateTimeProperty(auto_now=True)
+	created = db.DateTimeProperty(auto_now_add=True)
 	approved = db.DateTimeProperty()
 
 CACHE_USER = 'user:'
 
 log = logging.getLogger('user')
 
-class ListUser(webapp2.RequestHandler):
+class AddUser(webapp2.RequestHandler):
 
 	def post(self):
+		''' Create new user '''
+
 		user = users.get_current_user()
 		if user is None:
 			self.redirect(users.create_login_url())
 			u = User(user_id=user.user_id(), name=user.nickname(), email=user.email())
 			u.put()
 
-		self.redirect('/.user')
+		self.redirect('/.user/all')
+
+
+class ListUser(webapp2.RequestHandler):
 
 	def get(self):
-		users = User.all().fetch(1000)
+		users = User.all().filter('approved', None).fetch(1000)
 
 		template = jinja_environment.get_template('list.html')
 		self.response.out.write(template.render({'users': users}))
 
+	def post(self):
+		# Approve user
+		user_id = self.request.get('user_id')
+		log.info('Approve user ' + user_id)
+
+		user = get_user(int(user_id))
+		user.approved = datetime.now()
+		user.put()
+
 
 debug = os.environ.get('SERVER_SOFTWARE', '').startswith('Dev')
 
-app = webapp2.WSGIApplication([('/.user', ListUser),]
-							   , debug=debug)
+app = webapp2.WSGIApplication([('/.user', AddUser),
+							   ('/.user/all', ListUser),
+								], debug=debug)