def test_edit_jam_not_creator(client, user_token, jam, reader_token): rv = client.post( f"/jams/{jam.url}/", headers={"Authorization": reader_token[1].token}, json={"title": "new title"}, ) assert rv.json["success"] == 0 assert rv.json["error"]["code"] == 3 jam = Jam.get() assert jam.title == jam.title
def user_jams(username): """Получить список джемов, которые пользователь организовал""" user = User.get_or_none(User.username == username) if user is None: return errors.not_found() query = Jam.get_jams_organized_by_user(user) jams = [e.to_json() for e in query] return jsonify({"success": 1, "jams": jams})
def jam_entry(url, entry_url): """Получить заявку на джем по ссылке""" jam = Jam.get_or_none(Jam.url == url) if jam is None: return errors.not_found() entry = JamEntry.get_or_none(JamEntry.url == entry_url) if entry is None: return errors.not_found() return jsonify({"success": 1, "entry": _entry_to_json(entry)})
def leave(url): """Не участвовать в джеме""" user = get_user_from_request() jam = Jam.get_or_none(Jam.url == url) if jam is None: return errors.not_found() entry = JamEntry.get_or_none(jam=jam, creator=user) if entry: entry.is_archived = True entry.save() return jsonify({"success": 1})
def get_criterias(url, entry_url): """Получить оценки для заявки на джем""" jam = Jam.get_or_none(Jam.url == url) if jam is None: return errors.not_found() entry = JamEntry.get_or_none(JamEntry.url == entry_url) if entry is None: return errors.not_found() criterias = JamEntryVote.select().where((JamEntryVote.entry == entry) & ( JamEntryVote.voter == get_user_from_request())) return jsonify({"success": 1, "criterias": _criterias_to_json(criterias)})
def get_jams(): """Получить список джемов""" query = Jam.get_current_jams() current_jams = [_jam_to_json(j) for j in query] query = Jam.get_closest_jams() closest_jams = [_jam_to_json(j) for j in query] query = Jam.get_closed_jams() limit = max(1, min(int(request.args.get("limit") or 20), 100)) paginated_query = PaginatedQuery(query, paginate_by=limit) closed_jams = [_jam_to_json(j) for j in paginated_query.get_object_list()] return jsonify({ "success": 1, "jams": { "current": current_jams, "closest": closest_jams, "closed": closed_jams, }, "meta": { "page_count": paginated_query.get_page_count() }, })
def finish(url): """Закончить джем""" user = get_user_from_request() jam = Jam.get_or_none(Jam.url == url) if jam is None: return errors.not_found() if jam.creator != user: return errors.no_access() jam.status = 2 jam.save() return jsonify({"success": 1})
def edit_jam(url): """Редактировать джем""" user = get_user_from_request() jam = Jam.get_or_none(Jam.url == url) if jam is None: return errors.not_found() if jam.creator != user: return errors.no_access() json = request.json title = json.get("title", jam.title) # url = json.get("url", jam.url) description = json.get("description", jam.description) short_description = json.get("short_description", jam.short_description) start_date = json.get("start_date", jam.start_date) end_date = json.get("end_date", jam.end_date) criterias = json.get("criterias", []) image = None if "image" in json: image = json["image"] edit_blog_for_jam(jam.blog, title, url, image) jam.title = title # jam.url = url jam.description = sanitize(description) jam.short_description = sanitize(short_description) jam.start_date = start_date jam.end_date = end_date if image: jam.logo = Content.get_or_none(Content.id == image) jam.updated_date = datetime.datetime.now() jam.save() JamCriteria.delete().where(JamCriteria.jam == jam).execute() for criteria in criterias: JamCriteria.create(jam=jam, title=criteria["title"], order=criteria["order"]) return jsonify({"success": 1, "jam": jam.to_json()})
def participiate(url): """Участвовать в джеме""" user = get_user_from_request() jam = Jam.get_or_none(Jam.url == url) if jam is None: return errors.not_found() entry = JamEntry.get_or_none(jam=jam, creator=user) if not entry: JamEntry.create( jam=jam, creator=user, created_date=datetime.datetime.now(), url=uuid.uuid4(), ) else: entry.is_archived = False entry.save() return jsonify({"success": 1})
def edit_comment(url, comment_id): """Редактировать комментарий""" jam = Jam.get_or_none(Jam.url == url) if jam is None: return errors.not_found() user = get_user_from_request() if user is None: return errors.not_authorized() json = request.get_json() text = None if "text" in json: text = sanitize(json.get("text")) else: return errors.wrong_payload("text") comment = _edit_comment(comment_id, user, text) return jsonify({"success": 1, "comment": comment.to_json()})
def test_edit_jam(client, user_token, jam): rv = client.post( f"/jams/{jam.url}/", headers={"Authorization": user_token[1].token}, json={"title": "new title"}, ) assert rv.json["success"] == 1 assert "jam" in rv.json blog = Blog.get() assert blog.creator == user_token[0], "Wrong creator" assert blog.description == 'Это блог для джема "new title"' participiation = BlogParticipiation.get() assert participiation.user == user_token[0] assert participiation.role == 1, "Not owner on creation" jam = Jam.get() assert jam.creator == user_token[0], "Wrong creator" assert jam.title == "new title" assert jam.url == jam.url assert jam.description == jam.description assert jam.short_description == jam.short_description
def jam(user, reader_token): jam_title = "test jam" jam_url = "test-jam" blog = Blog.create( title="test_blog", url="test_blog", creator=user, created_date=datetime.datetime.now(), updated_date=datetime.datetime.now(), ) BlogParticipiation.create(blog=blog, user=user, role=1) blog.title = jam_title blog.description = f'Это блог для джема "{jam_title}"' blog.url = jam_url blog.blog_type = 1 blog.save() jam = Jam.create( created_date=datetime.datetime.now(), updated_date=datetime.datetime.now(), creator=user, blog=blog, title=jam_title, url=jam_url, description="some description without tags", short_description="short description", start_date=datetime.datetime.now() + timedelta(seconds=1), end_date=datetime.datetime.now() + timedelta(seconds=5), ) from src.model import db db.db_wrapper.database.close() return jam
def comments_in_entry(url, entry_url): """Получить список комментариев для джема или добавить новый комментарий""" jam = Jam.get_or_none(Jam.url == url) if jam is None: return errors.not_found() entry = JamEntry.get_or_none(JamEntry.url == entry_url) if entry is None: return errors.not_found() if request.method == "GET": user = get_user_from_request() # if jam.is_draft: # if user is None: # return errors.no_access() # if jam.creator != user: # return errors.no_access() return _get_comments("jam_entry", entry.id, user) elif request.method == "POST": user = get_user_from_request() if user is None: return errors.not_authorized() json = request.get_json() if "text" in json: text = sanitize(json.get("text")) else: return errors.wrong_payload("text") parent_id = None if "parent" in json: parent_id = json["parent"] parent = None if parent_id: parent = Comment.get_or_none(Comment.id == parent_id) comment = _add_comment("jam_entry", entry.id, user, text, parent_id) if user.id != jam.creator.id: t = "Пользователь {0} оставил комментарий к заявке на джем {1}: {2}" notification_text = t.format(user.visible_name, jam.title, text) Notification.create( user=jam.creator, created_date=datetime.datetime.now(), text=notification_text, object_type="comment", object_id=comment.id, ) if parent is not None: if user.id != parent.creator.id: t = "Пользователь {0} ответил на ваш комментарий {1}: {2}" notification_text = t.format(user.visible_name, parent.text, text) Notification.create( user=parent.creator, created_date=datetime.datetime.now(), text=notification_text, object_type="comment", object_id=comment.id, ) return jsonify({"success": 1, "comment": comment.to_json()})