[M] updated oh veiw and form. now we can post time using website
This commit is contained in:
parent
e1d1c85dea
commit
d2744493e2
@ -8,4 +8,23 @@ class OfficeHoursForm(forms.ModelForm):
|
|||||||
model = timer.models.OfficeHours
|
model = timer.models.OfficeHours
|
||||||
fields = ['user', 'begins_at', 'ends_at']
|
fields = ['user', 'begins_at', 'ends_at']
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
self.user = kwargs.pop('user')
|
||||||
|
return super().__init__(*args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
def full_clean(self):
|
||||||
|
if not self.user.is_authenticated:
|
||||||
|
raise ValidationError('Invalid User.')
|
||||||
|
return
|
||||||
|
|
||||||
|
def save(self):
|
||||||
|
begins_at = timer.models.OfficeHours.parse_time_str(self.data.get('begins_at'))
|
||||||
|
user = self.user
|
||||||
|
obj = timer.models.OfficeHours.objects.create(
|
||||||
|
user=user,
|
||||||
|
begins_at=begins_at,
|
||||||
|
ends_at=timer.models.OfficeHours.get_ends_at(begins_at))
|
||||||
|
return obj
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -10,11 +10,14 @@
|
|||||||
{% csrf_token %}
|
{% csrf_token %}
|
||||||
{% for f in form %}
|
{% for f in form %}
|
||||||
<div class="form-group " >
|
<div class="form-group " >
|
||||||
|
{% if f.name == 'begins_at' %}
|
||||||
<label>
|
<label>
|
||||||
{{ f.name }}
|
{{ f.name }}
|
||||||
</label>
|
</label>
|
||||||
<input type="" name={{ f.name }} class="form-control">
|
<input type="" name={{ f.name }} class="form-control">
|
||||||
|
{% endif %}
|
||||||
</div>
|
</div>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
<button type="submit" class="btn btn-primary">Submit</button>
|
||||||
</form>
|
</form>
|
||||||
{% endblock content %}
|
{% endblock content %}
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
from django.conf.urls import include, url
|
from django.conf.urls import include, url
|
||||||
|
# from django.core.urlresolvers import reverse
|
||||||
from django.urls import path
|
from django.urls import path
|
||||||
from rest_framework import routers
|
from rest_framework import routers
|
||||||
from timer import views
|
from timer import views
|
||||||
|
|||||||
@ -12,6 +12,7 @@ import timer.forms
|
|||||||
|
|
||||||
import datetime
|
import datetime
|
||||||
from django.utils.timezone import localtime
|
from django.utils.timezone import localtime
|
||||||
|
from django.urls import reverse
|
||||||
|
|
||||||
|
|
||||||
class OfficeHoursAPI(CreateAPIView):
|
class OfficeHoursAPI(CreateAPIView):
|
||||||
@ -49,6 +50,21 @@ class OfficeHoursFormView(django.views.generic.FormView):
|
|||||||
template_name = 'index.html'
|
template_name = 'index.html'
|
||||||
form_class = timer.forms.OfficeHoursForm
|
form_class = timer.forms.OfficeHoursForm
|
||||||
|
|
||||||
|
def get_form_kwargs(self):
|
||||||
|
kwargs = super().get_form_kwargs()
|
||||||
|
kwargs['user'] = self.request.user
|
||||||
|
return kwargs
|
||||||
|
|
||||||
|
def post(self, request, *args, **kwargs):
|
||||||
|
form = self.form_class(**self.get_form_kwargs())
|
||||||
|
if form.is_valid():
|
||||||
|
form.save()
|
||||||
|
|
||||||
|
return super().post(request, *args, **kwargs)
|
||||||
|
|
||||||
|
def get_success_url(self):
|
||||||
|
return reverse('office-hours-page')
|
||||||
|
|
||||||
def get_context_data(self, **kwargs):
|
def get_context_data(self, **kwargs):
|
||||||
context = super().get_context_data(**kwargs)
|
context = super().get_context_data(**kwargs)
|
||||||
today_oh = timer.models.OfficeHours.objects.filter(begins_at__date=localtime().date()).last()
|
today_oh = timer.models.OfficeHours.objects.filter(begins_at__date=localtime().date()).last()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user