-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviews.py
More file actions
94 lines (73 loc) · 2.78 KB
/
Copy pathviews.py
File metadata and controls
94 lines (73 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
from django.shortcuts import render, redirect
from django.http import HttpResponse
from django.contrib.auth.models import User
from django.contrib.auth import authenticate, login, logout
from django.core.exceptions import PermissionDenied
from .models import Link
# Create your views here.
def index_view(request):
context = {
'link_list': Link.objects.all().order_by('short_link'),
}
return render(request, "links/index.html", context)
def forward_view(request, short_link):
try:
link = Link.objects.get(short_link=short_link)
return redirect(link.long_link, pernament=True)
except Link.DoesNotExist:
return redirect(f'{short_link}/edit', pernament=False)
return HttpResponse(short_link)
def edit_view(request, short_link):
long_link = "#!"
link_owner = None
try:
link = Link.objects.get(short_link=short_link)
long_link = link.long_link
link_owner = link.owner
except Link.DoesNotExist:
None
context = {
'short_link': short_link,
'long_link': long_link,
}
if link_owner is None or link_owner == request.user:
return render(request, 'links/edit.html', context)
else:
raise PermissionDenied
def set_link_view(request, short_link):
if 'long_link' not in request.POST.keys():
return HttpResponse("No link provided")
user = None # null user means public link (editable by anyone)
if request.user.is_authenticated:
user = request.user
# By default, links are set to public. If the 'anonymous' tag in the POST
# data is set to anything except for 'true', it will become a private link.
# Private links are defined as visible to everyone but only editable by the
# user who created it
anonymous = True
if 'anonymous' in request.POST.keys() and request.user.is_authenticated:
anonymous = (request.POST['anonymous'] == 'true')
long_link = request.POST['long_link']
try:
link = Link.objects.get(short_link=short_link)
# Must have permissions
if link.owner is None or link.owner == user:
link.long_link = long_link
link.save()
else:
raise PermissionDenied
except Link.DoesNotExist:
link = Link(short_link=short_link, long_link=long_link, owner=user)
link.save()
return redirect(long_link, pernament=True)
def login_view(request):
return render(request, "links/login.html")
def logout_view(request):
logout(request)
return redirect('index')
def auth_view(request):
if 'username' not in request.POST or 'password' not in request.POST:
return HttpResponse("Missing username or password")
username = request.POST['username']
password = request.POST['password']
return redirect('index')