Esempio n. 1
0
def new_share_link(request, note_id):
    # Validate note
    current_note = validate_ownership_note(request.user, note_id)

    if request.method != 'POST':
        form = SharableLinkForm()

        # Generate unique code
        unique_code = '%32x' % random.getrandbits(16 * 8)
        entry = SharableLink.objects.filter(code=unique_code)

        while entry.exists():
            unique_code = '%32x' % random.getrandbits(16 * 8)
            entry = SharableLink.objects.filter(code=unique_code)

        form.set_code(unique_code)
    else:
        form = SharableLinkForm(data=request.POST)

        if form.create(note_id):
            return HttpResponseRedirect(reverse('share:share-note', kwargs={'note_id': note_id}))

    # Render
    context = regular_context(request.user)
    context['form'] = form
    context['current_note'] = current_note
    return render(request, 'new_share_link.html', context)
Esempio n. 2
0
def new_share_link(request, note_id):
    # Validate note
    current_note = validate_ownership_note(request.user, note_id)

    if request.method != 'POST':
        form = SharableLinkForm()

        # Generate unique code
        unique_code = '%32x' % random.getrandbits(16 * 8)
        entry = SharableLink.objects.filter(code=unique_code)

        while entry.exists():
            unique_code = '%32x' % random.getrandbits(16 * 8)
            entry = SharableLink.objects.filter(code=unique_code)

        form.set_code(unique_code)
    else:
        form = SharableLinkForm(data=request.POST)

        if form.create(note_id):
            return HttpResponseRedirect(
                reverse('share:share-note', kwargs={'note_id': note_id}))

    # Render
    context = regular_context(request.user)
    context['form'] = form
    context['current_note'] = current_note
    return render(request, 'new_share_link.html', context)
Esempio n. 3
0
def edit_shared_note(request, note_id, code):
    # Validate note
    note, sharable_link = validate_ownership_shared_note(
        request, note_id, code)

    # Create form
    if request.method != 'POST':
        form = SharedNoteForm(data={
            'title': note.title,
            'content': note.content,
            'tags': note.tags
        })
    else:
        form = SharedNoteForm(data=request.POST)

        if form.is_valid() and sharable_link.permissions == 'read+write':
            note.title = form.cleaned_data['title']
            note.content = form.cleaned_data['content']
            note.tags = form.cleaned_data['tags']
            note.save()
            return redirect('home')

    # Render
    context = regular_context(request.user)
    context['form'] = form
    context['current_note'] = note
    context['code'] = code
    return render(request, 'edit_shared_note.html', context)
Esempio n. 4
0
def new_share_link(request, note_id):
    # Validate note
    current_note = validate_ownership_note(request.user, note_id)

    if request.method != 'POST':
        form = SharableLinkForm()

        # Generate unique code
        unique_code = '%32x' % random.getrandbits(16 * 8)
        entry = SharableLink.objects.filter(code=unique_code)

        while entry.exists():
            unique_code = '%32x' % random.getrandbits(16 * 8)
            entry = SharableLink.objects.filter(code=unique_code)

        form.set_code(unique_code)
    else:
        form = SharableLinkForm(data=request.POST)

        if form.is_valid():
            SharableLink.objects.create(
                note_id=note_id,
                code=form.cleaned_data['code'],
                permissions=form.cleaned_data['permissions'],
                expiry_date=form.cleaned_data['expiry_date'])
            return redirect('home')

    # Render
    context = regular_context(request.user)
    context['form'] = form
    context['current_note'] = current_note
    return render(request, 'new_share_link.html', context)
Esempio n. 5
0
def edit_shared_note(request, note_id, code):
    # Validate note
    note, sharable_link = validate_ownership_shared_note(
        request, note_id, code)

    # Create form
    if request.method != 'POST':
        form = SharedNoteForm(data={
            'title': note.title,
            'content': note.content,
            'tags': note.tags
        })
    else:
        form = SharedNoteForm(data=request.POST)

        if form.update(note, sharable_link):
            return HttpResponseRedirect(
                reverse('share:view-note',
                        kwargs={
                            'note_id': note_id,
                            'code': code
                        }))

    # Render
    context = regular_context(request.user)
    context['form'] = form
    context['current_note'] = note
    context['code'] = code
    return render(request, 'edit_shared_note.html', context)
Esempio n. 6
0
def edit_share_link(request, note_id, code):
    # Validate note
    current_note = validate_ownership_note(request.user, note_id)
    link = SharableLink.objects.filter(code=code).filter(note_id=note_id).get()

    if request.method != 'POST':
        form = EditSharableLinkForm(data={
            'permissions': link.permissions,
            'expiry_date': link.expiry_date
        })
    else:
        form = EditSharableLinkForm(data=request.POST)

        if form.is_valid():
            link.permissions = form.cleaned_data['permissions']
            link.expiry_date = form.cleaned_data['expiry_date']
            link.save()
            return redirect('home')

    # Render
    context = regular_context(request.user)
    context['form'] = form
    context['current_note'] = current_note
    context['code'] = code
    return render(request, 'edit_share_link.html', context)
