Skip to content

Commit

Permalink
related to #16 - added the form views, empty and populated.
Browse files Browse the repository at this point in the history
  • Loading branch information
mfrasca committed May 5, 2019
1 parent 6684078 commit bb6875c
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 36 deletions.
66 changes: 66 additions & 0 deletions browse/forms.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from django.forms import ModelForm
from django import forms
from django_select2 import forms as s2forms

from django.http import JsonResponse

# HeavySelect2MultipleWidget, HeavySelect2Widget, ModelSelect2MultipleWidget,
# ModelSelect2TagWidget, ModelSelect2Widget, Select2MultipleWidget, Select2Widget

Expand All @@ -15,6 +18,15 @@ class Meta:
model = Taxon
fields = ['epithet', 'rank']

@classmethod
def as_view(cls):
def view(request, pk=None):
if pk is not None:
obj = Taxon.objects.get(pk=pk)
return JsonResponse({'form': "%s" % TaxonForm(instance=obj)})
return JsonResponse({'form': "%s" % TaxonForm()})
return view


class VerificationForm(ModelForm):
class Meta:
Expand All @@ -36,22 +48,76 @@ class Meta:
'qualifier': s2forms.Select2Widget,
}

@classmethod
def as_view(cls):
def view(request, accession_code, seq=None):
if seq is not None:
obj = Verification.objects.get(accession__code=accession_code, seq=seq)
return JsonResponse({'form': "%s" % VerificationForm(instance=obj)})
acc = Accession.objects.get(code=accession_code)
obj = Verification(accession=acc)
return JsonResponse({'form': "%s" % VerificationForm(instance=obj)})
return view


class LocationForm(ModelForm):
class Meta:
model = Location
fields = '__all__'

@classmethod
def as_view(cls):
def view(request, code=None):
if code is not None:
obj = Location.objects.get(code=code)
return JsonResponse({'form': "%s" % LocationForm(instance=obj)})
return JsonResponse({'form': "%s" % LocationForm()})
return view


class AccessionForm(ModelForm):
class Meta:
model = Accession
fields = '__all__'

@classmethod
def as_view(cls):
def view(request, code=None):
if code is not None:
obj = Accession.objects.get(code=code)
return JsonResponse({'form': "%s" % AccessionForm(instance=obj)})
return JsonResponse({'form': "%s" % AccessionForm()})
return view


class PlantForm(ModelForm):
class Meta:
model = Plant
fields = '__all__'

@classmethod
def as_view(cls):
def view(request, accession_code, code=None):
if code is not None:
obj = Plant.objects.get(accession__code=accession_code, code=code)
return JsonResponse({'form': "%s" % PlantForm(instance=obj)})
acc = Accession.objects.get(code=accession_code)
obj = Plant(accession=acc)
return JsonResponse({'form': "%s" % PlantForm()})
return view


class ContactForm(ModelForm):
class Meta:
model = Contact
fields = '__all__'

@classmethod
def as_view(cls):
def view(request, pk=None):
if pk is not None:
obj = Contact.objects.get(pk=pk)
return JsonResponse({'form': "%s" % ContactForm(instance=obj)})
return JsonResponse({'form': "%s" % ContactForm()})
return view

71 changes: 37 additions & 34 deletions browse/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -180,20 +180,7 @@
}

function perform_on_selected(parent_class, action_name, this_class, http_verb, optional, e) {
var api_url;
if (parent_class === 'new') {
// there is no parent class, so let's use this_class own entry point
api_url = {accession: "{% url 'accession-list' %}",
taxon: "{% url 'taxon-list' %}",
location: "{% url 'location-list' %}",
contact: "{% url 'contact-list' %}" }[this_class];
} else {
// the active td holds the infobox url
var infobox_url = $('#results-table-body td.table-success').attr('data-infobox-url');
api_url = infobox_url.replace('infobox/', '');
}
// get the data, populate the form, activate it
$.getJSON(api_url, partial(function(action_name, this_class, verb, data) {
var f = partial(function(action_name, this_class, verb, data) {
console.log(action_name, this_class, verb, data);
var modal_form = $('#modal-form-' + action_name);
var modal_form_body = $(modal_form).find('.modal-body');
Expand Down Expand Up @@ -227,7 +214,22 @@
// stretch width of elements to new size of modal
$('.modal td select').css({width: '100%'})
$('.modal td span').css({width: '100%'})
}, action_name, this_class, http_verb))
}, action_name, this_class, http_verb);
var api_url;
if (parent_class === 'new') {
// there is no parent class, so let's use this_class own entry point
api_url = {accession: "{% url 'accession-list' %}",
taxon: "{% url 'taxon-list' %}",
location: "{% url 'location-list' %}",
contact: "{% url 'contact-list' %}" }[this_class];
f({})
} else {
// the active td holds the infobox url
var infobox_url = $('#results-table-body td.table-success').attr('data-infobox-url');
api_url = infobox_url.replace('infobox/', '');
// get the data, populate the form, activate it
$.getJSON(api_url, f)
}
}

