/
views.py
130 lines (91 loc) · 3.73 KB
/
views.py
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
from django.contrib import messages
from django.http import Http404
from django.shortcuts import redirect
from django.template.defaultfilters import slugify
from django.views.generic import DetailView, ListView
from django.views.generic.edit import CreateView, UpdateView
from braces.views import LoginRequiredMixin
from .forms import CreateScriptForm, NewVersionScriptForm, UpdateScriptForm
from .models import Script, CLIENT_CHOICES
class SubmitScript(LoginRequiredMixin, CreateView):
model = Script
form_class = CreateScriptForm
def form_valid(self, form):
request = self.request
script = form.save(commit=False)
script.slug = slugify(script.title)
script.submitter = request.user
script.save()
# Create a new ScriptSource entry for this script using
# the latest source
script.add_version(form.cleaned_data.get('source'))
messages.success(request, "Score!")
return redirect(script)
class UpdateScript(LoginRequiredMixin, UpdateView):
model = Script
form_class = UpdateScriptForm
def get_object(self):
obj = super(UpdateScript, self).get_object()
# Ensure request.user owns the script
if obj.submitter != self.request.user:
raise Http404()
return obj
def form_valid(self, form):
script = form.save(commit=False)
# We also want to update the script's slug based on the new title
script.slug = slugify(script.title)
script.save()
messages.success(self.request, "Script updated successfully.")
return redirect(self.get_success_url())
class SubmitScriptVersion(LoginRequiredMixin, UpdateView):
model = Script
form_class = NewVersionScriptForm
def get_object(self):
obj = super(SubmitScriptVersion, self).get_object()
# Ensure request.user owns the script
if obj.submitter != self.request.user:
raise Http404()
return obj
def get_form_kwargs(self):
# Normally, the UpdateView mixin would pull out the object
# instance and stick it into the form kwargs. Since this isn't
# really a model form, we'll remove the instance argument
kwargs = super(SubmitScriptVersion, self).get_form_kwargs()
if 'instance' in kwargs:
del kwargs['instance']
return kwargs
def form_valid(self, form):
# If the form is valid, then pull out the script object
# and add a new version
source = form.cleaned_data.get('source')
# Add a new version to the script we're updating
script = self.get_object()
script.add_version(source)
# Flash a message and take them back to the detail view
msg = "Version {0} added. Thanks!"
messages.success(self.request,
msg.format(script.latest_version.version))
return redirect(script)
class ScriptDetail(DetailView):
model = Script
def get(self, *args, **kwargs):
script = self.get_object()
# If the slug in the URL doesn't match the script slug,
# then redirect
if script.slug != kwargs.get('slug'):
return redirect(script, permanent=True)
return super(ScriptDetail, self).get(*args, **kwargs)
class ScriptList(ListView):
model = Script
def get_queryset(self):
qs = Script.objects.order_by('-last_modified')
client = self.kwargs.get('client', None)
if not client:
return qs
# Uppercase the client and see if its in the client list
clients_dict = dict(CLIENT_CHOICES)
if client.upper() not in clients_dict:
raise Http404
if client:
qs = qs.filter(client=client.upper())
return qs