Esempio n. 1
0
def home(request, page_number=1):
    """Home page"""

    try:
        page_number = int(page_number)
    except ValueError:
        page_number = 1

    notes = Note.objects.filter(author=request.user.id)  # Get all the notes

    paginator = Paginator(notes, 10)  # Split into pages

    if page_number > paginator.num_pages:
        raise Http404

    current_page = paginator.page(page_number)
    prange = page_range(  # Generate page range
        page_number, paginator.num_pages)
    context = {
        "notes": current_page,  # Content of current page
        "current_page_number": page_number,  # Current page number
        "num_pages": paginator.num_pages,  # Number of pages
        "page_range": prange  # Pages that will be displayed
    }
    return render_to_response("home.html", context)  # Render page
Esempio n. 2
0
def manage_notes(request, page_number=1):
    """Manage notes"""

    try:
        page_number = int(page_number)
    except ValueError:  # If page number is not set
        page_number = 1  # Go to the first page

    notes = Note.objects.filter(author=request.user.id)  # Get all the notes
    paginator = Paginator(notes, 10)  # Split notes into pages

    try:  # Page number can be too big
        current_page = paginator.page(page_number)
    except EmptyPage:  # If page number is too big
        raise Http404  # Display '404 Not Found' error

    prange = page_range(  # Generate page range
        page_number, paginator.num_pages)

    context = {
        "notes": current_page,  # Current page content
        "current_page_number": page_number,  # Current page number
        "num_pages": paginator.num_pages,  # Number of pages
        "page_range": prange  # Page range
    }

    return render_to_response("manage.html", context)  # Render page
Esempio n. 3
0
def filter_undone(request, tags="", page_number=1):
    """Filter unchecked todo notes"""

    context = {}
    context["tags"] = tags
    if tags:
        context["tags"] = tags + "/"
    tags = transform_tags_single(tags)

    notes = Note.objects.filter(author=request.user.id,
                                type="t",
                                is_checked=False)
    filtered = []

    for note in notes:
        ntags = note.getTags()
        if tags:
            for tag in tags:
                if tag in ntags:
                    have_tag = True
                else:
                    have_tag = False
                    break
            if have_tag:
                filtered.append(note)
        else:
            filtered.append(note)

    try:
        page_number = int(page_number)
    except ValueError:
        page_number = 1

    paginator = Paginator(filtered, 10)
    filtered_page = paginator.page(page_number)
    prange = page_range(page_number, paginator.num_pages)

    context["filtered"] = filtered_page
    context["current_page_number"] = page_number
    context["num_pages"] = paginator.num_pages
    context["page_range"] = prange
    context["urlprefix"] = "undone"

    return render_to_response("filter.html", context)
Esempio n. 4
0
def find_similiar_notes(request, note_id, page_number=1):
    """Find notes, similiar to some note
	
	@param note_id: ID of the note
	"""

    try:
        page_number = int(page_number)
    except ValueError:
        page_number = 1

    try:
        note = Note.objects.get(id=note_id)
    except ObjectDoesNotExist:
        raise Http404

    notes = Note.objects.filter(author=request.user.id)

    sorted_notes = check_similarity(note, notes)
    paginator = Paginator(sorted_notes, 10)
    try:
        current_page = replace_newlines_sim(paginator.page(page_number))
    except EmptyPage:
        raise Http404

    prange = page_range(page_number, paginator.num_pages)

    context = {
        "note_": note,
        "notes": current_page,
        "current_page_number": page_number,
        "page_range": prange,
        "current_url": "/similiar/%s/" % note_id,
        "num_pages": paginator.num_pages
    }

    return render_to_response("similiar.html",
                              context,
                              context_instance=RequestContext(request))
Esempio n. 5
0
def search(request, query, page_number=1):
    """Search for notes"""

    context = {"errors": [], "q": query}

    # Define error messages
    too_long_error = "Search query cannot contain more than 150 symbols"
    empty_error = "Search query cannot be empty"

    if query:
        query = query.lower()
        query_splitted = query.split()
        context["qs"] = query_splitted

        if len(query) > 150:  # If search query is too long
            context["errors"].append(too_long_error)  # Append error

        try:  # Page number can be invalid
            page_number = int(page_number)  # Convert string to integer
        except TypeError:  # If page number is invalid
            page_number = 1  # Go to first page

    else:  # If search query is empty
        context["errors"].append(empty_error)  # Append error message

    if not context["errors"]:  # If there's no errors
        found = []
        sorted_found = []  # Will contain sorted results later
        notes = Note.objects.filter(author=request.user.id)

        for note in notes:
            passing = False
            for i in query_splitted:
                if i in note.no_html().lower() \
                or i in note.title.lower() or i in note.tags.lower():
                    if not note in found:
                        passing = True
                else:
                    passing = False
                    break

            if passing:
                found.append(note)
                sorted_found.append(  # Place by relevance
                    place_by_relevance(note,
                                       query_splitted,
                                       splitted=True,
                                       lower=True))

        sorted_found.sort()  # Sort notes

        try:  # Page number can be too big
            paginator = Paginator(sorted_found, 10)  # Split content into pages
            found = paginator.page(page_number)
        except EmptyPage:  # If page number is too big
            raise Http404  # Display '404 Not found' error

        prange = page_range(  # Generate page range
            page_number, paginator.num_pages)

        # Pack context
        context["found"] = found  # Set list with found notes
        context["num_pages"] = paginator.num_pages  # Set number of pages
        context["current_page_number"] = page_number  # Set current page number
        context["page_range"] = prange  # Set page range

        return render_to_response("search.html", context)