function getCookie(name) {
Expand Down Expand Up @@ -473,30 +475,31 @@ <h4 class="modal-title">Add Verification</h4>
</div>
</div>

<table id="verification-table">
{{ verification_form }}
</table>

<table id="contact-table">
{{ contact_form }}
</table>
<div id="storage" style="display:none">
<table id="verification-table">
{{ verification_form }}
</table>

<table id="plant-table">
{{ plant_form }}
</table>
<table id="contact-table">
{{ contact_form }}
</table>

<table id="taxon-table">
{{ taxon_form }}
</table>
<table id="plant-table">
{{ plant_form }}
</table>

<table id="accession-table">
{{ accession_form }}
</table>
<table id="taxon-table">
{{ taxon_form }}
</table>

<table id="location-table">
{{ location_form }}
</table>
<table id="accession-table">
{{ accession_form }}
</table>

<table id="location-table">
{{ location_form }}
</table>
</div>
<script type="text/javascript" src="{% static 'js/select2.min.js' %}"></script>
<script type="text/javascript" src="/static/django_select2/django_select2.js"></script>
</body>
Expand Down
20 changes: 20 additions & 0 deletions browse/urls.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,27 @@
from django.urls import path
from .views import index, filter_json
from .forms import TaxonForm, AccessionForm, VerificationForm, ContactForm, PlantForm, LocationForm

urlpatterns = [
path('', index, name='index'),
path('filter/', filter_json, name='filter'),

# taxonomy
path("taxon/form/", TaxonForm.as_view(), name="taxon-post-form"),
path("taxon/<int:pk>/form/", TaxonForm.as_view(), name="taxon-put-form"),

# collection
path("accession/form/", AccessionForm.as_view(), name="accession-post-form"),
path("accession/<int:pk>/form/", AccessionForm.as_view(), name="accession-put-form"),
path("accession/<str:accession_code>/verifications/form/", VerificationForm.as_view(), name="verification-post-form"),
path("accession/<str:accession_code>/verifications/<int:seq>/form/", VerificationForm.as_view(), name="verification-put-form"),
path("contact/form/", ContactForm.as_view(), name="contact-post-form"),
path("contact/<int:pk>/form/", ContactForm.as_view(), name="contact-put-form"),

# garden
path("accession/<str:accession_code>/plants/form/", PlantForm.as_view(), name="plant-post-form"),
path("accession/<str:accession_code>/plants/<str:code>/form/", PlantForm.as_view(), name="plant-put-form"),
path("location/form/", LocationForm.as_view(), name="location-post-form"),
path("location/<str:code>/form/", LocationForm.as_view(), name="location-put-form"),

]
3 changes: 3 additions & 0 deletions collection/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
ContactDetail, ContactList, ContactInfobox, )

urlpatterns = [
# accession
path("accessions/", AccessionList.as_view(), name="accession-list"),
path("accessions/<str:code>/", AccessionDetail.as_view(), name="accession"),
path("accessions/<str:code>/infobox/", AccessionInfobox.as_view(), name="accession-infobox"),
# verification
path("accessions/<str:accession_code>/verifications/", VerificationList.as_view(), name="verification-list"),
path("accessions/<str:accession_code>/verifications/<int:seq>/", VerificationDetail.as_view(), name="verification"),
# contact
path("contacts/", ContactList.as_view(), name="contact-list"),
path("contacts/<int:pk>/", ContactDetail.as_view(), name="contact"),
path("contacts/<int:pk>/infobox/", ContactInfobox.as_view(), name="contact-infobox"),
Expand Down
3 changes: 2 additions & 1 deletion garden/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
LocationList, LocationDetail, LocationInfobox, )

urlpatterns = [
# plant
path("accessions/<str:accession_code>/plants/", PlantList.as_view(), name="plant-list"),
path("accessions/<str:accession_code>/plants/<str:code>/", PlantDetail.as_view(), name="plant"),
path("accessions/<str:accession_code>/plants/<str:code>/infobox/", PlantInfobox.as_view(), name="plant-infobox"),
# location
path("locations/", LocationList.as_view(), name="location-list"),
path("locations/<str:code>/", LocationDetail.as_view(), name="location"),
path("locations/<str:code>/infobox/", LocationInfobox.as_view(), name="location-infobox"),
#path("locations/<int:code>/plants/", Plants.as_view(), name="plants-at-location"),
]
2 changes: 1 addition & 1 deletion taxonomy/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

urlpatterns = [
path("taxa/", TaxonList.as_view(), name="taxon-list"),
path("taxa/<int:pk>/", TaxonDetail.as_view(), name="taxon"),
path("taxa/<int:pk>/", TaxonDetail.as_view(), name="taxon-detail"),
path("taxa/<int:pk>/infobox/", TaxonInfobox.as_view(), name="taxon-infobox"),
path("ranks/", RankList.as_view(), name="rank-list"),
path("ranks/<int:pk>/", RankDetail.as_view(), name="rank"),
Expand Down
5 changes: 5 additions & 0 deletions taxonomy/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ def get(self, request, *args, **kwargs):
return Response([], status=status.HTTP_204_NO_CONTENT)


def taxon_form(request, pk=None):
print(pk)
return JsonResponse({})


class RankList(generics.ListCreateAPIView):
serializer_class = RankSerializer
queryset = Rank.objects.all()
Expand Down

0 comments on commit bb6875c

Please sign in to comment.