Skip to content

Commit

Permalink
Add /visits/cal.ics calendar feed
Browse files Browse the repository at this point in the history
By default, it returns a calendar for all visits. It is also filterable
by organization via `/visits/cal.ics?organization=<org_uuid>`

Separately, we'll need to link to both of these things from the
appropriate place.
  • Loading branch information
rcoh committed Jun 18, 2020
1 parent 7a05ab7 commit baeadba
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 72 deletions.
2 changes: 1 addition & 1 deletion apps/reporting/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Meta:
class OrganizationSerializer(serializers.ModelSerializer):
class Meta:
model = Organization
fields = ["name"]
fields = ["name", "id"]


class EncampmentSerializer(serializers.ModelSerializer):
Expand Down
10 changes: 1 addition & 9 deletions apps/reporting/urls.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
from django.urls import include
from django.urls import path
from rest_framework.routers import DefaultRouter

from apps.reporting import views

# Create a router and register our viewsets with it.

router = DefaultRouter()
router.register(r"visits", views.ReportViewSet)
router.register(r"organizations", views.OrganizationViewSet)
router.register(r"encampments", views.EncampmentViewSet)

urlpatterns = [
# API
path("api/", include(router.urls)),
# Encampments
path("", views.EncampmentListView.as_view(), name="encampment-list"),
path(
Expand All @@ -37,4 +28,5 @@
path("tasks/<str:pk>/complete", views.CompleteTask.as_view(), name="task-complete"),
# Visits
path("visits/", views.ScheduleVisitCreateView.as_view(), name="visit-create"),
path("visits/cal.ics", views.ScheduledVisitEventFeed()),
]
68 changes: 25 additions & 43 deletions apps/reporting/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
from django.views.generic.detail import SingleObjectMixin
from django.views.generic.edit import BaseCreateView
from django_filters.rest_framework import DjangoFilterBackend
from django_ical.views import ICalFeed
from rest_framework import viewsets
from stronghold.decorators import public

from apps.reporting.forms import ReportForm
from apps.reporting.forms import ScheduleVisitForm
Expand Down Expand Up @@ -142,6 +144,29 @@ def get_form(self, form_class=None):
return form


@public
class ScheduledVisitEventFeed(ICalFeed):
def get_object(self, request, *args, **kwargs):
org_id = request.GET.get("organization")
if org_id:
return ScheduledVisit.objects.filter(organization=org_id)
else:
return ScheduledVisit.objects.all()

def items(self, events):
return events.order_by("-date")

def item_title(self, item: ScheduledVisit):
return f"{item.encampment.name} <-> {item.organization.name}"

def item_description(self, item):
# TODO: link to encampment & add a location attribute
return f"{item.organization.name} visit to {item.encampment.name}"

def item_start_datetime(self, item):
return item.date


class ReportListView(ReportingBaseView, ListView):
model = Report

Expand All @@ -168,49 +193,6 @@ def get_form(self, form_class=None):
return form


# # form = super().get_form(form_class=form_class)
# print(form.errors)
# form.fields["date"].widget = date_picker()
# form.fields["education_provided"] = ChoiceField(
# choices=[('', 'Choose one'), ("verbal", "Verbal"), ("flyer", "Flyer"), ("none", "None")], initial=None)
# form.fields["occupancy"].widget = self._occupancy_field()
# for _, field in form.fields.items():
# if isinstance(field, ChoiceField):
# field.widget.attrs["class"] = "field-select"
# field.empty_label = "Choose one"
# elif isinstance(field, IntegerField):
# field.widget.attrs["class"] = "field-number"
# field.widget.attrs["placeholder"] = "Enter a number"
# else:
# field.widget.attrs["class"] = "field-input"
# field.widget.attrs["placeholder"] = "Enter details"

# form.fields["supplies_delivered"].widget.attrs['placeholder'] = "Enter details or leave blank"
# form.fields["food_delivered"].widget.attrs['placeholder'] = "Enter details or leave blank"

# preset_encampment = self.request.GET.get("encampment")
# if preset_encampment:
# form.fields["encampment"].initial = preset_encampment
# return form

# fields = [
# "date",
# "encampment",
# "type_of_setup",
# "education_provided",
# #"recorded_location",
# "performed_by",
# "supplies_delivered",
# "food_delivered",
# "occupancy",
# "talked_to",
# "assessed",
# "assessed_asymptomatic",
# "needs",
# "notes",
# ]


# API views. Maybe delete.
class ReportViewSet(viewsets.ModelViewSet):
queryset = Report.objects.all()
Expand Down
19 changes: 0 additions & 19 deletions config/urls.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,9 @@
from django.conf.urls import url
from django.contrib import admin
from django.urls import include
from django.urls import path
from django.views.generic import TemplateView
from rest_framework.schemas import get_schema_view

urlpatterns = [
path("", include("apps.reporting.urls")),
path("admax/", admin.site.urls),
path("accounts/", include("allauth.urls")),
url(r"^api-auth/", include("rest_framework.urls")),
path(
"openapi",
get_schema_view(
title="Your Project", description="API for all things …", version="1.0.0"
),
name="openapi-schema",
),
path(
"swagger-ui/",
TemplateView.as_view(
template_name="swagger-ui.html",
extra_context={"schema_url": "openapi-schema"},
),
name="swagger-ui",
),
]
1 change: 1 addition & 0 deletions reqs/common.base
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ django-allauth==0.42.0
django-stronghold>=0.4.0,<0.5
django-inline-svg>=0.1.1,<0.2
humanize>=2.4.0,<2.5
django-ical>=1.7.1,<1.8
3 changes: 3 additions & 0 deletions reqs/dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ django-allauth==0.42.0
django-extensions==2.2.9
django-extra-fields==2.0.5
django-filter==2.2.0
django-ical==1.7.1
django-inline-svg==0.1.1
django-recurrence==1.10.3
django-stronghold==0.4.0
django-tables2==2.3.1
django==3.0.6
Expand All @@ -41,6 +43,7 @@ flake8-quotes==3.0.0
flake8==3.7.9
freezegun==0.3.15
humanize==2.4.0
icalendar==4.0.6
idna==2.9
ipython-genutils==0.2.0
ipython==7.14.0
Expand Down
3 changes: 3 additions & 0 deletions reqs/prod.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@ dj-database-url==0.5.0
django-allauth==0.42.0
django-extra-fields==2.0.5
django-filter==2.2.0
django-ical==1.7.1
django-inline-svg==0.1.1
django-recurrence==1.10.3
django-stronghold==0.4.0
django-tables2==2.3.1
django==3.0.6
djangorestframework==3.11.0
docutils==0.15.2
gunicorn==20.0.4
humanize==2.4.0
icalendar==4.0.6
idna==2.9
iso3166==1.0.1
jmespath==0.10.0
Expand Down

0 comments on commit baeadba

Please sign in to comment.