def change_display_name(): if 'current_user_uid' not in session: return redirect(url_for('start')) else: req = request.get_json() User.change_display_name(session['current_user_uid'], req) session.pop('current_user_display_name', None) session['current_user_display_name'] = req['displayName'] return redirect(url_for('show_profile'))
def get_by_key(key): graffiti = db.get_dict(Graffiti.entry + '/' + key) return Graffiti(key=graffiti['key'], description=graffiti['description'], n_likes=graffiti['nlikes'], url=graffiti['url'], user=User.get_by_uid(graffiti['user']))
def get_user_by_user_key(user_key): user = User.search_by_key(user_key) status = 200 if not isinstance(user, dict): status = 404 user = f'Error 404. User with id: {user_key} not found' return Response(json.dumps(user), mimetype=mimetype, status=status)
def get_user_by_email(email): user = User.search_by_email(email) status = 200 if not isinstance(user, dict): status = 404 user = f'Error 404. User with email: {email} not found' return Response(json.dumps(user), mimetype=mimetype, status=status)
def get_graffiti(order=None, value=None): # ordered dictionary to list and getting first element graffiti = list( db.get_dict(Graffiti.entry, order=order, value=value).values())[0] return Graffiti(key=graffiti['key'], description=graffiti['description'], n_likes=graffiti['nlikes'], url=graffiti['url'], user=User.get_by_uid(graffiti['user']))
def delete_user_by_id(): if not request.json: return Response(json.dumps({'error': f'Bad request.'}), mimetype=mimetype, status=400) user_key = request.json['user'] user = User.search_by_key(user_key) status = 200 if user_key is not None and isinstance(user, dict): User(user_key).delete(user_key) result = {'result': f'Status 200. The user: {user_key} was deleted.'} else: status = 400 result = { 'Error': f'Error 400. Bad request. The provided data is not correct.' } return Response(json.dumps(result), mimetype=mimetype, status=status)
def start(): if request.method == 'POST': # when an user enters with Google email = request.form['email'] uid = request.form['uid'] display_name = request.form['displayName'] profile_picture = request.form['profilePicture'] user = User.get_by_uid(uid) if user is None: User.create_user(uid, email, display_name, profile_picture) session['current_user_email'] = email session['current_user_uid'] = uid session['current_user_display_name'] = display_name session['current_user_profile_picture'] = profile_picture return json.dumps({'status': 'OK'}) else: if 'current_user_uid' in session: return redirect(url_for('show_home')) else: return render_template('login.html', title='YATAM - Login')
def create_user(): if not request.json: return Response(json.dumps({'error': f'Bad request.'}), mimetype=mimetype, status=400) email = request.json['email'] username = request.json['username'] password = request.json['password'] user = User.search_by_email(email) status = 200 if not isinstance(user, dict): User(email=email, username=username, password=password) result = {'result': f'Status 200. The user was created.'} else: status = 400 result = { 'error': f'Error 400. The user with Email: {email} is already in the database' } return Response(json.dumps(result), mimetype=mimetype, status=status)
def get_all(): result = [] graffities = db.get_dict(Graffiti.entry) for key, value in graffities.items(): result.append( Graffiti(key=value.get('key'), description=value.get('description'), n_likes=value.get('nlikes'), url=value.get('url'), user=User.get_by_uid(value.get('user')))) return result
def get_by_key(key): leisure = db.get_dict(UserLeisure.entry + '/' + key) return UserLeisure(key=leisure['key'], description=leisure['description'], name=leisure['name'], address=leisure['address'], url_photo=leisure['url_photo'], user=User.get_by_uid(leisure['user']), schedule=leisure['schedule'], coordinates=leisure['coordinates'], n_likes=leisure['nlikes'])
def get_all(): result = [] ratings = db.get_dict(Rating.entry) for key, value in ratings.items(): result.append( Rating(key=value.get('key'), grade=value.get('grade'), description=value.get('description'), n_likes=value.get('n_likes'), leisure=value.get('url'), user=User.get_by_uid(value.get('user')))) return result
def get_ratings_by_leisure(order=None, value=None): ratings = db.get_dict(Rating.entry, order=order, value=value) result = [] if ratings: for key, value in ratings.items(): result.append( Rating(key=value.get('key'), grade=value.get('grade'), description=value.get('description'), n_likes=value.get('nlikes'), leisure=value.get('url'), user=User.get_by_uid(value.get('user')))) return result
def get_ratings_by_user(user_key): user = User.search_by_key(user_key) status = 200 if not isinstance(user, dict): status = 404 user = f'Error 404. User with id: {user_key} not found' return Response(json.dumps(user), mimetype=mimetype, status=status) else: ratings = Rating.search_by_user(user_key) if not bool(ratings): status = 404 ratings = f'Error 404. User with id: {user_key} has not ratings yet' return Response(json.dumps(ratings), mimetype=mimetype, status=status)
def get_user_leisure(order=None, value=None): # ordered dictionary to list and getting first element leisure = list( db.get_dict(UserLeisure.entry, order=order, value=value).values())[0] return UserLeisure(key=leisure['key'], description=leisure['description'], name=leisure['name'], address=leisure['address'], url_photo=leisure['url_photo'], user=User.get_by_uid(leisure['user']), schedule=leisure['schedule'], coordinates=leisure['coordinates'], n_likes=leisure['nlikes'])
def update_user_username(): if not request.json: return Response(json.dumps({'error': f'Bad request.'}), mimetype=mimetype, status=400) user_key = request.json['user'] username = request.json['username'] status = 200 user = User.search_by_key(user_key) if isinstance(user, dict): user = User(user_key) user.set_username(user_key, username) result = { 'result': f'Status 200. The user: {user_key} was updated with name {username}.' } else: status = 404 result = { 'Error': f'Error 404. User with key: {user_key} not found and cannot be updated' } return Response(json.dumps(result), mimetype=mimetype, status=status)
def update_user_password(): if not request.json: return Response(json.dumps({'error': f'Bad request.'}), mimetype=mimetype, status=400) user_key = request.json['user'] password = request.json['password'] status = 200 user = User.search_by_key(user_key) if isinstance(user, dict): user = User(user_key) user.set_password(user_key, password) result = { 'result': f'Status 200. The user: {user_key} was updated with password: {password}.' } else: status = 400 result = { 'Error': f'Error 400. Bad request. The provided data is not correct.' } return Response(json.dumps(result), mimetype=mimetype, status=status)
def get_all(): result = [] leisures = db.get_dict(UserLeisure.entry) for key, value in leisures.items(): result.append( UserLeisure(key=value.get('key'), description=value.get('description'), name=value.get('name'), url_photo=value.get('url_photo'), schedule=value.get('schedule'), user=User.get_by_uid(value.get('user')), coordinates=value.get('coordinates'), address=value.get('address'), n_likes=value.get('nlikes'))) return result
def create_graffiti(description, url, user): new_key = keygen.gen_random_key() data = { 'key': new_key, 'description': str(description), 'nlikes': 0, 'url': url, 'user': user } db.create(Graffiti.entry + '/' + new_key, data) return Graffiti(key=new_key, description=description, n_likes=0, url=url, user=User.get_by_uid(user))
def create_rating(): if not request.json: return Response(json.dumps({'error': f'Bad request.'}), mimetype=mimetype, status=400) user_key = request.json['user'] leisure_id = request.json['leisure'] grade = request.json['grade'] text = request.json['text'] user = User.search_by_key(user_key) leisure = '' for leisure_type in leisures_type: ls = LeisureList(leisure_type) leisure = ls.get_by_id(leisure_id) print(leisure) if isinstance(leisure, dict): break status = 200 if isinstance(user, dict) and isinstance(leisure, dict) and 5 >= grade >= 0: rating = Rating.search_by_user_and_leisure(user_key, leisure_id) if not isinstance(rating, dict): Rating(user=user_key, leisure=leisure_id, grade=grade, text=text) result = {'result': f'Status 200. The rating was created.'} else: status = 400 result = { 'error': f'Error 400. The rating with user_key:{user_key} and Leisure_ID:{leisure_id} is already in the' f'database.' } else: status = 400 result = { 'error': f'Error 400. The user_key:{user_key} or Leisure_ID:{leisure_id} is wrong or does not exists in the' f'system.' } return Response(json.dumps(result), mimetype=mimetype, status=status)
def create_user_leisure(name, description, address, url_photo, schedule, coordinates, user): new_key = keygen.gen_random_key() data = { 'key': new_key, 'name': name, 'description': str(description), 'address': address, 'url_photo': url_photo, 'schedule': schedule, 'coordinates': coordinates, 'user': user, 'nlikes': 0 } db.create(UserLeisure.entry + '/' + new_key, data) return UserLeisure(key=new_key, description=description, name=name, address=address, url_photo=url_photo, schedule=schedule, coordinates=coordinates, user=User.get_by_uid(user))