def update_score(db, _id) -> int: user = User.fetch(db, _id) score_likes = 0 boxes = CardBox.fetch_multiple(db, user.cardboxs) for box in boxes: score_likes = score_likes + box.rating score_followers = 0 users = User.fetch_all(db) for u in users: if u.is_following(user._id): score_followers += 1 score_boxes = len(user.cardboxs) score = (user.offline_score + score_likes * 100 + score_followers * 200 + score_boxes * 100) db.zadd(TABLE_SCORE, {_id: score}) return score
def user_settings(): # <-- Change profile picture --> picture_form = PictureForm() if picture_form.submit.data and picture_form.validate_on_submit(): filename = utils.sha1_of(current_user._id) + '.jpg' filepath = os.path.join(app.static_folder, 'img', filename) utils.save_file_field_img(picture_form.picture, filepath) flash('Successfully changed profile picture!') return (redirect(url_for('user_settings'))) elif picture_form.submit.data and picture_form.is_submitted(): for message in picture_form.picture.errors: flash(message, category='error') return (redirect(url_for('user_settings'))) picture_filepath = utils.profile_img_path(app, current_user._id) # <-- Change Showcase --> boxes = CardBox.fetch_multiple(db, current_user.cardboxs) showcase_form = ShowcaseForm() choices = [(b._id, b.name) for b in boxes] choices = [('', '---')] + choices showcase_form.cardbox_input.choices = choices if (showcase_form.submit_showcase.data and showcase_form.validate_on_submit()): new_showcase = dict(info=showcase_form.info_input.data, cardbox=showcase_form.cardbox_input.data, show_info=showcase_form.check_info.data, show_cardbox=showcase_form.check_cardbox.data, show_rank=showcase_form.check_rank.data) current_user.showcase = new_showcase current_user.store(db) flash('Showcase adjusted!') return redirect(url_for('user_settings')) showcase_form.check_info.data = current_user.showcase['show_info'] showcase_form.info_input.data = current_user.showcase['info'] showcase_form.check_cardbox.data = current_user.showcase['show_cardbox'] showcase_form.cardbox_input.data = current_user.showcase['cardbox'] showcase_form.check_rank.data = current_user.showcase['show_rank'] showcase_form.rank_token.data = current_user.get_rank(db) # <-- Change Password --> password_form = ChangePasswordForm() if (password_form.submit_password.data and password_form.validate_on_submit()): if not current_user.check_password(password_form.old_password.data): flash('Old passwort not correct.', 'error') return redirect(url_for('user_settings')) current_user.set_password(password_form.new_password.data) current_user.store(db) flash('Successfully changed password!') return redirect(url_for('user_settings')) return render_template('profile_settings.html', user=current_user, picture_form=picture_form, showcase_form=showcase_form, password_form=password_form, picture_filepath=picture_filepath)
def add_cardbox(): if not request.is_json: print('not json') abort(404) # already returns dictionary payload = request.get_json() # <-- validate payload --> req = ('username', 'password', 'tags', 'content', 'name', 'info') if not payload or not all(r in payload for r in req): print('missing key in payload') abort(404) # None check if any(payload[key] is None for key in req): print('key is None') abort(404) # type check if not all([ isinstance(payload['tags'], list), isinstance(payload['name'], str), isinstance(payload['info'], str) ]): print('key wrong type') abort(404) if any(' ' in tag for tag in payload['tags']): print('whitespace in tag') abort(404) # <-- validate content --> if not isinstance(payload['content'], list): print('content not list') abort(404) attrs = ('question', 'answers', 'correct_answer', 'explanation') if not all(a in _dict for a in attrs for _dict in payload['content']): print('missing key in card') abort(404) number_of_answers = 3 for card in payload['content']: q, a, ca, e = (card['question'], card['answers'], card['correct_answer'], card['explanation']) if not isinstance(q, str) or not isinstance(e, str): abort(404) if not (isinstance(a, list) and len(a) == number_of_answers): abort(404) if not (isinstance(ca, int) and ca in range(number_of_answers)): abort(404) # check authorization if User.exists(db, payload['username']): user = User.fetch(db, payload['username']) if not user.check_password(payload['password']): print('unauthorized') abort(404) cardbox_id = CardBox.gen_card_id() # 'Update'-Function boxes = CardBox.fetch_multiple(db, user.cardboxs) for box in boxes: if box.name == payload['name']: cardbox_id = box._id break else: user.cardboxs.append(cardbox_id) # store content in separate redis table Card.save_content(db, cardbox_id, payload['content']) # create CardBox object for metadata new_box = CardBox(cardbox_id, name=payload['name'], owner=user._id, rating=0, info=payload['info'], tags=payload['tags']) new_box.store(db) user.store(db) User.update_score(db, user._id) return 'OK'