Permalink
Browse files

have most of the form submit together

  • Loading branch information...
1 parent 272785d commit b9bebfc7870efbad7334806347eed7284c7bf86c @f4nt committed Feb 12, 2012
View
@@ -11,3 +11,4 @@ class AnimalAdmin(admin.ModelAdmin):
admin.site.register(models.Animal, AnimalAdmin)
admin.site.register(models.Person)
admin.site.register(models.Admission)
+admin.site.register(models.SpecieType)
View
@@ -11,10 +11,20 @@ class Meta:
class SpecieTypeForm(forms.ModelForm):
''' Form for creating SpecieTypes '''
+ animal = forms.ModelChoiceField(
+ models.Animal.objects.all(),
+ widget=forms.widgets.HiddenInput
+ )
class Meta:
model = models.SpecieType
+class PersonForm(forms.ModelForm):
+ ''' Form for creating new people '''
+
+ class Meta:
+ model = models.Person
+
class AdmissionForm(forms.ModelForm):
''' Form for creating admissions '''
@@ -0,0 +1,82 @@
+# encoding: utf-8
+import datetime
+from south.db import db
+from south.v2 import SchemaMigration
+from django.db import models
+
+class Migration(SchemaMigration):
+
+ def forwards(self, orm):
+
+ # Changing field 'Admission.received_from'
+ db.alter_column('anitracker_admission', 'received_from_id', self.gf('django.db.models.fields.related.ForeignKey')(null=True, to=orm['anitracker.Person']))
+
+ # Changing field 'Admission.animal_subtype'
+ db.alter_column('anitracker_admission', 'animal_subtype_id', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['anitracker.SpecieType'], null=True))
+
+ # Changing field 'Admission.released_to'
+ db.alter_column('anitracker_admission', 'released_to_id', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['anitracker.Person'], null=True))
+
+
+ def backwards(self, orm):
+
+ # User chose to not deal with backwards NULL issues for 'Admission.received_from'
+ raise RuntimeError("Cannot reverse this migration. 'Admission.received_from' and its values cannot be restored.")
+
+ # User chose to not deal with backwards NULL issues for 'Admission.animal_subtype'
+ raise RuntimeError("Cannot reverse this migration. 'Admission.animal_subtype' and its values cannot be restored.")
+
+ # User chose to not deal with backwards NULL issues for 'Admission.released_to'
+ raise RuntimeError("Cannot reverse this migration. 'Admission.released_to' and its values cannot be restored.")
+
+
+ models = {
+ 'anitracker.admission': {
+ 'Meta': {'object_name': 'Admission'},
+ 'animal': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['anitracker.Animal']"}),
+ 'animal_age': ('django.db.models.fields.CharField', [], {'max_length': '64'}),
+ 'animal_subtype': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['anitracker.SpecieType']", 'null': 'True'}),
+ 'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
+ 'date_of_admission': ('django.db.models.fields.DateField', [], {}),
+ 'disposition': ('django.db.models.fields.CharField', [], {'max_length': '64'}),
+ 'disposition_date': ('django.db.models.fields.DateField', [], {}),
+ 'follow_up': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'modified': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}),
+ 'notes': ('django.db.models.fields.TextField', [], {'null': 'True', 'blank': 'True'}),
+ 'received_from': ('django.db.models.fields.related.ForeignKey', [], {'blank': 'True', 'related_name': "'received_person'", 'null': 'True', 'to': "orm['anitracker.Person']"}),
+ 'released_to': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['anitracker.Person']", 'null': 'True', 'blank': 'True'})
+ },
+ 'anitracker.animal': {
+ 'Meta': {'object_name': 'Animal'},
+ 'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'modified': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}),
+ 'name': ('django.db.models.fields.CharField', [], {'max_length': '256'})
+ },
+ 'anitracker.person': {
+ 'Meta': {'object_name': 'Person'},
+ 'address': ('django.db.models.fields.CharField', [], {'max_length': '256'}),
+ 'address_two': ('django.db.models.fields.CharField', [], {'max_length': '256', 'null': 'True'}),
+ 'county': ('django.db.models.fields.CharField', [], {'max_length': '128'}),
+ 'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
+ 'email': ('django.db.models.fields.EmailField', [], {'max_length': '75', 'null': 'True'}),
+ 'first_name': ('django.db.models.fields.CharField', [], {'max_length': '128'}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'last_name': ('django.db.models.fields.CharField', [], {'max_length': '128'}),
+ 'modified': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}),
+ 'state': ('django.contrib.localflavor.us.models.USStateField', [], {'max_length': '2'}),
+ 'telephone': ('django.contrib.localflavor.us.models.PhoneNumberField', [], {'max_length': '20'}),
+ 'zipcode': ('django.db.models.fields.CharField', [], {'max_length': '10'})
+ },
+ 'anitracker.specietype': {
+ 'Meta': {'object_name': 'SpecieType'},
+ 'animal': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['anitracker.Animal']"}),
+ 'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'modified': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}),
+ 'name': ('django.db.models.fields.CharField', [], {'max_length': '256'})
+ }
+ }
+
+ complete_apps = ['anitracker']
View
@@ -74,10 +74,14 @@ class Admission(BaseModel):
)
date_of_admission = models.DateField()
- received_from = models.ForeignKey('Person', related_name='received_person')
- released_to = models.ForeignKey('Person')
+ received_from = models.ForeignKey('Person',
+ related_name='received_person',
+ null=True,
+ blank=True,
+ )
+ released_to = models.ForeignKey('Person', null=True, blank=True)
animal = models.ForeignKey('Animal')
- animal_subtype = models.ForeignKey('SpecieType')
+ animal_subtype = models.ForeignKey('SpecieType', null=True)
animal_age = models.CharField(max_length=64, choices=ANIMAL_AGE_CHOICES)
disposition = models.CharField(max_length=64,
choices=DISPOSITION_CHOICES)
@@ -1,12 +1,20 @@
<html lang="en">
<head>
<title>Missouri Wildlife Hotline{% block title %}{% endblock %}</title>
+ <link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.17/themes/black-tie/jquery-ui.css" type="text/css" media="all" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
+ <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js"></script>
{% block extra_head %}
{% endblock %}
</head>
<body>
{% block content %}
{% endblock %}
+ <script type="text/javascript">
+ $(document).ready(function() {
+ {% block onready %}
+ {% endblock %}
+ });
+ </script>
</body>
</html>
@@ -1,9 +1,69 @@
{% extends 'anitracker/base.html' %}
{% block title %} - Admissions {% endblock %}
{% block content %}
- {{ animal_form.as_p }}
- <br /><br />
- {{ specie_form.as_p }}
- <br /><br />
- {{ admission_form.as_p }}
+ <form method='post' action={% url index %}>
+ <p>
+ <p>{{ form.date_of_admission.label }}: {{ form.date_of_admission }} {{ form.date_of_admission.errors }}</p>
+ <p>{{ form.animal.label }}: {{ form.animal }} or <span id="new-animal">Add a New Animal</span> {{ form.animal.errors }} </p>
+ <span id="add-new-animal" style="display: none;"><p>{{ animal_form.name.label }}: {{ animal_form.name }} {{ animal_form.name.errors }}</p></span>
+ <p>{{ form.animal_subtype.label }}: {{ form.animal_subtype }} or <span id="new-animal-subtype">Add a New Animal SubType</span> {{ form.animal_subtype.errors }}</p>
+ <span id="add-new-subtype" style="display: none;"><p>{{ specie_form.as_p }}</p></span>
+ <p>{{ form.received_from.label }}: {{ form.received_from }} or <span id="new-received-from">Add New Person</span>{{ form.received_from.errors }}</p>
+ <span id="add-new-finder" style="display: none;"><p>{{ finder_form.as_p }}</p></span>
+ <p>{{ form.animal_age.label }}: {{ form.animal_age }} {{ form.animal_age.errors }}</p>
+ <p>{{ form.disposition_date.label }}: {{ form.disposition_date }} {{ form.disposition_date.errors }} </p>
+ <p>{{ form.disposition.label }}: {{ form.disposition }} {{ form.disposition.errors }} </p>
+ <span id="released-to" style="display: none;"><p>{{ form.released_to.label }}: {{ form.released_to }} or <span id="new-released-to">Add a New Person</span>{{ form.released_to.errors }}<br /></p></span>
+ <span id="add-new-receiver" style="display: none;"><p>{{ released_person_form.as_p }}</p></span>
+ <p>{{ form.follow_up.label }}: {{ form.follow_up }} {{ form.follow_up.errors }}</p>
+ <p>{{ form.notes.label }}: {{ form.notes }} {{ form.notes.errors }}</p>
+ </p>
+ {% csrf_token %}
+ <input type='submit' value='Submit' />
+ </form>
+{% endblock %}
+{% block onready %}
+ $('#id_date_of_admission').datepicker();
+ $('#id_disposition_date').datepicker();
+
+ $('#new-animal').click(function() {
+ $('#add-new-animal').toggle();
+ });
+
+ $('#new-animal-subtype').click(function() {
+ $('#add-new-subtype').toggle();
+ });
+
+ $('#new-received-from').click(function() {
+ $('#add-new-finder').toggle();
+ });
+
+ $('#new-released-to').click(function() {
+ $('#add-new-receiver').toggle();
+ });
+
+ $('#id_disposition').change(function() {
+ var value = $('#id_disposition').val();
+ if (value == 'transferred' || value == 'released') {
+ $('#released-to').toggle();
+ }
+ });
+
+ $('#id_animal').change(function() {
+ var value = $('#id_animal').val();
+ $('#id_specie-animal').val(value);
+ if ( value != null ) {
+ $.ajax({
+ url: '{% url specie-type-json %}' + '?animal_pk=' + value,
+ success: function(data) {
+ var options = '<option selected="selected" value="">---------</option>';
+ for (var i=0; i < data.specie_types.length; i++) {
+ options += '<option value="' + data.specie_types[i][0] + '">' + data.specie_types[i][1] + '</option>';
+ }
+ $('#id_animal_subtype').html(options);
+ }
+ });
+ }
+
+ });
{% endblock %}
View
@@ -2,7 +2,10 @@
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns = patterns('anitracker.views',
- url('^$', 'index', name='index')
+ url('^$', 'index', name='index'),
+ url('^species/specie\.json',
+ 'specie_type_json', name='specie-type-json'
+ ),
)
urlpatterns += staticfiles_urlpatterns()
View
@@ -1,15 +1,67 @@
-from django.shortcuts import render
+import json
-from anitracker import forms
+from django.http import HttpResponse
+from django.shortcuts import render, get_object_or_404, redirect
+
+from anitracker import forms, models
def index(request):
- animal_form = forms.AnimalForm()
- specie_form = forms.SpecieTypeForm()
- admission_form = forms.AdmissionForm()
+ animal_form = forms.AnimalForm(prefix='animal')
+ specie_form = forms.SpecieTypeForm(prefix='specie')
+ released_person_form = forms.PersonForm(prefix='released')
+ finder_form = forms.PersonForm(prefix='finder')
+ form = forms.AdmissionForm()
+
+ if request.method == 'POST':
+ animal = None
+ specie = None
+ released_to = None
+ finder = None
+ admission = models.Admission()
+
+ animal_form = forms.AnimalForm(request.POST, prefix='animal')
+ specie_form = forms.SpecieTypeForm(request.POST, prefix='specie')
+ released_person_form = forms.PersonForm(request.POST,
+ prefix='released')
+ finder_form = forms.PersonForm(request.POST, prefix='finder')
+
+ if animal_form.is_valid():
+ animal = animal_form.save()
+ admission.animal = animal
+
+ if specie_form.is_valid():
+ specie = specie_form.save()
+ admission.animal_subtype = specie
+
+ if released_person_form.is_valid():
+ released_to = released_person_form.save()
+ admission.released_to = released_to
+
+ if finder_form.is_valid():
+ finder = finder_form.save()
+ admission.received_from = finder
+
+ form = forms.AdmissionForm(request.POST, instance=admission)
+ import ipdb; ipdb.set_trace() ### XXX BREAKPOINT
+ if form.is_valid():
+ form.save()
+ return redirect('index')
return render(request, 'anitracker/index.html', {
'animal_form': animal_form,
'specie_form': specie_form,
- 'admission_form': admission_form
+ 'finder_form': finder_form,
+ 'released_person_form': released_person_form,
+ 'form': form
})
+
+def specie_type_json(request):
+ ''' Takes animal PK, returns SpecieTypes tied to Animal '''
+ animal = get_object_or_404(
+ models.Animal,
+ pk=request.GET.get('animal_pk') or -1)
+ json_data = {
+ 'specie_types': [(x.pk, x.name) for x in animal.specietype_set.all()]
+ }
+ return HttpResponse(json.dumps(json_data), mimetype='application/json')

0 comments on commit b9bebfc

Please sign in to comment.