[A] added a page for viewing office hour!
[A] added a template [M] modified OH's api
This commit is contained in:
parent
82cb9a425a
commit
e0bb8e78d6
11
timer/forms.py
Normal file
11
timer/forms.py
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
from django import forms
|
||||||
|
|
||||||
|
import timer.models
|
||||||
|
|
||||||
|
|
||||||
|
class OfficeHoursForm(forms.ModelForm):
|
||||||
|
class Meta:
|
||||||
|
model = timer.models.OfficeHours
|
||||||
|
fields = ['user', 'begins_at', 'ends_at']
|
||||||
|
|
||||||
|
|
||||||
@ -1,9 +1,27 @@
|
|||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
import django.db
|
import django.db
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
from django.utils.timezone import localtime
|
||||||
|
import datetime
|
||||||
|
|
||||||
|
|
||||||
class OfficeHours(models.Model):
|
class OfficeHours(models.Model):
|
||||||
begins_at = models.DateTimeField()
|
begins_at = models.DateTimeField()
|
||||||
ends_at = models.DateTimeField()
|
ends_at = models.DateTimeField()
|
||||||
user = models.ForeignKey(User, on_delete=django.db.models.deletion.CASCADE)
|
user = models.ForeignKey(User, on_delete=django.db.models.deletion.CASCADE)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def parse_time_str(cls, time_srt):
|
||||||
|
return datetime.datetime.strptime(time_srt, '%Y-%m-%d %H:%M')
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def get_ends_at(cls, begins_at):
|
||||||
|
return begins_at + datetime.timedelta(hours=9.5)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def get_begins_at_str(self):
|
||||||
|
return localtime(self.begins_at).strftime('%Y-%m-%d %H:%M')
|
||||||
|
|
||||||
|
@property
|
||||||
|
def get_ends_at_str(self):
|
||||||
|
return localtime(self.ends_at).strftime('%Y-%m-%d %H:%M')
|
||||||
|
|||||||
@ -0,0 +1,9 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>Office Hour</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>office hour ends at: {{ today_oh.get_ends_at_str }}</h1>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@ -7,5 +7,6 @@ from timer import views
|
|||||||
# Wire up our API using automatic URL routing.
|
# Wire up our API using automatic URL routing.
|
||||||
# Additionally, we include login URLs for the browsable API.
|
# Additionally, we include login URLs for the browsable API.
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
url('office-hours', views.OfficeHoursAPI.as_view())
|
url(r'^office-hours/$', views.OfficeHoursAPI.as_view()),
|
||||||
|
url(r'^office-hours-page/$', views.OfficeHoursFormView.as_view(), name='office-hours-page'),
|
||||||
]
|
]
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
from django.contrib.auth.models import User, Group
|
from django.contrib.auth.models import User, Group
|
||||||
from django.shortcuts import render
|
from django.shortcuts import render
|
||||||
|
import django.views.generic
|
||||||
from rest_framework import authentication, permissions, status, viewsets
|
from rest_framework import authentication, permissions, status, viewsets
|
||||||
from rest_framework.generics import CreateAPIView
|
from rest_framework.generics import CreateAPIView
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
@ -7,33 +8,10 @@ from rest_framework.views import APIView
|
|||||||
|
|
||||||
import timer.serializers
|
import timer.serializers
|
||||||
import timer.models
|
import timer.models
|
||||||
|
import timer.forms
|
||||||
|
|
||||||
import datetime
|
import datetime
|
||||||
|
from django.utils.timezone import localtime
|
||||||
|
|
||||||
# class OfficeHoursViewSet(viewsets.ModelViewSet):
|
|
||||||
# """
|
|
||||||
# API endpoint that allows office hours to be viewed or edited.
|
|
||||||
# """
|
|
||||||
|
|
||||||
# queryset = timer.models.OfficeHours.objects.order_by('-id')
|
|
||||||
# serializer_class = timer.serializers.OfficeHoursSerializer
|
|
||||||
|
|
||||||
|
|
||||||
# class UserViewSet(viewsets.ModelViewSet):
|
|
||||||
# """
|
|
||||||
# API endpoint that allows users to be viewed or edited.
|
|
||||||
# """
|
|
||||||
# queryset = User.objects.all().order_by('-date_joined')
|
|
||||||
# serializer_class = timer.serializers.UserSerializer
|
|
||||||
|
|
||||||
|
|
||||||
# class GroupViewSet(viewsets.ModelViewSet):
|
|
||||||
# """
|
|
||||||
# API endpoint that allows groups to be viewed or edited.
|
|
||||||
# """
|
|
||||||
# queryset = Group.objects.all()
|
|
||||||
# serializer_class = timer.serializers.GroupSerializer
|
|
||||||
|
|
||||||
|
|
||||||
class OfficeHoursAPI(CreateAPIView):
|
class OfficeHoursAPI(CreateAPIView):
|
||||||
@ -44,14 +22,15 @@ class OfficeHoursAPI(CreateAPIView):
|
|||||||
permission_classes = (permissions.IsAuthenticated,)
|
permission_classes = (permissions.IsAuthenticated,)
|
||||||
|
|
||||||
queryset = timer.models.OfficeHours.objects.all()
|
queryset = timer.models.OfficeHours.objects.all()
|
||||||
|
# serializer_class = timer.serializers.GroupSerializer
|
||||||
|
|
||||||
def create(self, request, *args, **kwargs):
|
def create(self, request, *args, **kwargs):
|
||||||
begins_at = request.data.get('begins_at')
|
begins_at = request.data.get('begins_at')
|
||||||
if not begins_at:
|
if not begins_at:
|
||||||
raise
|
raise
|
||||||
try:
|
try:
|
||||||
begins_at = datetime.datetime.strptime(begins_at, '%Y-%m-%d %H:%M')
|
begins_at = timer.models.OfficeHours.parse_time_str(begins_at)
|
||||||
ends_at = begins_at + datetime.timedelta(hours=9.5)
|
ends_at = timer.models.OfficeHours.get_ends_at(begins_at)
|
||||||
oh, __ = timer.models.OfficeHours.objects.get_or_create(
|
oh, __ = timer.models.OfficeHours.objects.get_or_create(
|
||||||
begins_at=begins_at,
|
begins_at=begins_at,
|
||||||
ends_at=ends_at,
|
ends_at=ends_at,
|
||||||
@ -60,9 +39,19 @@ class OfficeHoursAPI(CreateAPIView):
|
|||||||
raise
|
raise
|
||||||
|
|
||||||
resp_data = {
|
resp_data = {
|
||||||
'begins_at': begins_at.strftime('%Y-%m-%d %H:%M'),
|
'begins_at': oh.get_begins_at_str,
|
||||||
'ends_at': ends_at.strftime('%H:%M'),
|
'ends_at': oh.get_ends_at_str,
|
||||||
}
|
}
|
||||||
|
|
||||||
return Response(resp_data, status=status.HTTP_201_CREATED)
|
return Response(resp_data, status=status.HTTP_201_CREATED)
|
||||||
|
|
||||||
|
|
||||||
|
class OfficeHoursFormView(django.views.generic.FormView):
|
||||||
|
template_name = 'index.html'
|
||||||
|
form_class = timer.forms.OfficeHoursForm
|
||||||
|
|
||||||
|
def get_context_data(self, **kwargs):
|
||||||
|
context = super().get_context_data(**kwargs)
|
||||||
|
today_oh = timer.models.OfficeHours.objects.filter(begins_at__date=localtime().date()).last()
|
||||||
|
context['today_oh'] = today_oh
|
||||||
|
return context
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user