Commit 9d0207e0 authored by Bengfort's avatar Bengfort
Browse files

Merge branch '1978-use-non-ascending-ids-scheduler' into 'master'

1978 use non ascending ids scheduler

See merge request !13
parents ef50e5c7 e312cb6f
Pipeline #9069 passed with stages
in 3 minutes and 12 seconds
# Generated by Django 3.1.6 on 2021-02-22 14:39
from django.db import migrations, models
import uuid
class Migration(migrations.Migration):
dependencies = [
('main', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='schedule',
name='uuid',
field=models.UUIDField(default=uuid.uuid4, editable=False, unique=True),
),
]
......@@ -17,6 +17,7 @@
# <http://www.gnu.org/licenses/>.
import datetime
import uuid
from django.db import models
from django.utils import formats
......@@ -24,6 +25,7 @@ from django.utils.translation import gettext_lazy as _
class Schedule(models.Model):
uuid = models.UUIDField(default=uuid.uuid4, unique=True, editable=False)
title = models.CharField(_('Title'), max_length=254)
def __str__(self):
......
......@@ -69,7 +69,7 @@
<div class="form-group">
<label for="id_schedule_id">{% translate 'Schedule ID' %}</label>
<div class="input-group">
<input id="id_schedule_id" class="form-control" value="{{ object.pk }}" readonly>
<input id="id_schedule_id" class="form-control" value="{{ object.uuid }}" readonly>
<div class="input-group-append">
<button type="button" class="btn btn-outline-secondary" tabindex="-1" data-clipboard="#id_schedule_id" data-animation="success">{% translate 'Copy' %}</button>
</div>
......
......@@ -53,7 +53,7 @@ class ScheduleFormMixin(LoginRequiredMixin):
form_class = ScheduleForm
def get_success_url(self):
return reverse('schedule-update', args=[self.object.pk])
return reverse('schedule-update', args=[self.object.uuid])
def form_valid(self, form, *args):
messages.success(self.request, _('Data has been saved.'))
......@@ -65,11 +65,14 @@ class ScheduleCreateView(ScheduleFormMixin, CreateView):
class ScheduleUpdateView(ScheduleFormMixin, UpdateView):
pass
slug_field = 'uuid'
slug_url_kwarg = 'uuid'
class ScheduleDeleteView(LoginRequiredMixin, DeleteView):
model = Schedule
slug_field = 'uuid'
slug_url_kwarg = 'uuid'
def get_success_url(self):
return reverse('index')
......@@ -85,7 +88,7 @@ class InvitationUpdateView(UpdateView):
return get_object_or_404(Invitation, **self.kwargs)
def get_success_url(self):
return reverse('invitation', args=[self.object.schedule.pk, self.object.token])
return reverse('invitation', args=[self.object.schedule.uuid, self.object.token])
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
......@@ -150,6 +153,9 @@ class InvitationApiView(View):
})
def put(self, request, *args, **kwargs):
if 'schedule__uuid' in kwargs:
schedule = get_object_or_404(Schedule, uuid=kwargs.pop('schedule__uuid'))
kwargs['schedule_id'] = schedule.id
invitation, _ = Invitation.objects.get_or_create(**kwargs)
return HttpResponse(status=204)
......
......@@ -7,7 +7,7 @@ from .main.models import Schedule
from .main.models import Invitation
class TestInvitationApiView(TestCase):
class TestLegacyInvitationApiView(TestCase):
def setUp(self):
self.client.defaults['HTTP_AUTHORIZATION'] = 'token ' + settings.API_TOKEN
......@@ -30,3 +30,28 @@ class TestInvitationApiView(TestCase):
response = self.client.delete(url)
self.assertEqual(response.status_code, 204)
self.assertFalse(Invitation.objects.filter(token=invitation.token).exists())
class TestInvitationApiView(TestCase):
def setUp(self):
self.client.defaults['HTTP_AUTHORIZATION'] = 'token ' + settings.API_TOKEN
def test_get(self):
invitation = baker.make(Invitation)
url = '/api/{}/{}/'.format(invitation.schedule.uuid, invitation.token)
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
def test_put(self):
schedule = baker.make(Schedule)
url = '/api/{}/{}/'.format(schedule.uuid, 'sometoken')
response = self.client.put(url)
self.assertEqual(response.status_code, 204)
self.assertTrue(Invitation.objects.filter(token='sometoken').exists())
def test_delete(self):
invitation = baker.make(Invitation)
url = '/api/{}/{}/'.format(invitation.schedule.uuid, invitation.token)
response = self.client.delete(url)
self.assertEqual(response.status_code, 204)
self.assertFalse(Invitation.objects.filter(token=invitation.token).exists())
......@@ -35,16 +35,28 @@ from .main.views import ScheduleUpdateView
urlpatterns = [
path('', ScheduleListView.as_view(), name='index'),
path('new/', ScheduleCreateView.as_view(), name='schedule-create'),
path('<int:pk>/', ScheduleUpdateView.as_view(), name='schedule-update'),
path('<int:pk>/delete/', ScheduleDeleteView.as_view(), name='schedule-delete'),
path('<int:pk>/', ScheduleUpdateView.as_view(), name='schedule-update-legacy'),
path('<uuid:uuid>/', ScheduleUpdateView.as_view(), name='schedule-update'),
path('<int:pk>/delete/', ScheduleDeleteView.as_view(), name='schedule-delete-legacy'),
path('<uuid:uuid>/delete/', ScheduleDeleteView.as_view(), name='schedule-delete'),
path(
'invitations/<int:schedule_id>/<token>/',
InvitationUpdateView.as_view(),
name='invitation-legacy',
),
path(
'invitations/<uuid:schedule__uuid>/<token>/',
InvitationUpdateView.as_view(),
name='invitation',
),
path(
'api/<int:schedule_id>/<token>/',
InvitationApiView.as_view(),
name='api-invitation-legacy',
),
path(
'api/<uuid:schedule__uuid>/<token>/',
InvitationApiView.as_view(),
name='api-invitation',
),
path('login/', LoginView.as_view(), name='login'),
......
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