oba
-- Thanks. Here are all the files you asked for.......
=============================================================
forms.py:
=============================================================
class AppointmentForm(django.forms.Form):
customer = django.forms.IntegerField()
year = django.forms.CharField(min_length = 4, max_length = 4)
month = django.forms.CharField(max_length = 2)
day = django.forms.CharField(max_length = 2)
hour = django.forms.CharField(max_length = 2,
validators = [verify_hour])
minute = django.forms.CharField(min_length = 2, max_length = 2)
am_or_pm = django.forms.CharField(max_length = 2,
widget = AM_OR_PM,
initial = "PM")
def clean(self):
cleaned_data = super().clean()
try:
year = int(cleaned_data.get("year"))
month = int(cleaned_data.get("month"))
day = int(cleaned_data.get("day"))
date = datetime.date(year = year,
month = month,
day = day)
except (ValueError, TypeError):
raise django.forms.ValidationError("Invalid date")
if date < datetime.date.today():
raise django.forms.ValidationError("Invalid date")
try:
hour = int(cleaned_data.get("hour"))
minute = int(cleaned_data.get("minute"))
datetime.time(hour = hour, minute = minute)
except (ValueError, TypeError):
raise django.forms.ValidationError("Invalid time")
=============================================================
view
=============================================================
CUST = grandmas4hire.models.Customer
APPT = grandmas4hire.models.Appointment
...
@django.contrib.auth.decorators.login_required
def admin_appt(request):
custs = CUST.objects.all()
custs = [(e.first + " " + e.last, e.id) for e in custs]
appts = [e for e in APPT.objects.all()]
appts = sorted(appts,
key = lambda a : a.customer.last + a.customer.first + \
str(a.date) + str(a.time))
if request.method == "POST":
form = grandmas4hire.forms.AppointmentForm(request.POST)
if form.is_valid():
cust = form.cleaned_data["customer"]
cust = CUST.objects.get(id = cust)
year = int(form.cleaned_data["year"])
month = int(form.cleaned_data["month"])
day = int(form.cleaned_data["day"])
date = datetime.date(year = year,
month = month,
day = day)
hour = int(form.cleaned_data["hour"])
min_ = int(form.cleaned_data["minute"])
time = datetime.time(hour = hour, minute = min_)
if form.cleaned_data["am_or_pm"] == "PM":
time = datetime.time(hour = time.hour + 12,
minute = time.minute)
appt = APPT(customer = cust,
user = request.user,
date = date,
time = time,
status = "in process",
reminded = False,
updated = False)
appt.save()
reply = django.shortcuts.redirect("/admin_appt")
else:
reply = django.shortcuts.render(request,
"admin_appt.html",
{"form" : form,
"custs" : custs,
"appts" : appts})
else:
form = grandmas4hire.forms.AppointmentForm()
reply = django.shortcuts.render(request,
"admin_appt.html",
{"form" : form,
"custs" : custs,
"appts" : appts})
return reply
=============================================================
template
=============================================================
{% extends "html_base" %}
{% block body_elements %}
<div id = "admin_appt">
<form action = "." method = "post"> {% csrf_token %}
<p>ADD APPOINTMENTS {{form.errors}}</p>
<div>Customer:</div>
<p>
<select name = "customer" id = "id_customer">
{% for cust in custs %}
<option value = {{cust.1}}>
{{cust.0}}
</option>
{% endfor %}
</select>
</p>
<div>Date (YYYY-MM-DD):</div>
<p>{{form.year}}-{{form.month}}-{{form.day}}</p>
<div>Time (HH:MM): {{form.hour.errors}}</div>
<p>{{form.hour}}:{{form.minute}} {{form.am_or_pm}}</p>
<p><input type = "submit" value = "ADD APPOINTMENT"/></p>
</form>
<p><a href = "/admin">Go Back To Admin Page</a></p>
<table>
<tr>
<th>NAME</th>
<th>DATE</th>
<th>TIME</th>
</tr>
{% for appt in appts %}
<tr>
<td>
{{appt.customer.first}}
{{appt.customer.last}}
</td>
<td>{{appt.date|date:"Y-m-d"}}</td>
<td>{{appt.time|time:"h:i A"}}</td>
</tr>
{% endfor %}
</table>
</div>
{% endblock %}
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/b9d668ce-fc6b-476f-9803-fbcdbe059c8co%40googlegroups.com.
No comments:
Post a Comment