Esempio n. 7
0
def search(request):
    if request.method != 'POST':
        return redirect('trash:trash')

    # Render
    query = request.POST['query']

    context = regular_context(request.user)
    context['query'] = query
    context['notes'] = TrashNote.objects.search(request.user, query)
    return render(request, 'search_trash.html', context)
Esempio n. 8
0
def search(request):
    if request.method != 'POST':
        return redirect('trash:trash')

    # Render
    query = request.POST['query']

    context = regular_context(request.user)
    context['query'] = query
    context['notes'] = TrashNote.objects.search(request.user, query)
    return render(request, 'search_trash.html', context)
Esempio n. 9
0
def view_shared_note(request, note_id, code):
    # Validate note
    note, sharable_link = validate_ownership_shared_note(request, note_id, code)

    # Render
    note.rendered_content = render_markdown(note.content)

    context = regular_context(request.user)
    context['current_note'] = note
    context['code'] = sharable_link.code
    context['permissions'] = sharable_link.permissions
    return render(request, 'view_shared_note.html', context)
Esempio n. 10
0
def delete_share_link(request, note_id, code):
    # Validate note
    note, sharable_link = validate_ownership_shared_note(request, note_id, code, False)

    if request.method == 'POST':
        sharable_link.delete()
        return HttpResponseRedirect(reverse('share:share-note', kwargs={'note_id': note_id}))

    # Render
    context = regular_context(request.user)
    context['current_note'] = note
    context['code'] = code
    return render(request, 'delete_share_link.html', context)
Esempio n. 11
0
def register_view(request):
    if request.method != 'POST':
        form = UserCreationForm()
    else:
        form = UserCreationForm(data=request.POST)

        if form.create(request):
            return redirect('home')

    # Render
    context = regular_context(request.user)
    context['form'] = form
    return render(request, 'register.html', context)
Esempio n. 12
0
def login_view(request):
    if request.method != 'POST':
        form = AuthenticationForm()
    else:
        form = AuthenticationForm(data=request.POST)

        if form.login(request):
            return redirect('home')

    # Render
    context = regular_context(request.user)
    context['form'] = form
    return render(request, 'login.html', context)
Esempio n. 13
0
def view_shared_note(request, note_id, code):
    # Validate note
    note, sharable_link = validate_ownership_shared_note(
        request, note_id, code)

    # Render
    note.rendered_content = render_markdown(note.content)

    context = regular_context(request.user)
    context['current_note'] = note
    context['code'] = sharable_link.code
    context['permissions'] = sharable_link.permissions
    return render(request, 'view_shared_note.html', context)
Esempio n. 14
0
def change_password(request):
    # Source: https://simpleisbetterthancomplex.com/tips/2016/08/04/django-tip-9-password-change-form.html
    if request.method != 'POST':
        form = PasswordChangeForm(request.user)
    else:
        form = PasswordChangeForm(request.user, request.POST)

        if form.update(request):
            return redirect('home')

    # Render
    context = regular_context(request.user)
    context['form'] = form
    return render(request, 'change_password.html', context)
Esempio n. 15
0
def delete_shared_note(request, note_id, code):
    # Validate note
    note, sharable_link = validate_ownership_shared_note(
        request, note_id, code)

    if request.method == 'POST' and sharable_link.permissions == 'read+write':
        note.delete()
        return redirect('home')

    # Render
    context = regular_context(request.user)
    context['current_note'] = note
    context['code'] = code
    return render(request, 'delete_shared_note.html', context)
Esempio n. 16
0
def delete_share_link(request, note_id, code):
    # Validate note
    note, sharable_link = validate_ownership_shared_note(
        request, note_id, code, False)

    if request.method == 'POST':
        sharable_link.delete()
        return redirect('home')

    # Render
    context = regular_context(request.user)
    context['current_note'] = note
    context['code'] = code
    return render(request, 'delete_share_link.html', context)
Esempio n. 17
0
def delete_shared_note(request, note_id, code):
    # Validate note
    current_note, sharable_link = validate_ownership_shared_note(request, note_id, code)

    if request.method == 'POST' and sharable_link.permissions == 'read+write':
        current_note.trash = True
        current_note.save()
        return redirect('home')

    # Render
    context = regular_context(request.user)
    context['current_note'] = current_note
    context['code'] = code
    return render(request, 'delete_shared_note.html', context)
