Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[EVENEMENT] restaurer les evenements sans javascript #433

Merged
merged 8 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions lacommunaute/event/models.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
from django.conf import settings
from django.db import models
from django.db.models.functions import TruncMonth
from django.utils import timezone
from machina.models.abstract_models import DatedModel


class CurrentUpcomingManager(models.Manager):
def get_queryset(self):
return super().get_queryset().filter(date__gte=TruncMonth(timezone.now()))


class Event(DatedModel):
name = models.CharField(max_length=100, verbose_name="Nom")
date = models.DateField(verbose_name="Date de début", help_text="au format JJ/MM/YYYY")
Expand All @@ -27,7 +20,6 @@ class Event(DatedModel):
)

objects = models.Manager()
current_and_upcomings = CurrentUpcomingManager()

class Meta:
verbose_name = "Event"
Expand Down
21 changes: 0 additions & 21 deletions lacommunaute/event/tests/tests_model.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,7 @@
from dateutil.relativedelta import relativedelta
from django.db import IntegrityError
from django.test import TestCase
from django.utils import timezone

from lacommunaute.event.factories import EventFactory
from lacommunaute.event.models import Event


class EventCurrentUpcomingManagerTest(TestCase):
def test_manager(self):
now = timezone.now()

EventFactory(date=now)

old_event = EventFactory(date=now - relativedelta(months=1))

# upcomming event
EventFactory(date=now + relativedelta(months=1))

# Event at the beginning of the current month
EventFactory(date=now.replace(now.year, now.month, 1))

self.assertEqual(Event.current_and_upcomings.count(), 3)
self.assertNotIn(old_event, Event.current_and_upcomings.all())


class EventModelTest(TestCase):
Expand Down
67 changes: 43 additions & 24 deletions lacommunaute/event/tests/tests_views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import json
from datetime import datetime, timedelta

from dateutil.relativedelta import relativedelta
Expand Down Expand Up @@ -207,30 +206,50 @@ def test_list_my_events(self):
self.assertNotIn(not_myevent, response.context_data["object_list"])


class calendar_data_test(TestCase):
def test_json_response(self):
class EventDetailViewTest(TestCase):
def test_detail_view(self):
event = EventFactory()
items = [
{
"id": event.id,
"name": event.name,
"color": 1,
"location": event.location,
"description": event.description,
"poster_id": event.poster.id,
"time": event.time.strftime("%H:%M:%S"),
"year": event.date.year,
"month": event.date.month,
"day": event.date.day,
"duration": 1,
}
]
response = self.client.get(reverse("event:data_source"))
self.assertEqual(json.loads(response.content)["items"], items)
response = self.client.get(reverse("event:detail", kwargs={"pk": event.pk}))
self.assertContains(response, event.name, status_code=200)


class calendar_test(TestCase):
def test_template(self):
response = self.client.get(reverse("event:calendar"))
self.assertTemplateUsed(response, "event/event_calendar.html")
class EventMonthArchiveViewTest(TestCase):
def test_view_wo_args(self):
event = EventFactory(date=timezone.now())
response = self.client.get(reverse("event:current"))
self.assertContains(response, event.name, status_code=200)
self.assertContains(response, reverse("event:detail", kwargs={"pk": event.pk}))
self.assertContains(response, reverse("event:create"))

def test_view_with_args(self):
event = EventFactory(date=timezone.now() + relativedelta(months=1))
response = self.client.get(reverse("event:month", kwargs={"year": event.date.year, "month": event.date.month}))
self.assertContains(response, event.name, status_code=200)

def test_navbar(self):
event = EventFactory(date=timezone.now())
event_in_the_future = EventFactory(date=event.date + relativedelta(months=1))
event_in_the_past = EventFactory(date=event.date - relativedelta(months=1))
response = self.client.get(reverse("event:current"))
self.assertContains(
response,
reverse(
"event:month",
kwargs={
"year": event_in_the_past.date.year,
"month": f"{event_in_the_past.date.month:02d}",
},
),
status_code=200,
)
self.assertContains(
response,
reverse(
"event:month",
kwargs={
"year": event_in_the_future.date.year,
"month": f"{event_in_the_future.date.month:02d}",
},
),
status_code=200,
)
9 changes: 5 additions & 4 deletions lacommunaute/event/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,22 @@
from lacommunaute.event.views import (
EventCreateView,
EventDeleteView,
EventDetailView,
EventListView,
EventMonthArchiveView,
EventUpdateView,
calendar,
calendar_data,
)


app_name = "event"


urlpatterns = [
path("", calendar, name="calendar"),
path("create/", EventCreateView.as_view(), name="create"),
path("<int:pk>/", EventDetailView.as_view(), name="detail"),
path("<int:pk>/update/", EventUpdateView.as_view(), name="update"),
path("<int:pk>/delete/", EventDeleteView.as_view(), name="delete"),
path("myevents/", EventListView.as_view(), name="myevents"),
path("events/events.json", calendar_data, name="data_source"),
path("events/<int:year>/<int:month>/", EventMonthArchiveView.as_view(), name="month"),
path("events/", EventMonthArchiveView.as_view(), name="current"),
]
68 changes: 29 additions & 39 deletions lacommunaute/event/views.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import logging
from datetime import datetime

from django.contrib.auth.mixins import LoginRequiredMixin
from django.core.exceptions import PermissionDenied
from django.db.models import F
from django.db.models.functions import ExtractDay, ExtractMonth, ExtractYear
from django.http import JsonResponse
from django.template.response import TemplateResponse
from django.http import Http404
from django.urls import reverse
from django.views.generic.dates import MonthArchiveView
from django.views.generic.detail import DetailView
from django.views.generic.edit import CreateView, DeleteView, UpdateView
from django.views.generic.list import ListView

Expand Down Expand Up @@ -65,38 +65,28 @@ def get_queryset(self):
return Event.objects.filter(poster=self.request.user)


# TODO vincentporte : factoriser les EventXXXView


def calendar_data(request):
data = {
"items": list(
Event.current_and_upcomings.annotate(
year=ExtractYear("date"),
month=ExtractMonth("date"),
day=ExtractDay("date"),
duration=ExtractDay(F("end_date") - F("date")) + 1,
).values(
"id",
"name",
"color",
"location",
"description",
"poster_id",
"year",
"month",
"day",
"time",
"duration",
)
)
}
return JsonResponse(data)


# TODO vincentporte : supprimer ce pseudo endpoint au profit du passage en context des données
# dans la méthode calendar (refactor js)


def calendar(request):
return TemplateResponse(request, "event/event_calendar.html")
class EventDetailView(DetailView):
model = Event
template_name = "event/event_detail.html"


class EventMonthArchiveView(MonthArchiveView):
allow_future = True
date_field = "date"
queryset = Event.objects.all()
month_format = "%m"
year_format = "%Y"

def get_month(self):
try:
month = super().get_month()
except Http404:
month = datetime.now().strftime(self.get_month_format())
return month

def get_year(self):
try:
year = super().get_year()
except Http404:
year = datetime.now().strftime(self.get_year_format())
return year
Loading