Commit 6f469ea2 authored by Daniel W Bond's avatar Daniel W Bond

added comments, removed duplicated code in CreateFlag

parent c580cbce
...@@ -192,6 +192,7 @@ class ListingPage(View): ...@@ -192,6 +192,7 @@ class ListingPage(View):
if self.request.user.is_authenticated: if self.request.user.is_authenticated:
view = CreateBid.as_view() view = CreateBid.as_view()
return view(request, *args, **kwargs) return view(request, *args, **kwargs)
# the nonauthenticated version cannot post
else: else:
pass pass
...@@ -204,15 +205,17 @@ class CreateFlag(LoginRequiredMixin, CreateView): ...@@ -204,15 +205,17 @@ class CreateFlag(LoginRequiredMixin, CreateView):
context_object_name = 'flag' context_object_name = 'flag'
login_url = 'login' login_url = 'login'
def get(self, request, *args, **kwargs):
me = self.request.user.student
# duplicated code!!! def parse_url_for_listing(self, request):
current_url = self.request.get_full_path() # there's possibly a better way than url parsing, but here we are
current_url = request.get_full_path()
listing_slug = current_url.split('/')[3] listing_slug = current_url.split('/')[3]
# [u'', u'share', u'listing', u'C1s3oD', u'flag'] # [u'', u'share', u'listing', u'C1s3oD', u'flag']
selected_listing = Listing.objects.get(slug=listing_slug) return Listing.objects.get(slug=listing_slug)
def get(self, request, *args, **kwargs):
me = self.request.user.student
selected_listing = self.parse_url_for_listing(self.request)
posting_student = selected_listing.poster posting_student = selected_listing.poster
# can only create a flag if you haven't previously created one # can only create a flag if you haven't previously created one
...@@ -229,23 +232,14 @@ class CreateFlag(LoginRequiredMixin, CreateView): ...@@ -229,23 +232,14 @@ class CreateFlag(LoginRequiredMixin, CreateView):
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
context = super(CreateFlag, self).get_context_data(**kwargs) context = super(CreateFlag, self).get_context_data(**kwargs)
me = self.request.user.student me = self.request.user.student
selected_listing = self.parse_url_for_listing(self.request)
# duplicated code!!!
current_url = self.request.get_full_path()
listing_slug = current_url.split('/')[3]
# [u'', u'share', u'listing', u'C1s3oD', u'flag']
selected_listing = Listing.objects.get(slug=listing_slug)
context['listing'] = selected_listing context['listing'] = selected_listing
return context return context
def form_valid(self, form): def form_valid(self, form):
me = self.request.user.student me = self.request.user.student
selected_listing = self.parse_url_for_listing(self.request)
current_url = self.request.get_full_path()
listing_slug = current_url.split('/')[3]
# [u'', u'share', u'listing', u'C1s3oD', u'flag']
selected_listing = Listing.objects.get(slug=listing_slug)
form.instance.flagger = me form.instance.flagger = me
form.instance.listing = selected_listing form.instance.listing = selected_listing
...@@ -422,9 +416,11 @@ class EditListing(LoginRequiredMixin, FormValidMessageMixin, UpdateView): ...@@ -422,9 +416,11 @@ class EditListing(LoginRequiredMixin, FormValidMessageMixin, UpdateView):
me = self.request.user.student me = self.request.user.student
posting_student = self.get_object().poster posting_student = self.get_object().poster
# can't edit a bid on a cancelled listing
if (self.get_object().cancelled is True): if (self.get_object().cancelled is True):
raise Http404 raise Http404
# can only edit your own bids
if not(posting_student == me): if not(posting_student == me):
return HttpResponseForbidden() return HttpResponseForbidden()
else: else:
...@@ -606,6 +602,7 @@ class CancelListing(LoginRequiredMixin, FormValidMessageMixin, UpdateView): ...@@ -606,6 +602,7 @@ class CancelListing(LoginRequiredMixin, FormValidMessageMixin, UpdateView):
if (self.get_object().cancelled is True): if (self.get_object().cancelled is True):
raise Http404 raise Http404
# only you can cancel your own listing
if not(posting_student == me): if not(posting_student == me):
return HttpResponseForbidden() return HttpResponseForbidden()
else: else:
...@@ -637,6 +634,7 @@ class ReopenListing(LoginRequiredMixin, FormValidMessageMixin, UpdateView): ...@@ -637,6 +634,7 @@ class ReopenListing(LoginRequiredMixin, FormValidMessageMixin, UpdateView):
if (self.get_object().cancelled is False): if (self.get_object().cancelled is False):
raise Http404 raise Http404
# only you can re-open your own listing
if not(posting_student == me): if not(posting_student == me):
return HttpResponseForbidden() return HttpResponseForbidden()
else: else:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment