From 574909ed1b8feb832de6134c4c4cae2fa851b769 Mon Sep 17 00:00:00 2001 From: Victor Rocha Date: Tue, 17 Jun 2014 18:40:02 -0400 Subject: [PATCH 1/3] [refs #112] Adds a new field to the Inspections model. --- eatsmart/locations/durham/tasks.py | 23 ++++++- eatsmart/locations/durham/utils.py | 8 +++ eatsmart/settings/base.py | 4 ++ ...auto__add_field_establishment_image_url.py | 67 +++++++++++++++++++ inspections/models.py | 1 + 5 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 eatsmart/locations/durham/utils.py create mode 100644 inspections/migrations/0003_auto__add_field_establishment_image_url.py diff --git a/eatsmart/locations/durham/tasks.py b/eatsmart/locations/durham/tasks.py index 79f6f20..6f7f777 100644 --- a/eatsmart/locations/durham/tasks.py +++ b/eatsmart/locations/durham/tasks.py @@ -2,7 +2,8 @@ from eatsmart.celery import app from eatsmart.locations.durham import api - +from eatsmart.locations.durham.utils import check_image +from inspections.models import Establishment logger = getLogger(__file__) @@ -15,3 +16,23 @@ def import_durham_data(): api.InspectionImporter().run() api.ViolationImporter().run() logger.info("Finished Durham Import") + + +@app.task +def update_image_urls(): + """Iterates over all the Establishments and checks if it has a image and the + image exists. If the image_url does not exists, use the property id to + construct an image_url and check if the image exists.""" + for est in Establishment.objects.filter(county='Durham'): + if est.image_url: + image_exists = check_image(est.image_url) + if not image_exists: + est.image_url = '' + est.save() + else: + if est.property_id: + est.image_url = 'http://www.ustaxdata.com/nc/durham/' \ + 'photos_renamed/{0}.jpg'.format(est.property_id) + image_exists = check_image(est.image_url) + if image_exists: + est.save() diff --git a/eatsmart/locations/durham/utils.py b/eatsmart/locations/durham/utils.py new file mode 100644 index 0000000..684b946 --- /dev/null +++ b/eatsmart/locations/durham/utils.py @@ -0,0 +1,8 @@ +import requests + + +def check_image(url): + """Checks if an image url exists, returns True if the image exists.""" + r = requests.get(url) + if r.status_code == 200: + return True diff --git a/eatsmart/settings/base.py b/eatsmart/settings/base.py index 50e2499..e26cbd7 100644 --- a/eatsmart/settings/base.py +++ b/eatsmart/settings/base.py @@ -230,4 +230,8 @@ 'task': 'eatsmart.locations.durham.tasks.import_durham_data', 'schedule': crontab(minute=0, hour=0), # Execute daily at midnight }, + 'import-image-url': { + 'task': 'eatsmart.locations.durham.tasks.update_image_urls', + 'schedule': crontab(minute=30, hour=2, day_of_week=1) # Monday at 2:30 + } } diff --git a/inspections/migrations/0003_auto__add_field_establishment_image_url.py b/inspections/migrations/0003_auto__add_field_establishment_image_url.py new file mode 100644 index 0000000..d4bf29b --- /dev/null +++ b/inspections/migrations/0003_auto__add_field_establishment_image_url.py @@ -0,0 +1,67 @@ +# -*- coding: utf-8 -*- +from south.utils import datetime_utils as datetime +from south.db import db +from south.v2 import SchemaMigration +from django.db import models + + +class Migration(SchemaMigration): + + def forwards(self, orm): + # Adding field 'Establishment.image_url' + db.add_column('inspections_establishment', 'image_url', + self.gf('django.db.models.fields.URLField')(max_length=255, blank=True, default=''), + keep_default=False) + + + def backwards(self, orm): + # Deleting field 'Establishment.image_url' + db.delete_column('inspections_establishment', 'image_url') + + + models = { + 'inspections.establishment': { + 'Meta': {'object_name': 'Establishment', 'unique_together': "(('external_id', 'county'),)"}, + 'address': ('django.db.models.fields.CharField', [], {'max_length': '255'}), + 'city': ('django.db.models.fields.CharField', [], {'max_length': '64'}), + 'county': ('django.db.models.fields.CharField', [], {'db_index': 'True', 'max_length': '64'}), + 'external_id': ('django.db.models.fields.CharField', [], {'max_length': '128'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'image_url': ('django.db.models.fields.URLField', [], {'max_length': '255', 'blank': 'True'}), + 'location': ('django.contrib.gis.db.models.fields.PointField', [], {'null': 'True', 'blank': 'True'}), + 'name': ('django.db.models.fields.CharField', [], {'max_length': '255'}), + 'opening_date': ('django.db.models.fields.DateTimeField', [], {}), + 'phone_number': ('django.db.models.fields.CharField', [], {'max_length': '64', 'blank': 'True'}), + 'postal_code': ('django.db.models.fields.CharField', [], {'max_length': '16'}), + 'property_id': ('django.db.models.fields.CharField', [], {'max_length': '128', 'blank': 'True'}), + 'state': ('django.db.models.fields.CharField', [], {'max_length': '64'}), + 'state_id': ('django.db.models.fields.BigIntegerField', [], {}), + 'status': ('django.db.models.fields.CharField', [], {'max_length': '32', 'default': "'active'"}), + 'type': ('django.db.models.fields.PositiveIntegerField', [], {'default': '0'}), + 'update_date': ('django.db.models.fields.DateTimeField', [], {'null': 'True', 'db_index': 'True', 'blank': 'True'}) + }, + 'inspections.inspection': { + 'Meta': {'object_name': 'Inspection'}, + 'date': ('django.db.models.fields.DateTimeField', [], {'db_index': 'True'}), + 'description': ('django.db.models.fields.TextField', [], {'blank': 'True'}), + 'establishment': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'inspections'", 'to': "orm['inspections.Establishment']"}), + 'external_id': ('django.db.models.fields.CharField', [], {'max_length': '128'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'score': ('django.db.models.fields.FloatField', [], {'null': 'True', 'blank': 'True'}), + 'type': ('django.db.models.fields.PositiveIntegerField', [], {'default': '0'}), + 'update_date': ('django.db.models.fields.DateTimeField', [], {'null': 'True', 'db_index': 'True', 'blank': 'True'}) + }, + 'inspections.violation': { + 'Meta': {'object_name': 'Violation'}, + 'code': ('django.db.models.fields.CharField', [], {'max_length': '32'}), + 'date': ('django.db.models.fields.DateTimeField', [], {'db_index': 'True'}), + 'description': ('django.db.models.fields.TextField', [], {'blank': 'True'}), + 'establishment': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'violations'", 'to': "orm['inspections.Establishment']"}), + 'external_id': ('django.db.models.fields.CharField', [], {'max_length': '128'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'inspection': ('django.db.models.fields.related.ForeignKey', [], {'null': 'True', 'to': "orm['inspections.Inspection']", 'related_name': "'violations'", 'blank': 'True'}), + 'update_date': ('django.db.models.fields.DateTimeField', [], {'null': 'True', 'db_index': 'True', 'blank': 'True'}) + } + } + + complete_apps = ['inspections'] \ No newline at end of file diff --git a/inspections/models.py b/inspections/models.py index 025f693..ca3acfb 100644 --- a/inspections/models.py +++ b/inspections/models.py @@ -52,6 +52,7 @@ class Establishment(models.Model): external_id = models.CharField(ugettext_lazy("External ID"), max_length=128) state_id = models.BigIntegerField(ugettext_lazy("State ID")) property_id = models.CharField(ugettext_lazy("Property ID"), max_length=128, blank=True) + image_url = models.URLField(max_length=255, blank=True) name = models.CharField(ugettext_lazy("Name"), max_length=255) type = models.PositiveIntegerField(ugettext_lazy("Type"), default=0, choices=TYPE_CHOICES) address = models.CharField(ugettext_lazy("Address"), max_length=255) From 1ff4a0fe46b6abc1dfb44c1f8a63b79e345f2264 Mon Sep 17 00:00:00 2001 From: Victor Rocha Date: Tue, 17 Jun 2014 19:01:58 -0400 Subject: [PATCH 2/3] [refs #112] Adds image_url and property_id to admin list display --- inspections/admin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inspections/admin.py b/inspections/admin.py index a6aa1bb..ee5b443 100644 --- a/inspections/admin.py +++ b/inspections/admin.py @@ -6,7 +6,7 @@ class EstablishmentAdmin(LeafletGeoAdmin): search_fields = ('name', 'address') list_display = ('id', 'name', 'type', - 'county', 'state_id', 'point', 'update_date') + 'county', 'state_id', 'image_url', 'property_id', 'point', 'update_date') list_filter = ('county', 'postal_code') ordering = ('-update_date',) From 61b368260cdd1137163db4e8fb3a3c2f8647d32b Mon Sep 17 00:00:00 2001 From: Victor Rocha Date: Tue, 17 Jun 2014 22:30:40 -0400 Subject: [PATCH 3/3] [refs #112] Improves the admin list display for Establishments. --- inspections/admin.py | 50 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 47 insertions(+), 3 deletions(-) diff --git a/inspections/admin.py b/inspections/admin.py index ee5b443..006275d 100644 --- a/inspections/admin.py +++ b/inspections/admin.py @@ -1,13 +1,50 @@ from django.contrib import admin +from django.contrib.admin import SimpleListFilter +from django.utils.encoding import force_text +from django.utils.translation import ugettext_lazy as _ + from inspections.models import Establishment, Inspection, Violation from leaflet.admin import LeafletGeoAdmin +class ImageFilter(SimpleListFilter): + title = _('Image') + + parameter_name = 'image' + + def lookups(self, request, model_admin): + return ( + ('all', _('All')), + ('yes', _('Yes')), + ('no', _('No')), + ) + + def choices(self, cl): + for lookup, title in self.lookup_choices: + yield { + 'selected': (self.value() == force_text(lookup) or + not self.value() and force_text(lookup) == "all"), + 'query_string': cl.get_query_string({ + self.parameter_name: lookup, + }, []), + 'display': title, + } + + def queryset(self, request, queryset): + value = self.value() + if not value == 'all': + if value == 'yes': + return queryset.exclude(image_url='') + else: + return queryset.filter(image_url='') + return queryset + + class EstablishmentAdmin(LeafletGeoAdmin): search_fields = ('name', 'address') - list_display = ('id', 'name', 'type', - 'county', 'state_id', 'image_url', 'property_id', 'point', 'update_date') - list_filter = ('county', 'postal_code') + list_display = ('id', 'name', 'type', 'county', 'state_id', 'image', + 'point', 'update_date') + list_filter = ('county', ImageFilter, 'postal_code') ordering = ('-update_date',) def point(self, obj): @@ -15,6 +52,13 @@ def point(self, obj): return "{}, {}".format(obj.location[0], obj.location[1]) return None + def image(self, obj): + if not obj.image_url: + return '' + return u'' \ + u''.format(obj.image_url) + image.allow_tags = True + class InspectionAdmin(admin.ModelAdmin): search_fields = ('id', 'establishment__external_id', 'external_id',