Unverified Commit 9c022931 authored by David Haynes's avatar David Haynes
Browse files

Finish first pass on editing links

- Can edit short, target, and expiration date
- Get around the short  PK by just making a copy and applying changes and then
saving it
parent a6205fc3
Pipeline #1178 passed with stage
in 1 minute and 2 seconds
...@@ -222,11 +222,16 @@ def edit(request, short): ...@@ -222,11 +222,16 @@ def edit(request, short):
# request # request
url_form = URLForm(request.POST, host=request.META.get('HTTP_HOST')) url_form = URLForm(request.POST, host=request.META.get('HTTP_HOST'))
# Make a copy of the old URL
copy = url
# Remove the old one
url.delete()
# Django will check the form to make sure it's valid # Django will check the form to make sure it's valid
if url_form.is_valid(): if url_form.is_valid():
# If the short changed then we need to create a new object and # If the short changed then we need to create a new object and
# migrate some data over # migrate some data over
if url_form.cleaned_data.get('short').strip() != url.short: if url_form.cleaned_data.get('short').strip() != copy.short:
# Parse the form and create a new URL object # Parse the form and create a new URL object
res = post(request, url_form) res = post(request, url_form)
...@@ -237,12 +242,9 @@ def edit(request, short): ...@@ -237,12 +242,9 @@ def edit(request, short):
# We can procede with the editing process # We can procede with the editing process
else: else:
# Migrate clicks data # Migrate clicks data
res.clicks = url.clicks res.clicks = copy.clicks
res.qrclicks = url.clicks res.qrclicks = copy.clicks
res.socialclicks = url.clicks res.socialclicks = copy.clicks
# Remove the old one
url.delete()
# Save the new URL # Save the new URL
res.save() res.save()
...@@ -252,9 +254,9 @@ def edit(request, short): ...@@ -252,9 +254,9 @@ def edit(request, short):
# The short was not edited and thus, we can directly edit the url # The short was not edited and thus, we can directly edit the url
else: else:
if url_form.cleaned_data.get('target').strip() != url.target: if url_form.cleaned_data.get('target').strip() != copy.target:
url.target = url_form.cleaned_data.get('target').strip() copy.target = url_form.cleaned_data.get('target').strip()
url.save() copy.save()
# Grab the expiration field value. It's currently an unsable # Grab the expiration field value. It's currently an unsable
# string value, so we need to parse it into a datetime object # string value, so we need to parse it into a datetime object
...@@ -273,12 +275,12 @@ def edit(request, short): ...@@ -273,12 +275,12 @@ def edit(request, short):
else: else:
pass # leave the field NULL pass # leave the field NULL
if edited_expires != url.expires: if edited_expires != copy.expires:
url.expires = edited_expires copy.expires = edited_expires
url.save() copy.save()
# Redirect to the shiny new *edited URL # Redirect to the shiny new *edited URL
return redirect('view', res.short) return redirect('view', copy.short)
# Else, there is an error, redisplay the form with the validation errors # Else, there is an error, redisplay the form with the validation errors
else: else:
...@@ -308,9 +310,6 @@ def edit(request, short): ...@@ -308,9 +310,6 @@ def edit(request, short):
return render(request, 'core/edit_link.html', { return render(request, 'core/edit_link.html', {
'form': url_form 'form': url_form
}) })
# redirect to my_links
# return redirect('my_links')
else: else:
# do not allow them to edit # do not allow them to edit
raise PermissionDenied() raise PermissionDenied()
......
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