def get(self): suggestions = {} areas = set() query_string = self.request.get("query") location = self.request.get("location") if location == 'me': user_id = users.get_current_user().user_id() user = User.get_by_key_name('key:' + user_id) areas.add(str(user.area)) elif location == 'map': min_lat = float(self.request.get('min_lat')) min_lng = float(self.request.get('min_lng')) max_lat = float(self.request.get('max_lat')) max_lng = float(self.request.get('max_lng')) areas = geo.getInnerSquares(db.GeoPt(min_lat, min_lng), db.GeoPt(max_lat, max_lng)) #TODO: Optimization could reduce number of calls if (self.request.get("action") == "tags"): suggestions = get_suggestions(query_string, areas) # while len(areas) > 0: # area = areas.pop() # category = Category.all().filter('name =', category_string).filter('area =', area).get() # if category: # tags = category.tag_set.fetch(1000) # for tag in tags: # suggestions[tag.value] = suggestions.get(tag.value, 0) + 1 # suggestions = heapq.nlargest(100, suggestions.iteritems(), itemgetter(1)) self.response.out.write('{Suggestions: ' + simplejson.dumps(suggestions) + "}")
def get(self): user_id = users.get_current_user().user_id() user = User.get_by_key_name('key:' + user_id) if not user: self.response.out.write('false') else: json_response = simplejson.dumps({'user': user.user.nickname(), 'timestamp': str(user.timestamp), 'lat': user.home.lat, 'lon': user.home.lon, 'currency': user.currency}) self.response.out.write(json_response)
def post(self): name = request.json['name'] age = request.json['age'] profile = request.json['profile'] organisation = request.json['organisation'] new_user = User(name, age) new_user.profile = UserProfile(new_user.id, profile['address']) for i in organisation: new_organisation = Organisation(new_user.id, i['name']) new_user.organisation.append(new_organisation) db.session.add(new_organisation) db.session.add(new_user.profile) db.session.add(new_user) db.session.commit() return user_schema.jsonify(new_user)
def post(self): locale = self.request.get('locale') user_id = users.get_current_user().user_id() user = User.get_by_key_name('key:' + user_id) user.locale = locale user.put() logging.info('Changed user locale = ' + user.locale) json_response = simplejson.dumps({'locale': user.locale}) self.response.out.write(json_response)
def post(self): currency = self.request.get('currency') user_id = users.get_current_user().user_id() user = User.get_by_key_name('key:' + user_id) user.currency = currency user.put() logging.info('Changed user currency = ' + user.currency) json_response = simplejson.dumps({'currency': user.currency}) self.response.out.write(json_response)
def get(self, user_id): user = User.get_by_key_name('key:' + user_id) if user: nickname = user.user.nickname() template_values = {'user_id': user_id, 'nickname': nickname } path = os.path.join(os.path.dirname(__file__), 'User.html') self.response.out.write(template.render(path , template_values)) return else: self.redirect("/", true)
def initialize(self, *a, **kw): webapp2.RequestHandler.initialize(self, *a, **kw) self.user = None self.page = None self.new_url = '/' user_id = self.get_cookie('user_id') if user_id: self.user = User.get_user(user_id)
def post(self): # Retrieve inputs username = self.request.get('username') pwd = self.request.get('password') pwd_retry = self.request.get('verify') email = self.request.get('email') new_url = str(self.request.get('new_url')) # Validity checks errors = {} if not valid_username(username): errors['username'] = '******' if not valid_pwd(pwd): errors['pwd'] = 'Invalid password' elif pwd != pwd_retry: errors['pwd'] = 'Passwords not matching' if not valid_email(email): errors['email'] = 'Invalid Email' # Check if user already existing # if checks ok if not errors: user = User.login(username, pwd) if user: errors['username'] = '******' # if checks ok if not errors: # Add new user into DB user = User.signup(username, pwd, email) print 'New user signed up' # Add cookie self.login(user) # Redirect to new url self.redirect(new_url) else: # Format signup page # with inputs and errors self.render_signup(username, email, errors)
def add_users(): admin = User( username='******', role=UserRole.ADMIN, school=School.query.filter_by(name="Swaffham Prior Primary").first()) admin.password = "******" prior_admin = User( username='******', role=UserRole.SINGLE_SCHOOL_RW, school=School.query.filter_by(name="Swaffham Prior Primary").first()) prior_admin.password = "******" bulbeck_ro = User( username='******', role=UserRole.SINGLE_SCHOOL_RO, school=School.query.filter_by(name="Swaffham Bulbeck Primary").first()) bulbeck_ro.password = "******" DB.session.add(prior_admin) DB.session.add(bulbeck_ro) DB.session.add(admin) DB.session.commit()
def get(self): if not MainHandler.can_go_in(self): self.redirect('/static/cf.html') current_user = users.get_current_user() user = '' userEmail = '' currency = '$' locale = '' if current_user: tatamee_user = User.get_by_key_name('key:' + current_user.user_id()) if tatamee_user: currency = tatamee_user.currency locale = tatamee_user.locale user = current_user.nickname() userEmail = current_user.email() links = users.create_logout_url('/') else: links = users.create_login_url('/home') if not locale: locale = MainHandler.getLocale(self) template_values = {'user':user, 'userEmail':userEmail, 'links':links, 'locale':locale, 'currency': currency} path = os.path.join(os.path.dirname(__file__), 'Client.html') self.response.out.write(template.render(path , template_values))
def post(self): # Retrieve inputs username = self.request.get('username') pwd = self.request.get('password') new_url = str(self.request.get('new_url')) # Validity checks errors = {} if not valid_username(username): errors['username'] = '******' if not valid_pwd(pwd): errors['pwd'] = 'Invalid password' if self.user: errors['username'] = '******' # Try to get user from DB # if checks ok user = None if not errors: user = User.login(username, pwd) # Validity checks if not user: errors['username'] = '******' # If user successfully found if user: # Add cookie self.login(user) print 'User logged in' # Redirect to new url self.redirect(new_url) else: # Format login page # with inputs and errors self.render_login(username, errors)
def post(self): user_id = users.get_current_user().user_id() user = User.get_by_key_name('key:' + user_id) if not user: location = db.GeoPt(float(self.request.get('lat')), float(self.request.get('lng'))) square = geo.hashArea(location) user = User(key_name='key:' + user_id, home=location, area=square) user.user = users.get_current_user() country = self.request.get('country') #Can use memcache here if geo.country_currency.has_key(country): user.currency = geo.country_currency[country].decode('utf-8') else: user.currency = '$' else: user.home = db.GeoPt(float(self.request.get('lat')), float(self.request.get('lng'))) user.area = geo.hashArea(user.home) country = self.request.get('country') if geo.country_currency.has_key(country): user.currency = geo.country_currency[country].decode('utf-8') else: user.currency = '$' #TODO: Update location of all user items? # TODO: COunter not working # if not city.users: # city.users = 1 # else: # city.users = city.users + 1 #user.city = city user.put() logging.info('wrote user to database = ' + str(user.key())) json_response = simplejson.dumps({'currency': user.currency}) self.response.out.write(json_response)
def get_all_users(self): envelope = self.create_envelope('get_all_users') envelope['results'] = [ user.to_dict() for user in User.query().fetch() ] self.write_response(self.jsonify(envelope))
def post(self): id = self.request.get("id") if id != '': item_key = db.Key(id) item = db.get(item_key) item_parts_to_write = [] user_id = users.get_current_user().user_id() user = User.get_by_key_name('key:' + user_id) if (item.parent().user == users.get_current_user()): item.title = self.request.get("title") item.price = float(self.request.get("price")) item.description = self.request.get("description") item.category = self.request.get("category") item.tags = self.request.get("tags").lower() item.location = user.home item.currency = user.currency item.timestamp = datetime.datetime.now() item_parts_to_write.append(item) category_string = pluralize(clean_word(self.request.get("category").lower())) if self.request.get("img"): image = images.Image(self.request.get("img")) image_post.write_image(item, image, 0, "") tags = remove_duplicates([clean_word(word) for word in self.request.get("tags").split(',')]) extra_tags = [''] for tag in tags: if len(tag.split()) > 1: tags.extend(tag.split()) extra_tags.extend(tag.split()) if category_string: tags.append(category_string) extra_tags.append(category_string) singulars = get_singulars(tags) tags += singulars extra_tags += singulars title_words = [clean_word(word) for word in item.title.split(' ')] tags += title_words extra_tags += title_words # Delete old item light so that when sorted by __key__ ascending (default) # Oldest items (about to expire) show first. #TODO check if touching or re putting is enough? db.delete(item.itemlight_set[0]) item_light = ItemLight(parent=user, title=item.title, item_key=item.key(), location=user.home, area=user.area, tags=tags, extra_tags=extra_tags) item_parts_to_write.append(item_light) db.put(item_parts_to_write) logging.info('Item was updated: %s, by user: %s' % (item.title, item.parent().user.nickname())) else: user_id = users.get_current_user().user_id() user = User.get_by_key_name('key:' + user_id) item = Item(parent=user, location=user.home) item.currency = user.currency item.title = self.request.get("title") item.category = self.request.get("category") item.tags = self.request.get("tags").lower() item.price = float(self.request.get("price")) item.description = self.request.get("description") item_key = item.put() category_string = pluralize(clean_word(self.request.get("category").lower())) if self.request.get("img"): image = images.Image(self.request.get("img")) image_post.write_image(item, image, 0, "") tags = remove_duplicates([clean_word(word) for word in self.request.get("tags").split(',')]) extra_tags = [''] for tag in tags: if len(tag.split()) > 1: tags.extend(tag.split()) extra_tags.extend(tag.split()) if category_string: tags.append(category_string) extra_tags.append(category_string) singulars = get_singulars(tags) tags += singulars extra_tags += singulars title_words = [clean_word(word) for word in item.title.split(' ')] tags += title_words extra_tags += title_words item_light = ItemLight(parent=user, title=item.title, item_key=item_key, location=user.home, area=user.area, tags=tags, extra_tags=extra_tags) item_light.put() logging.info('Item was created: %s, by user: %s' % (item.title, item.parent().user.nickname())) self.response.out.write(cgi.escape(MyEncoder.encode(MyEncoder(), item))) self.response.headers.add_header('Location', '/item/' + str(item.key())) self.response.set_status(201)
def get(self): if (self.request.get("action") == "map"): #TODO: Add results from categories with high edges min_lat = float(self.request.get('min_lat')) min_lng = float(self.request.get('min_lng')) max_lat = float(self.request.get('max_lat')) max_lng = float(self.request.get('max_lng')) search_terms = [word.strip().lower() for word in self.request.get('search_term').split()] result = [] tags = {} areas = geo.getInnerSquares(db.GeoPt(min_lat, min_lng), db.GeoPt(max_lat, max_lng)) while len(areas) > 0: query = db.Query(ItemLight) query.filter("area =", areas.pop()) for term in search_terms: query.filter("tags =", term) items = query.fetch(200) for item in items: result.append(item) self.response.out.write(cgi.escape('{Items: ' + MyEncoder.encode(MyEncoder(), result) + "}")) elif (self.request.get("action") == "user"): user_id = self.request.get("user_id") offset = int(self.request.get("offset")) user = User.get_by_key_name('key:' + user_id) items = [] if user: items = ItemLight.all().ancestor(user).fetch(1000, offset=offset) self.response.out.write(cgi.escape('{Items: ' + MyEncoder.encode(MyEncoder(), items) + "}")) return elif (self.request.get("action") == "me"): items = [] user = users.get_current_user() if user: user = User.get_by_key_name('key:' + user.user_id()) query = db.Query(ItemLight) query.ancestor(user) items = query.fetch(1000) self.response.out.write(cgi.escape('{Items: ' + MyEncoder.encode(MyEncoder(), items) + "}")) elif (self.request.get("action") == "item"): key = self.request.get("key") item = Item.get(key) self.response.out.write(MyEncoder.encode(MyEncoder(), item)) elif (self.request.get("action") == "comments"): offset = int(self.request.get("offset")) key = self.request.get("id") item = Item.get(key) comments = Comment.all().ancestor(item).order('-__key__').fetch(5, offset=offset) self.response.out.write(cgi.escape('{Messages: ' + MyEncoder.encode(MyEncoder(), comments) + '}')) elif (self.request.get("action") == "good_ratings"): offset = int(self.request.get("offset")) key = self.request.get("id") item = Item.get(key) ratings = GoodRating.all().ancestor(item.parent()).order('-__key__').fetch(5, offset=offset) logging.info(MyEncoder.encode(MyEncoder(), ratings)) self.response.out.write(cgi.escape('{Messages: ' + MyEncoder.encode(MyEncoder(), ratings) + '}')) elif (self.request.get("action") == "bad_ratings"): offset = int(self.request.get("offset")) key = self.request.get("id") item = Item.get(key) ratings = BadRating.all().ancestor(item.parent()).order('-__key__').fetch(5, offset=offset) self.response.out.write(cgi.escape('{Messages: ' + MyEncoder.encode(MyEncoder(), ratings) + '}'))
def get_all_users(self): users = User.query().fetch() self.response.write(template.render("templates/users.html", {'users': users}))