mirror of
https://github.com/nikdoof/vapemap.git
synced 2025-12-13 06:22:16 +00:00
Added very basic Tastypie API, and dynamic forms for Country/County.
This commit is contained in:
18
app/stores/api.py
Normal file
18
app/stores/api.py
Normal file
@@ -0,0 +1,18 @@
|
||||
from django.forms.models import model_to_dict
|
||||
from tastypie.resources import ModelResource, ALL, ALL_WITH_RELATIONS
|
||||
from stores.models import County, Country
|
||||
|
||||
|
||||
class CountryResource(ModelResource):
|
||||
|
||||
def dehydrate(self, bundle):
|
||||
counties = County.objects.filter(country=bundle.data['id'])
|
||||
bundle.data['counties'] = [model_to_dict(c) for c in counties]
|
||||
return bundle
|
||||
|
||||
class Meta:
|
||||
queryset = Country.objects.all()
|
||||
resource_name = 'country'
|
||||
filtering = {
|
||||
'id': ('exact',)
|
||||
}
|
||||
@@ -9,6 +9,25 @@
|
||||
{% block scripts %}
|
||||
<script type="text/javascript" src="https://maps.google.com/maps/api/js?sensor=false"></script>
|
||||
<script type="text/javascript" src="{% static "js/geocoder.js" %}"></script>
|
||||
<script type="text/javascript">
|
||||
$(document).ready(function() {
|
||||
$('select#id_0-county').html('').prop('disabled', 'disabled');
|
||||
$('select#id_0-country').change(function(){
|
||||
var country_id = $("select#id_0-country option:selected").val();
|
||||
var url = '{% url 'api_dispatch_list' resource_name='country' api_name='1.0' %}' + country_id + "/";
|
||||
$('select#id_0-county').html('').prop('disabled', 'disabled');
|
||||
$.getJSON(url, function(data) {
|
||||
if (data.counties.length > 0) {
|
||||
$(data.counties).each(function () {
|
||||
$('select#id_0-county').append($('<option/>').val(this.id).text(this.name));
|
||||
});
|
||||
$('select#id_0-county').change();
|
||||
$('select#id_0-county').prop('disabled', false);
|
||||
}
|
||||
})
|
||||
})
|
||||
});
|
||||
</script>
|
||||
{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
|
||||
@@ -78,6 +78,7 @@ INSTALLED_APPS = [
|
||||
'bootstrapform',
|
||||
'registration',
|
||||
'haystack',
|
||||
'tastypie',
|
||||
'moderation',
|
||||
'stores',
|
||||
]
|
||||
|
||||
@@ -87,7 +87,6 @@
|
||||
{% endblock %}
|
||||
{% switch uservoice_tab %}
|
||||
<script type='text/javascript'>
|
||||
|
||||
var _ues = {
|
||||
host:'vapourhunter.userecho.com',
|
||||
forum:'18759',
|
||||
@@ -103,13 +102,11 @@
|
||||
tab_bg_color:'#999999',
|
||||
tab_hover_color:'#0080c0'
|
||||
};
|
||||
|
||||
(function() {
|
||||
var _ue = document.createElement('script'); _ue.type = 'text/javascript'; _ue.async = true;
|
||||
_ue.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'cdn.userecho.com/js/widget-1.4.gz.js';
|
||||
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(_ue, s);
|
||||
})();
|
||||
|
||||
</script>
|
||||
{% endswitch %}
|
||||
</html>
|
||||
@@ -2,14 +2,22 @@ from django.conf import settings
|
||||
from django.conf.urls import patterns, include, url
|
||||
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
|
||||
from django.contrib import admin
|
||||
from tastypie.api import Api
|
||||
from stores.api import CountryResource, CountyResource
|
||||
admin.autodiscover()
|
||||
|
||||
|
||||
v1_api = Api(api_name='1.0')
|
||||
v1_api.register(CountryResource())
|
||||
|
||||
|
||||
urlpatterns = patterns('',
|
||||
url(r'^admin/', include(admin.site.urls)),
|
||||
url(r'user/', include('registration.backends.default.urls')),
|
||||
url(r'user/', include('django.contrib.auth.urls')),
|
||||
url(r'^search/', include('haystack.urls')),
|
||||
url(r'^moderation/', include('moderation.urls')),
|
||||
url(r'^api/', include(v1_api.urls)),
|
||||
url('', include('stores.urls'))
|
||||
)
|
||||
|
||||
|
||||
@@ -21,3 +21,4 @@ django-storages
|
||||
boto
|
||||
raven>=3
|
||||
django-waffle>=0.9.1
|
||||
django-tastypie>=0.9.14
|
||||
Reference in New Issue
Block a user