Esempio n. 18
0
def delete_share_link(request, note_id, code):
    # Validate note
    note, sharable_link = validate_ownership_shared_note(
        request, note_id, code, False)

    if request.method == 'POST':
        sharable_link.delete()
        return HttpResponseRedirect(
            reverse('share:share-note', kwargs={'note_id': note_id}))

    # Render
    context = regular_context(request.user)
    context['current_note'] = note
    context['code'] = code
    return render(request, 'delete_share_link.html', context)
Esempio n. 19
0
def share_note(request, note_id):
    # Validate note
    current_note = validate_ownership_note(request.user, note_id)

    # Render
    sharable_links = current_note.sharablelink_set.all()

    for link in sharable_links:
        args = {'note_id': current_note.id, 'code': link.code}
        link.full_url = request.build_absolute_uri(reverse('share:view-note', kwargs=args))

    current_note.rendered_content = render_markdown(current_note.content)

    context = regular_context(request.user)
    context['current_note'] = current_note
    context['sharable_links'] = sharable_links
    return render(request, 'share_note.html', context)
Esempio n. 20
0
def share_note(request, note_id):
    # Validate note
    current_note = validate_ownership_note(request.user, note_id)

    # Render
    sharable_links = current_note.sharablelink_set.all()

    for link in sharable_links:
        args = {'note_id': current_note.id, 'code': link.code}
        link.full_url = request.build_absolute_uri(
            reverse('share:view-note', kwargs=args))

    current_note.rendered_content = render_markdown(current_note.content)

    context = regular_context(request.user)
    context['current_note'] = current_note
    context['sharable_links'] = sharable_links
    return render(request, 'share_note.html', context)
Esempio n. 21
0
def register_view(request):
    if request.method != 'POST':
        form = UserCreationForm()
    else:
        form = UserCreationForm(data=request.POST)

        if form.is_valid():
            new_user = form.save()
            UserProfile.objects.create(user=new_user)
            authed_user = authenticate(username=new_user.username,
                                       password=request.POST['password1'])
            login(request, authed_user)
            return redirect('home')

    # Render
    context = regular_context(request.user)
    context['form'] = form
    return render(request, 'register.html', context)
Esempio n. 22
0
def login_view(request):
    if request.method != 'POST':
        form = AuthenticationForm()
    else:
        form = AuthenticationForm(data=request.POST)

        if form.is_valid():
            data = form.clean()
            authed_user = authenticate(username=data['username'],
                                       password=data['password'])

            # Create user profile if one does not exist (for users made through createsuperuser)
            if not UserProfile.objects.filter(user=authed_user).exists():
                UserProfile.objects.create(user=authed_user)

            login(request, authed_user)
            return redirect('home')

    # Render
    context = regular_context(request.user)
    context['form'] = form
    return render(request, 'login.html', context)
Esempio n. 23
0
def edit_share_link(request, note_id, code):
    # Validate note
    current_note = validate_ownership_note(request.user, note_id)
    link = SharableLink.objects.filter(code=code).filter(note_id=note_id).get()

    if request.method != 'POST':
        form = EditSharableLinkForm(data={
            'permissions': link.permissions,
            'expiry_date': link.expiry_date
        })
    else:
        form = EditSharableLinkForm(data=request.POST)

        if form.update(link):
            return HttpResponseRedirect(reverse('share:share-note', kwargs={'note_id': note_id}))

    # Render
    context = regular_context(request.user)
    context['form'] = form
    context['current_note'] = current_note
    context['code'] = code
    return render(request, 'edit_share_link.html', context)
Esempio n. 24
0
def edit_share_link(request, note_id, code):
    # Validate note
    current_note = validate_ownership_note(request.user, note_id)
    link = SharableLink.objects.filter(code=code).filter(note_id=note_id).get()

    if request.method != 'POST':
        form = EditSharableLinkForm(data={
            'permissions': link.permissions,
            'expiry_date': link.expiry_date
        })
    else:
        form = EditSharableLinkForm(data=request.POST)

        if form.update(link):
            return HttpResponseRedirect(
                reverse('share:share-note', kwargs={'note_id': note_id}))

    # Render
    context = regular_context(request.user)
    context['form'] = form
    context['current_note'] = current_note
    context['code'] = code
    return render(request, 'edit_share_link.html', context)
Esempio n. 25
0
def edit_shared_note(request, note_id, code):
    # Validate note
    note, sharable_link = validate_ownership_shared_note(request, note_id, code)

    # Create form
    if request.method != 'POST':
        form = SharedNoteForm(data={
            'title': note.title,
            'content': note.content,
            'tags': note.tags
        })
    else:
        form = SharedNoteForm(data=request.POST)

        if form.update(note, sharable_link):
            return HttpResponseRedirect(reverse('share:view-note', kwargs={'note_id': note_id, 'code': code}))

    # Render
    context = regular_context(request.user)
    context['form'] = form
    context['current_note'] = note
    context['code'] = code
    return render(request, 'edit_shared_note.html', context)