Пример #1
0
def index(page=1):
    per_page = flask.current_app.config["PER_PAGE"]
    number_of_notes = len(list(Note))
    pages = number_of_notes // per_page + (number_of_notes % per_page > 0)

    # TODO: Select category

    form = dict(flask.request.form)
    cids = []
    if "apply_filter" in list(form):
        form.pop("apply_filter")
        cids = [
            int(re.match("category:([0-9]*)", category).group(1))
            for category in list(form)
        ]

    categories = utils.categories(cids=cids)
    keywords = flask.request.form.get("search", "", type=str)
    notes = Note.search(keywords)

    if not notes:
        notes = Note.public()

    nids = []
    for note in notes:
        # TODO: Better solution for deleting all empty notes
        if not note.title and not note.content:
            note.delete_instance()

        # TODO: Improve filtering of notes
        if all([
            NoteCategory.select().where(
                NoteCategory.note == note.id,
                NoteCategory.category == cid,
            ).exists() for cid in cids
        ]):
            nids.append(note.id)

    # Filter notes
    notes = notes.where(Note.id.in_(nids))

    return flask.render_template(
        "index.html",
        notes=notes.paginate(page, per_page),
        page=page,
        pages=pages,
        categories=categories,
        keywords=keywords,
    )
Пример #2
0
 def search(self):
     query = request.args.get('query')
     notes = Note.search(request.args.get('query') or '')
     notes = self.process_query(notes)  # Apply any filters, etc.
     return self.paginated_object_list(notes)