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)})
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})
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