def comment(project, issue): """Submits a new comment.""" text = request.form.get('text') error = comment_validation(text) if error is not None: flash(error) else: new_comment = Comment(text=text, user=current_user, issue=issue) db.session.add(new_comment) if current_user == issue.assignee: admin_reviewer_add_notification( project, 'new comment', { 'avatar': current_user.avatar(), 'fullname': current_user.fullname(), 'projectId': project.id, }, issue.id, ) else: issue.assignee.add_notification( 'new comment', { 'avatar': current_user.avatar(), 'fullname': current_user.fullname(), 'projectId': project.id, }, issue.id, ) db.session.commit() return redirect(url_for('issue.issue', id=project.id, issue_id=issue.id))
def close(project, issue): """Marks an issue as closed. Args: project: in: close_issue_permission_required() decorator type: Project description: A Project object whose id is the same as the id in the path. issue: in: close_issue_permission_required() decorator type: Issue description: An Issue object whose id is the same as the id in the path. url: in: formData type: String description: The url to be redirected to. Responses: 302: description: Redirect to the url page. 400: description: Bad request. 403: description: Current user does not have the permission. 404: description: Project or issue does not exist. """ redirect_url = request.form.get('url') issue.status = 'Closed' issue.closed_timestamp = time() if issue.assignee is None: issue.creator.add_notification( 'mark closed', { 'avatar': current_user.avatar(), 'fullname': current_user.fullname(), 'issueTitle': issue.title, }, ) else: issue.assignee.add_notification( 'mark closed', { 'avatar': current_user.avatar(), 'fullname': current_user.fullname(), 'issueTitle': issue.title, }, ) db.session.commit() return redirect(redirect_url)
def edit_profile(): form = UserProfileForm() if request.method == 'GET': if current_user.is_authenticated: form.username = current_user.username form.nickname = current_user.nickname form.email = current_user.email form.about_me = current_user.about_me form.last_seen = current_user.last_seen form.sex = '' form.avatar = '' return render_template('auth/editprofile.html', title='', form=form) elif form.validate_on_submit: current_user.nickname = form.nickname.data current_user.email = form.email.data current_user.about_me = form.about_me.data db.session.commit() path = os.path.join( os.path.join(os.getcwd(), "app\\static\\avatars", current_user.avatar(100))) current_app.logger.info('upload file path is: %s' % path) # filepath = get_avatar_filepath('jpg') upload_file(path) flash('Your changes have been saved.') return redirect(url_for('auth.edit_profile'))
def wine_detail(wine_id): if current_user.is_authenticated: display_name = current_user.displayName() avatar = current_user.avatar() is_admin = current_user.is_admin() is_owner = get_is_owner(wine_id) else: display_name = None avatar = None is_admin = False is_owner = False wine = db.session.query(Wine).get(wine_id) region = db.session.query(Region).get(wine.region) category = db.session.query(Category).get(wine.category) price = get_dollar_signs(wine) return render_template('wines/wine-detail.html', wine=wine, region=region, category=category, price=price, is_admin=is_admin, is_owner=is_owner, display_name=display_name, avatar=avatar)
def edit_profile(): form = UpdateAccountForm() if form.validate_on_submit(): current_user.username = form.username.data current_user.about = form.about.data db.session.commit() flash('Информация обновлена!', 'success') return redirect(url_for('my_profile')) elif request.method == 'GET': form.username.data = current_user.username form.about.data = current_user.about return render_template('profile.html', title='Account', image_file=current_user.avatar(125), image_file_small=current_user.avatar(32), form=form)
def resmat(): image_file_small = '' if current_user.is_authenticated: image_file_small = current_user.avatar(32) return render_template('resmat.html', title='Справочные материалы', image_file_small=image_file_small)
def profile(): form = EditForm() if form.validate_on_submit(): count_of_same_data = User.query.filter((User.username==form.username.data) & (User.email==form.email.data)).count() if count_of_same_data == 1: flash('Your profile has been edited', 'alert-success') else: user = User.query.get(current_user.id) user.username = form.username.data user.email = form.email.data try: db.session.commit() flash('Your profile has been edited', 'alert-success') except: flash('Another user has same username or email', 'alert-danger') return redirect(url_for('main.profile')) if request.method == 'GET': form.username.data = current_user.username form.email.data = current_user.email if current_user.url_photo == None: url_photo = current_user.avatar(128) else: url_photo = current_user.url_photo form.url_avatar = url_photo return render_template('main/profile.html', form=form)
def faq(): image_file_small = '' if current_user.is_authenticated: image_file_small = current_user.avatar(32) return render_template('faq.html', title='FAQ', image_file_small=image_file_small)
def about(): image_file_small = '' if current_user.is_authenticated: image_file_small = current_user.avatar(32) return render_template('about.html', title='About', image_file_small=image_file_small)
def room(room_link): not_joined = False this_room = Room.query.filter_by(link=room_link).first() gm = User.query.filter_by(id=this_room.user_id).first() all_tokens = Token.query.filter_by(room_id=this_room.id) try: guests = list( map( lambda x: (User.query.get_or_404(x).username, User.query.get_or_404(x).id) if x != gm.id else (None, None), json.loads(this_room.guests))) except TypeError: guests = [] is_gm = (current_user.id == gm.id) if (current_user.username, current_user.id) not in guests and not is_gm: not_joined = True color = gen_color(this_room.id) return render_template('room.html', title=this_room.name, room=this_room, not_joined=not_joined, color=color, gm=gm, tokens=all_tokens, is_gm=is_gm, guests=guests, image_file_small=current_user.avatar(32))
def new_character(): form = CharacterForm() if form.validate_on_submit(): character = Character( name=form.name.data, lvlclass=form.lvlclass.data, armor=int(form.armor.data), speed=int(form.speed.data), race=form.race.data, stats= f'[{int(form.stat1.data)},{int(form.stat2.data)},{int(form.stat3.data)},{int(form.stat4.data)},{int(form.stat5.data)},{int(form.stat6.data)}]', willsave= f'[{int(form.sw1.data)},{int(form.sw2.data)},{int(form.sw3.data)},{int(form.sw4.data)},{int(form.sw5.data)},{int(form.sw6.data)}]', skills= f'[{int(form.sk1.data)},{int(form.sk2.data)},{int(form.sk3.data)},{int(form.sk4.data)},{int(form.sk5.data)},{int(form.sk6.data)},{int(form.sk7.data)},{int(form.sk8.data)},{int(form.sk9.data)},{int(form.sk10.data)},{int(form.sk11.data)},{int(form.sk12.data)},{int(form.sk13.data)},{int(form.sk14.data)},{int(form.sk15.data)},{int(form.sk16.data)},{int(form.sk17.data)},{int(form.sk18.data)}]', abilities=form.abilities.data, inventory=form.inventory.data, health=form.health.data, author=current_user) db.session.add(character) db.session.commit() flash('Персонаж успешно создан!', 'success') return redirect(url_for('chars')) return render_template('new_character.html', title='Создать персонажа', form=form, image_file_small=current_user.avatar(32))
def add_basic_notification(self, name, title): self.add_notification( name, { 'avatar': current_user.avatar(), 'fullname': current_user.fullname(), 'projectTitle': title, }, )
def create(user_project): """Creates a new issue. Args: user_project: in: permission_required() decorator type: UserProject description: A UserProject object whose user_id belongs to the current user and project_id is the same as the query parameter - id. title: in: formData type: string description: The new issue's title. description: in: formData type: string description: The new issue's description. Responses: 302: description: Redirect to project page. 403: description: Forbidden. 404: description: Project not found. """ title = request.form.get('title') description = request.form.get('description') project = user_project.project error = create_validation(title, description) if error is not None: flash(error) else: new_issue = Issue( title=title, description=description, creator=current_user, project=project, ) db.session.add(new_issue) admin_reviewer_add_notification( project, 'new issue', { 'avatar': current_user.avatar(), 'fullname': current_user.fullname(), 'projectTitle': project.title, 'issueTitle': title, }, ) db.session.commit() os.makedirs( os.path.join(current_app.config['UPLOAD_PATH'], str(new_issue.id))) return redirect(url_for('project.project', id=project.id))
def assign(project, issue): """Assigns an issue. Args: project: in: assign_issue_permission_required() decorator type: Project description: A Project object whose id is the same as the id in the path. issue: in: assign_issue_permission_required() decorator type: Issue description: An Issue object whose id is the same as the id in the path. priority: in: formData type: String description: The priority level of the issue. assignee_id: in: formData type: int description: The assignee's id. Responses: 302: description: Redirect to project page. 400: description: Bad request. 403: description: Current user does not have the permission. 404: description: Project or issue does not exist. """ priority = request.form.get('priority') assignee_id = request.form.get('assignee_id') error = assign_validation(project, issue, priority, assignee_id) if error is not None: flash(error) else: issue.priority = priority issue.status = 'In Progress' issue.assignee_id = assignee_id if issue.assignee != current_user: issue.assignee.add_notification( 'assign issue', { 'avatar': current_user.avatar(), 'fullname': current_user.fullname(), 'projectId': project.id, }, issue.id, ) db.session.commit() return redirect(url_for('project.project', id=project.id))
def user(user_id): this_user = User.query.get_or_404(user_id) image_file = this_user.avatar(200) this_chars = Character.query.filter_by(user_id=user_id) return render_template('user.html', user=this_user, chars=this_chars, image_file=image_file, image_file_small=current_user.avatar(32), you=current_user)
def change_role(user_project): """Changes a member's role. Changes a member's role. If member's previous role is Reviewer, demote to Developer. Otherwise, promote to Reviewer. Notifies the member. Produces: application/json Args: user_project: in: permission_required() decorator type: UserProject description: A UserProject object whose user_id belongs to the current user and project_id is the same as the query parameter - id. user_id: in: json type: int description: Id of the user to be promoted or demoted. Responses: 200: description: Change successfully. 400: description: Bad request. 404: description: User not found. 422: description: Unprocessable. """ body = request.get_json() user_id = body.get('user_id') if user_id is None: abort(400) project = user_project.project user = User.query.get_or_404(user_id) user_project = change_role_validation(project, user) new_role = user_project.change_role() user.add_notification( 'change role', { 'avatar': current_user.avatar(), 'fullname': current_user.fullname(), 'projectTitle': project.title, 'newRole': new_role.name, }, ) db.session.commit() return {'success': True}
def delete(project, issue): """Deletes an issue. Produces: application/json text/html Args: project: in: delete_issue_permission_required() decorator type: Project description: A Project object whose id is the same as the id in the path. issue: in: delete_issue_permission_required() decorator type: Issue description: An Issue object whose id is the same as the id in the path. Responses: 302: description: Redirect to project page. 400: description: Bad request. 403: description: Forbidden. 404: description: Project or issue does not exist. """ db.session.delete(issue) data = { 'avatar': current_user.avatar(), 'fullname': current_user.fullname(), 'projectTitle': project.title, 'issueTitle': issue.title, } if issue.creator != current_user: issue.creator.add_notification( 'delete issue', data, ) if issue.assignee is not None and issue.assignee != current_user: issue.assignee.add_notification( 'delete issue', data, ) db.session.commit() shutil.rmtree( os.path.join(current_app.config['UPLOAD_PATH'], str(issue.id)), ignore_errors=True, ) return redirect(url_for('project.project', id=project.id))
def rooms(): my_rooms = Room.query.filter_by(user_id=current_user.id) all_rooms = Room.query.all() j_rooms = [] for room in all_rooms: if current_user.id in json.loads( room.guests) and current_user.id != room.user_id: j_rooms.append(room) return render_template('rooms.html', title='Комнаты', my_rooms=my_rooms, j_rooms=j_rooms, image_file_small=current_user.avatar(32))
def post_comment(): form = g.comment_form post_id = request.form['id'] body = request.form['comment'] post = Post.query.filter_by(id=post_id).first() language = guess_language(body) if language == 'UNKNOWN' or len(language) > 5: language = '' comment = Comment(body=body,language=language,post_id=int(post_id),user_id=current_user.id) db.session.add(comment) db.session.commit() return jsonify({ 'comment_num':post.comments.count(), 'avatarURL':current_user.avatar(70), 'comment_username':current_user.username, })
def editself(): form = EditProfile() if form.validate_on_submit(): user = User.query.get(current_user.id) user.lang = form.lang.data user.paypal = form.paypal.data if user.username != form.username.data: user.username = form.username.data db.session.commit() flash(_l('Gespeichert')) form.username.data = current_user.username form.email.data = current_user.email form.paypal.data = current_user.paypal form.lang.data = current_user.lang or app.config['BABEL_DEFAULT_LOCALE'] refresh() return render_template('auth/edit_self.html', form=form, avatar=current_user.avatar(175))
def new_room(): form = RoomForm() if form.validate_on_submit(): time = ''.join(map(str, localtime()[:])) link = md5(time.encode('utf-8')).hexdigest()[:-2] + str(randint( 10, 99)) room = Room(name=form.name.data, author=current_user, guests=str([current_user.id]), link=link) db.session.add(room) db.session.commit() flash('Комната создана!', 'success') return redirect(url_for('rooms')) return render_template('new_room.html', title='Создать комнату', form=form, image_file_small=current_user.avatar(32))
def edit_profile(): form = EditProfileForm(current_user.username) if form.validate_on_submit(): current_user.username = form.edit_username.data current_user.about_me = form.about_me.data db.session.commit() flash(_('Your changes have been saved.'), 'success') return redirect(url_for('main.user', username=current_user.username)) elif request.method == 'GET': form.edit_username.data = current_user.username form.about_me.data = current_user.about_me return render_template('edit_profile.html', avatar=current_user.avatar(350), title='Edit Profile', form=form)
def edit_character(char_id): form = CharacterForm() current_char = Character.query.get_or_404(char_id) if current_char.user_id != current_user.id and not current_user.is_moderator: flash('Ошибка доступа', 'danger') return redirect(url_for('chars')) if form.validate_on_submit(): current_char.name = form.name.data current_char.lvlclass = form.lvlclass.data current_char.armor = int(form.armor.data) current_char.speed = int(form.speed.data) current_char.race = form.race.data current_char.stats = f'[{int(form.stat1.data)},{int(form.stat2.data)},{int(form.stat3.data)},{int(form.stat4.data)},{int(form.stat5.data)},{int(form.stat6.data)}]' current_char.willsave = f'[{int(form.sw1.data)},{int(form.sw2.data)},{int(form.sw3.data)},{int(form.sw4.data)},{int(form.sw5.data)},{int(form.sw6.data)}]' current_char.skills = f'[{int(form.sk1.data)},{int(form.sk2.data)},{int(form.sk3.data)},{int(form.sk4.data)},{int(form.sk5.data)},{int(form.sk6.data)},{int(form.sk7.data)},{int(form.sk8.data)},{int(form.sk9.data)},{int(form.sk10.data)},{int(form.sk11.data)},{int(form.sk12.data)},{int(form.sk13.data)},{int(form.sk14.data)},{int(form.sk15.data)},{int(form.sk16.data)},{int(form.sk17.data)},{int(form.sk18.data)}]' current_char.abilities = form.abilities.data current_char.inventory = form.inventory.data current_char.health = form.health.data db.session.commit() flash('Информация обновлена!', 'success') return redirect(url_for('chars')) elif request.method == 'GET': form.name.data = current_char.name form.lvlclass.data = current_char.lvlclass form.armor.data = current_char.armor form.speed.data = current_char.speed form.race.data = current_char.race form.health.data = current_char.health form.abilities.data = current_char.abilities form.inventory.data = current_char.inventory willsave = json.loads(current_char.willsave) skills = json.loads(current_char.skills) stats = json.loads(current_char.stats) for stat in range(len(willsave)): exec(f'form.sw{stat+1}.data = bool({willsave[stat]})') for stat in range(len(skills)): exec(f'form.sk{stat+1}.data = bool({skills[stat]})') for stat in range(len(stats)): exec(f'form.stat{stat+1}.data = {stats[stat]}') return render_template('new_character.html', title='Редактировать персонажа', image_file_small=current_user.avatar(32), form=form)
def category_detail(category_id): if current_user.is_authenticated: display_name = current_user.displayName() avatar = current_user.avatar() is_admin = current_user.is_admin() is_owner = get_is_owner(category_id) else: display_name = None avatar = None is_admin = False is_owner = False cat_list = get_sorted_categories() category = db.session.query(Category).filter_by(id=category_id).one() wine_list = db.session.query(Wine).filter_by(category=category_id).all() data = Prepopulated_Data(category, cat_list) return render_template('categories/category-detail.html', category=data, is_admin=is_admin, is_owner=is_owner, wine_list=wine_list, display_name=display_name, avatar=avatar)
def send_message(recipient): user = User.query.filter_by(username=recipient).first_or_404() from_messages = request.args.get('from_messages', False, type=bool) form = MessageForm() if form.validate_on_submit(): try: language = TextBlob(form.message.data).detect_language() except: language = '' msg = Message(author=current_user, recipient=user, body=form.message.data, language=language) db.session.add(msg) user.add_notification('unread_message_count', user.new_messages()) user.add_notification( 'new_message', { 'state': 1, 'title': _("New message from %(username)s", username=current_user.username), 'icon': current_user.avatar(60), 'body': msg.body, 'url': url_for('main.messages') }) db.session.commit() flash(_('Your message has been sent.'), category='success') if from_messages: return redirect(url_for('main.messages')) return redirect(url_for('main.user', username=recipient)) return render_template('send_message.html', title=_('Send Message'), form=form, recipient=recipient)
def region_detail(region_id): if current_user.is_authenticated: display_name = current_user.displayName() avatar = current_user.avatar() is_admin = current_user.is_admin() is_owner = get_is_owner(region_id) else: display_name = None avatar = None is_admin = False is_owner = False reg_list = get_sorted_regions() region = db.session.query(Region).filter_by(id=region_id).one() wine_list = db.session.query(Wine).filter_by(region=region_id).all() data = Prepopulated_Data(region, reg_list) print(data) return render_template('regions/region-detail.html', region=data, is_admin=is_admin, is_owner=is_owner, wine_list=wine_list, display_name=display_name, avatar=avatar)
def add_token(room_id): form = TokenForm() this_room = Room.query.get_or_404(room_id) if this_room.user_id != current_user.id: flash('Ошибка доступа', 'danger') return redirect(url_for('room', room_link=this_room.link)) my_chars = Character.query.filter_by(user_id=current_user.id) form.character.choices = [(char1.id, char1.name) for char1 in my_chars] if form.validate_on_submit(): chosen_char = Character.query.get_or_404(form.character.data) token = Token(photo="default.png", name=chosen_char.name, party=this_room, char=chosen_char, health=chosen_char.health, max_health=chosen_char.health, slots=chosen_char.inventory) db.session.add(token) db.session.commit() return redirect(url_for('room', room_link=this_room.link)) return render_template('new_token.html', title='Создать токен', form=form, image_file_small=current_user.avatar(32))
def restore(project, issue): """Restores a closed or resolved issue back to previous status. Restores a closed or resolved issue back to previous status. Restores resolved issue's status to In Progress. Restores closed issue's status to Open or In Progress based on if there is an existing assignee. Args: project: in: assign_issue_permission_required() decorator type: Project description: A Project object whose id is the same as the id in the path. issue: in: assign_issue_permission_required() decorator type: Issue description: An Issue object whose id is the same as the id in the path. url: in: formData type: String description: The url to be redirected to. Responses: 302: description: Redirect to the url page. 400: description: Bad request. 403: description: Current user does not have the permission. 404: description: Project or issue does not exist. """ redirect_url = request.form.get('url') if issue.status == 'Closed': if issue.assignee is None: status = 'Open' admin_reviewer_add_notification( project, 'mark open', { 'avatar': current_user.avatar(), 'fullname': current_user.fullname(), 'issueTitle': issue.title, }, ) else: status = 'In Progress' if issue.assignee != current_user: issue.assignee.add_notification( 'mark in progress', { 'avatar': current_user.avatar(), 'fullname': current_user.fullname(), 'issueTitle': issue.title, 'preStatus': issue.status, 'projectId': project.id, }, issue.id, ) issue.closed_timestamp = None else: status = 'In Progress' if issue.assignee != current_user: issue.assignee.add_notification( 'mark in progress', { 'avatar': current_user.avatar(), 'fullname': current_user.fullname(), 'issueTitle': issue.title, 'preStatus': issue.status, 'projectId': project.id, }, issue.id, ) issue.resolved_timestamp = None issue.status = status db.session.commit() return redirect(redirect_url)
def edit(project, issue): """Edits an issue. Edits an issue's title and description. If the project's status is In Progress, edits the priority and assignee too. Produces: application/json text/html Args: project: in: edit_issue_permission_required() decorator type: Project description: A Project object whose id is the same as the id in the path. issue: in: edit_issue_permission_required() decorator type: Issue description: An Issue object whose id is the same as the id in the path. title: in: json type: string description: The issue's new title. description: in: json type: string description: The issue's new description. priority: in: json type: String description: The priority level of the issue. assignee_id: in: json type: String description: The assignee's id. Responses: 200: description: Edit successfully. 400: description: Bad request. 403: description: Forbidden. 404: description: Project or issue does not exist. 422: description: Unprocessable. """ body = request.get_json() title = body.get('title') description = body.get('description') if None in (title, description): abort(400) if issue.status == 'In Progress': priority = body.get('priority') assignee_id = body.get('assignee_id') if None in (priority, assignee_id): abort(400) new_assignee = edit_validation(issue, title, description, project, priority, assignee_id) issue.priority = priority if issue.assignee != new_assignee: if issue.assignee != current_user: issue.assignee.add_notification( 'remove assignee', { 'avatar': current_user.avatar(), 'fullname': current_user.fullname(), 'issueTitle': issue.title, }, ) if new_assignee != current_user: new_assignee.add_notification( 'assign issue', { 'avatar': current_user.avatar(), 'fullname': current_user.fullname(), 'projectId': project.id, }, issue.id, ) issue.assignee_id = assignee_id else: edit_validation(issue, title, description) issue.title = title issue.description = description db.session.commit() return {'success': True}
def invite(user_project): """Searches for users to be invited or sends invitation. GET: Args: search: in: query type: string description: The search term. Responses: 200: description: Searched user's information. POST: Args: target: in: json type: string description: The target to be searched with. role: in: json type: string description: Role name to be assigned to the invited user. Responses: 200: description: Invitation successfully sent. 400: description: Bad request. 422: description: Unprocessable. Produces: application/json text/html Args: user_project: in: permission_required() decorator type: UserProject description: A UserProject object whose user_id belongs to the current user and project_id is the same as the query parameter - id. Responses: 403: description: Current user is not a member of the project or does not have the permission. 404: description: Project does not exist. """ project = user_project.project if request.method == 'POST': body = request.get_json() target = body.get('target') role_name = body.get('role') if None in (target, role_name): abort(400) user = invite_validation(project, target, role_name) if user is not None: data = { 'avatar': current_user.avatar(), 'fullname': current_user.fullname(), 'projectTitle': project.title, 'roleName': role_name, } user.add_notification('invitation', data, target_id=project.id) db.session.commit() return {'success': True} search_term = request.args.get('search') if search_term is None: return {'success': True, 'users': []} if search_term[-1] == ' ': search_term = search_term[:-1] ilike_regex = f'{search_term}%' users = ( User.query.filter( User.username.ilike(ilike_regex) | db.func.concat(User.first_name, ' ', User.last_name).ilike(ilike_regex) ) .order_by(db.func.concat(User.first_name, ' ', User.last_name)) .limit(10) .all() ) return { 'success': True, 'users': [ { 'fullname': user.fullname(), 'username': user.username, 'avatar': user.avatar(), 'joined': user in project.users, } for user in users ], }