Ejemplo n.º 1
0
def edit_my_entry(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 is None:
        return errors.not_found()

    json = request.json

    title = json.get("title", entry.title)
    url = json.get("url", entry.url)
    description = json.get("info", entry.info)
    short_description = json.get("short_info", entry.short_info)
    links = json.get("links", [])

    has_entry_with_same_url = False
    entries_with_same_url = JamEntry.select().where((JamEntry.url == url)
                                                    & (JamEntry.jam == jam))
    for e in entries_with_same_url:
        if e.id != entry.id:
            has_entry_with_same_url = True

    if has_entry_with_same_url:
        return errors.jam_entry_url_already_taken()

    image = None
    if "logo" in json:
        image = json["logo"]

    entry.title = title
    entry.url = url
    entry.info = sanitize(description)
    entry.short_info = sanitize(short_description)

    if image:
        entry.logo = Content.get_or_none(Content.id == image)

    JamEntryLink.delete().where(JamEntryLink.entry == entry).execute()
    for link in links:
        JamEntryLink.create(
            entry=entry,
            title=link["title"],
            href=link["href"],
            order=link["order"],
        )

    entry.save()

    return jsonify({"success": 1, "entry": _entry_to_json(entry)})
Ejemplo n.º 2
0
def _jam_to_json(jam):
    jam_dict = jam.to_json()

    jam_dict["is_participiating"] = False
    user = get_user_from_request()
    if user:
        entry = JamEntry.get_or_none(jam=jam, creator=user)
        if entry:
            jam_dict["is_participiating"] = True

    jam_dict["participators"] = JamEntry.select().where(
        JamEntry.jam == jam).count()

    return jam_dict
Ejemplo n.º 3
0
def _get_post(post):
    if post is None:
        return errors.not_found()

    if post.is_draft:
        user = get_user_from_request()
        if user is None:
            return errors.no_access()

        if post.creator != user:
            return errors.no_access()

    user = get_user_from_request()

    if post.blog is not None:
        # workaround, delete later. Sometime in the past you can save post
        # without blog, so this check will fail.
        has_access = Blog.has_access(post.blog, user)
        if not has_access:
            return errors.no_access()

    post_dict = post.to_json()
    post_dict = Vote.add_votes_info(post_dict, 3, user)

    entries = JamEntry.get_entries_for_post(post)
    post_dict["jam_entries"] = [e.to_json() for e in entries]

    return jsonify({"success": 1, "post": post_dict})
Ejemplo n.º 4
0
def edit_comment_in_entry(url, entry_url, comment_id):
    """Редактировать комментарий"""
    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()

    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()})
Ejemplo n.º 5
0
def add_votes(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()

    JamEntryVote.delete().where((JamEntryVote.entry == entry) & (
        JamEntryVote.voter == get_user_from_request())).execute()

    json = request.json

    json_criterias = json
    for criteria_id, vote in json_criterias.items():
        JamEntryVote.create(
            entry=entry,
            voter=get_user_from_request(),
            vote=vote,
            criteria=criteria_id,
        )

    return jsonify({"success": 1})
Ejemplo n.º 6
0
def manage_jam_entries(post, json):
    if json is not None:
        entries = json.get("jam_entries", [])
        JamEntryPost.delete().where(JamEntryPost.post == post).execute()
        for e in entries:
            entry = JamEntry.get_or_none(JamEntry.id == e["id"])
            if entry is None:
                # just skip for now
                continue
            JamEntryPost.create(entry=entry, post=post)
Ejemplo n.º 7
0
def my_entry(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)

    return jsonify({"success": 1, "entry": _entry_to_json(entry)})
Ejemplo n.º 8
0
def jam_entries(url):
    """Получить список заявок на джем"""
    jam = Jam.get_or_none(Jam.url == url)

    if jam is None:
        return errors.not_found()

    entries = JamEntry.select().where(JamEntry.jam == jam)
    entries = [_entry_to_json(entry) for entry in entries]

    return jsonify({"success": 1, "entries": entries})
Ejemplo n.º 9
0
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})
Ejemplo n.º 10
0
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)})
Ejemplo n.º 11
0
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})
Ejemplo n.º 12
0
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)})
Ejemplo n.º 13
0
def user_jam_entries(username):
    """Получить список заявок на джем для данного пользователя"""
    user = User.get_or_none(User.username == username)

    if user is None:
        return errors.not_found()

    query = JamEntry.get_user_entries(user)
    limit = max(1, min(int(request.args.get("limit") or 100), 100))
    paginated_query = PaginatedQuery(query, paginate_by=limit)

    entries = [e.to_json() for e in paginated_query.get_object_list()]

    return jsonify({
        "success": 1,
        "entries": entries,
        "meta": {
            "page_count": paginated_query.get_page_count()
        },
    })
Ejemplo n.º 14
0
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()})