Add form drafts API (#52)
This commit is contained in:
parent
0317535c11
commit
08bb577e37
@ -1,8 +1,10 @@
|
||||
from django.contrib import admin
|
||||
from leaflet.admin import LeafletGeoAdmin
|
||||
|
||||
from .models import CaseStudy
|
||||
from .models import CaseStudy, CaseStudyDraft
|
||||
|
||||
class CaseStudyDraftAdmin(admin.ModelAdmin):
|
||||
model = CaseStudyDraft
|
||||
|
||||
class CaseStudyAdmin(LeafletGeoAdmin):
|
||||
list_display = ('id', 'date_created', 'entry_name', 'approved')
|
||||
@ -31,3 +33,5 @@ class CaseStudyAdmin(LeafletGeoAdmin):
|
||||
unapprove.short_description = "Un-approve selected case studies"
|
||||
|
||||
admin.site.register(CaseStudy, CaseStudyAdmin)
|
||||
|
||||
admin.site.register(CaseStudyDraft, CaseStudyDraftAdmin)
|
||||
|
26
apps/map/migrations/0053_casestudydraft.py
Normal file
26
apps/map/migrations/0053_casestudydraft.py
Normal file
@ -0,0 +1,26 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Generated by Django 1.11.6 on 2018-04-12 06:50
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
('map', '0052_auto_20180412_0647'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='CaseStudyDraft',
|
||||
fields=[
|
||||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('data', models.TextField()),
|
||||
('author', models.ForeignKey(editable=False, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
|
||||
],
|
||||
),
|
||||
]
|
@ -12,6 +12,15 @@ from phonenumber_field.modelfields import PhoneNumberField
|
||||
from . import validators
|
||||
|
||||
|
||||
class CaseStudyDraft(models.Model):
|
||||
author = models.ForeignKey(
|
||||
User,
|
||||
on_delete=models.CASCADE
|
||||
)
|
||||
|
||||
data = models.TextField()
|
||||
|
||||
|
||||
class Shapefile(models.Model):
|
||||
file = models.FileField(
|
||||
upload_to='shapefiles/',
|
||||
|
@ -2,6 +2,79 @@ from django.test import TestCase
|
||||
|
||||
from .models import CaseStudy
|
||||
|
||||
from django.contrib.auth.models import User
|
||||
from django.urls import reverse
|
||||
from .models import CaseStudyDraft
|
||||
|
||||
|
||||
class CaseStudyDraftAPITests(TestCase):
|
||||
|
||||
test_user = 'testuser1'
|
||||
test_pass = '12345'
|
||||
|
||||
def setUp(self):
|
||||
test_user1 = User.objects.create_user(username=self.test_user, password=self.test_pass)
|
||||
test_user1.save()
|
||||
|
||||
def login(self):
|
||||
return self.client.login(username=self.test_user, password=self.test_pass)
|
||||
|
||||
def test_get_not_logged_in(self):
|
||||
response = self.client.get(reverse('drafts'))
|
||||
self.assertEqual(response.status_code, 403)
|
||||
|
||||
def test_put_not_logged_in(self):
|
||||
response = self.client.put(reverse('drafts'))
|
||||
self.assertEqual(response.status_code, 403)
|
||||
|
||||
def test_delete_not_logged_in(self):
|
||||
response = self.client.delete(reverse('drafts'))
|
||||
self.assertEqual(response.status_code, 403)
|
||||
|
||||
def test_get_and_put(self):
|
||||
login = self.login()
|
||||
|
||||
# No draft should currently exist
|
||||
response = self.client.get(reverse('drafts'))
|
||||
self.assertEqual(response.status_code, 404)
|
||||
self.assertEqual(response.content, b"")
|
||||
|
||||
# Putting a draft should succeed
|
||||
response = self.client.put(reverse('drafts'), '{"test":1}')
|
||||
self.assertEqual(response.status_code, 201)
|
||||
|
||||
# We should get it back
|
||||
response = self.client.get(reverse('drafts'))
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response['Content-Type'], "application/json")
|
||||
self.assertEqual(response.content, b'{"test":1}')
|
||||
|
||||
# Updating it should succeed
|
||||
response = self.client.put(reverse('drafts'), '{"test":2}')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
# We should get the update back
|
||||
response = self.client.get(reverse('drafts'))
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response['Content-Type'], "application/json")
|
||||
self.assertEqual(response.content, b'{"test":2}')
|
||||
|
||||
def test_delete(self):
|
||||
login = self.login()
|
||||
|
||||
# Create a draft
|
||||
response = self.client.put(reverse('drafts'), '{"test":1}')
|
||||
self.assertEqual(response.status_code, 201)
|
||||
|
||||
# Delete it again
|
||||
response = self.client.delete(reverse('drafts'))
|
||||
self.assertEqual(response.status_code, 204)
|
||||
|
||||
# Check it's not there
|
||||
response = self.client.get(reverse('drafts'))
|
||||
self.assertEqual(response.status_code, 404)
|
||||
|
||||
|
||||
class CaseStudyTests(TestCase):
|
||||
|
||||
def test_get_renewable_generation_detail_when_empty(self):
|
||||
|
@ -13,6 +13,7 @@ urlpatterns = [
|
||||
url(r'^case-study/create/short/?$', views.ShortForm.as_view(), name='short-form'),
|
||||
url(r'^case-study/create/long/?$', views.LongForm.as_view(), name='long-form'),
|
||||
url(r'^case-study/create/success/?$', views.FormSuccess.as_view(), name='form-success'),
|
||||
url(r'^case-study/draft/?$', views.Drafts.as_view(), name='drafts'),
|
||||
url(r'^case-study/(?P<slug>[-\w]+)/?$', views.CaseStudyDetail.as_view(), name='detail'),
|
||||
url(r'^map/?$', views.Map.as_view(), name='map')
|
||||
]
|
||||
|
@ -1,13 +1,16 @@
|
||||
from django.core.mail import send_mail
|
||||
from django.conf import settings
|
||||
from django.views import View
|
||||
from django.views.generic import DetailView
|
||||
from django.views.generic.base import TemplateView
|
||||
from django.views.generic.edit import CreateView
|
||||
from django.http import Http404, HttpResponse
|
||||
from django.contrib.auth.mixins import LoginRequiredMixin
|
||||
from django.urls import reverse
|
||||
from .models import CaseStudy
|
||||
from .models import CaseStudy, CaseStudyDraft
|
||||
from .forms import ShortCaseStudyForm, LongCaseStudyForm
|
||||
|
||||
|
||||
NOTIFY_MESSAGE = """
|
||||
Hello,
|
||||
|
||||
@ -72,3 +75,48 @@ class CaseStudyDetail(DetailView):
|
||||
template_name = "map/detail.html"
|
||||
model = CaseStudy
|
||||
context_object_name = "case_study"
|
||||
|
||||
|
||||
class Drafts(View):
|
||||
"""Retrieve or save a draft."""
|
||||
|
||||
def get_object(self, request):
|
||||
try:
|
||||
return CaseStudyDraft.objects.get(author=request.user)
|
||||
except:
|
||||
return None
|
||||
|
||||
def get(self, request):
|
||||
if not request.user.is_authenticated:
|
||||
return HttpResponse(status=403) # Forbidden
|
||||
|
||||
draft = self.get_object(request)
|
||||
if draft == None:
|
||||
return HttpResponse(status=404) # Not Found
|
||||
else:
|
||||
return HttpResponse(draft.data, content_type="application/json")
|
||||
|
||||
def put(self, request):
|
||||
if not request.user.is_authenticated:
|
||||
return HttpResponse(status=403) # Forbidden
|
||||
|
||||
# Find an existing object is there is one
|
||||
draft = self.get_object(request)
|
||||
if draft == None:
|
||||
# If there isn't, create a new draft...
|
||||
draft = CaseStudyDraft(author=request.user, data=request.body)
|
||||
draft.save()
|
||||
return HttpResponse(status=201) # Created
|
||||
else:
|
||||
draft.data = request.body
|
||||
draft.save()
|
||||
return HttpResponse(status=200) # OK
|
||||
|
||||
def delete(self, request):
|
||||
if not request.user.is_authenticated:
|
||||
return HttpResponse(status=403) # Forbidden
|
||||
|
||||
draft = self.get_object(request)
|
||||
if draft != None:
|
||||
draft.delete()
|
||||
return HttpResponse(status=204)
|
||||
|
Loading…
Reference in New Issue
Block a user