Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[refs #112] Adds a new field to the Inspections model. #138

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion eatsmart/locations/durham/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand All @@ -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():
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For production debugging, it might be nice if this task had a few logger.debug or logger.info statements to track what happened.

"""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()
8 changes: 8 additions & 0 deletions eatsmart/locations/durham/utils.py
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions eatsmart/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
50 changes: 47 additions & 3 deletions inspections/admin.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,64 @@
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', '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):
if obj.location:
return "{}, {}".format(obj.location[0], obj.location[1])
return None

def image(self, obj):
if not obj.image_url:
return ''
return u'<a href="{0}"><img src="/static/admin/img/icon-yes.gif" />' \
u'</a>'.format(obj.image_url)
image.allow_tags = True


class InspectionAdmin(admin.ModelAdmin):
search_fields = ('id', 'establishment__external_id', 'external_id',
Expand Down
Original file line number Diff line number Diff line change
@@ -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']
1 change: 1 addition & 0 deletions inspections/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down