Last active
August 29, 2015 14:04
-
-
Save actongorton/add920dc957b9a5abfaf to your computer and use it in GitHub Desktop.
Restaurant Inspections v3.0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.conf.urls import url | |
from tastypie.paginator import Paginator | |
from tastypie.exceptions import BadRequest | |
from tastypie import fields | |
from tastypie.resources import ModelResource, ALL_WITH_RELATIONS | |
from haystack.query import SearchQuerySet | |
from restaurants.models import RestaurantInformation, InspectionInformation | |
class InspectionResource(ModelResource): | |
class Meta: | |
queryset = InspectionInformation.objects.all() | |
resource_name = 'inspection' | |
filtering = { | |
'insp_score': ALL_WITH_RELATIONS, | |
} | |
class RestaurantResource(ModelResource): | |
inspections = fields.ToManyField( | |
InspectionResource, | |
'inspections', | |
full=True | |
) | |
class Meta: | |
queryset = RestaurantInformation.objects.all() | |
resource_name = 'restaurants' | |
filtering = { | |
'rest_name': ALL_WITH_RELATIONS, | |
'rest_owner': ALL_WITH_RELATIONS, | |
'rest_permit': ALL_WITH_RELATIONS, | |
'inspections': ALL_WITH_RELATIONS, | |
} | |
# Custom search endpoint | |
def override_urls(self): | |
return [ | |
url(r"^(?P<resource_name>%s)/search/?$" % (self._meta.resource_name), | |
self.wrap_view('get_search'), | |
name="api_get_search"), | |
] | |
def get_search(self, request, **kwargs): | |
''' Custom endpoint for search ''' | |
self.method_check(request, allowed=['get']) | |
self.is_authenticated(request) | |
self.throttle_check(request) | |
query = request.GET.get('q', None) | |
if not query: | |
raise BadRequest('Please supply the search parameter...') | |
results = SearchQuerySet().models(RestaurantInformation).auto_query(query) | |
paginator = Paginator(request.GET, results, resource_uri='/cucitizen/restaurants/api/search/') | |
bundles = [] | |
for result in paginator.page()['objects']: | |
bundle = self.build_bundle(obj=result.object, request=request) | |
bundles.append(self.full_dehydrate(bundle)) | |
object_list = { | |
'meta': paginator.page()['meta'], | |
'objects': bundles | |
} | |
object_list['meta']['search_query'] = query | |
self.log_throttled_access(request) | |
return self.create_response(request, object_list) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db import models | |
class RestaurantInformation(models.Model): | |
rest_permit = models.IntegerField(unique=True, verbose_name='Permit') | |
rest_name = models.CharField(max_length=200, verbose_name='Name') | |
rest_address = models.CharField(max_length=200, verbose_name='Address') | |
rest_city = models.CharField(max_length=100, verbose_name='City') | |
rest_zip = models.IntegerField(verbose_name='Zip Code') | |
rest_owner = models.CharField(max_length=200, verbose_name='Owner') | |
rest_latitude = models.CharField(max_length=40, verbose_name='Latitude') | |
rest_longitude = models.CharField(max_length=40, verbose_name='Longitude') | |
class Meta: | |
ordering = ['rest_name'] | |
def __unicode__(self): | |
return self.rest_name + ', ' + self.rest_address + ', ' + self.rest_city | |
class InspectionInformation(models.Model): | |
insp_rest_permit = models.ForeignKey(RestaurantInformation, null=False, to_field='rest_permit', related_name='inspections') | |
insp_score = models.DecimalField(verbose_name='Score', decimal_places=2, max_digits=5) | |
insp_date = models.DateField(verbose_name='Date') | |
insp_inspector = models.CharField(max_length=200, verbose_name='Inspector') | |
insp_report = models.FileField(upload_to='restaurants.InspectionFile/bytes/filename/mimetype', | |
verbose_name='Inspection Report') | |
class Meta: | |
unique_together = ("insp_rest_permit", "insp_score", "insp_date") | |
ordering = ['insp_date'] | |
class InspectionFile(models.Model): | |
bytes = models.TextField() | |
filename = models.CharField(max_length=255) | |
mimetype = models.CharField(max_length=50) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
http://127.0.0.1:8000/cucitizen/api/restaurants/?format=json&inspections__insp_score__lte=36.00&inspections__insp_date__year=2014 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment