Skip to content

Commit

Permalink
draft wip for prereview
Browse files Browse the repository at this point in the history
WIP: update events forms & lists

WIP: update events forms & lists

WIP: update events forms & lists

cleanup it once
  • Loading branch information
vincentporte committed Oct 2, 2023
1 parent 81dea2d commit 432dd0b
Show file tree
Hide file tree
Showing 39 changed files with 442 additions and 1,912 deletions.
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
61 changes: 37 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,44 @@ 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": event_in_the_past.date.month}, # format "2f"
),
status_code=200,
)
self.assertContains(
response,
reverse(
"event:month",
kwargs={"year": event_in_the_future.date.year, "month": event_in_the_future.date.month},
),
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"),
]
67 changes: 28 additions & 39 deletions lacommunaute/event/views.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
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.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 +64,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 Exception:
month = datetime.now().strftime(self.get_month_format())
return month

def get_year(self):
try:
year = super().get_year()
except Exception:
year = datetime.now().strftime(self.get_year_format())
return year
161 changes: 0 additions & 161 deletions lacommunaute/static/vendor/tiva-calendar/css/calendar.css

This file was deleted.

Loading

0 comments on commit 432dd0b

Please sign in to comment.