From da674829656df88a001f1d911459f44a4fc5da66 Mon Sep 17 00:00:00 2001 From: Sanj Date: Fri, 19 Aug 2011 18:35:24 +0530 Subject: [PATCH] basic imports / admin working --- gazetteer/places/admin.py | 83 ++++++-------------------------- gazetteer/places/imports.py | 48 ++++++++++++------- gazetteer/places/models.py | 96 ++++++++++++++++++++++++++++++++++++- gazetteer/settings.py | 2 +- 4 files changed, 143 insertions(+), 86 deletions(-) diff --git a/gazetteer/places/admin.py b/gazetteer/places/admin.py index d3d05c8..17fe197 100644 --- a/gazetteer/places/admin.py +++ b/gazetteer/places/admin.py @@ -1,73 +1,20 @@ -from django.contrib import admin +from django.contrib.gis import admin from models import * -class PlaceAdmin(admin.ModelAdmin): - pass -# admin.site.register(Occupation, OccupationAdmin) -# admin.site.register(Relation, RelationAdmin) -admin.site.register(Place, PlaceAdmin) +class FeatureNamesInline(admin.StackedInline): + model = Name + extra = 3 -''' -#class NicknameInline(admin.StackedInline): -# model = Nickname -# extra = 3 +class FeatureAdmin(admin.GeoModelAdmin): + search_fields = ['preferred_name'] + list_filter = ('feature_type',) + inlines = [FeatureNamesInline] + list_per_page = 500 +# map_template = 'gis/admin/osm.html' +# default_lon = 72.855211097628413 +# default_lat = 19.415775291486027 +# default_zoom = 4 +# extra_js = ['http://openstreetmap.org/openlayers/OpenStreetMap.js'] -#class LinkInlineModelAdmin(admin.InlineModelAdmin): -# pass +admin.site.register(Feature, FeatureAdmin) -#class LinkInline(LinkInlineModelAdmin): -# model = Link -# extra = 3 - -class ProfileInline(admin.StackedInline): - model = Person - extra = 1 - -class PerformanceAdmin(admin.ModelAdmin): - filter_horizontal = ('links', 'images') - -class EventAdmin(admin.ModelAdmin): - filter_horizontal = ('links',) - -class ProfileAdmin(admin.ModelAdmin): - filter_horizontal = ('links', 'files', 'images',) -# inlines = [NicknameInline] -# inlines = [LinkInline] - -class VenueAdmin(admin.ModelAdmin): - ordering = ('name',) - prepopulated_fields = {'slug': ('name',)} - filter_horizontal = ('links', 'images') - -class TheatreGroupAdmin(admin.ModelAdmin): - prepopulated_fields = {'slug': ('name',)} - filter_horizontal = ('links', 'files', 'images') - -class ProductionAdmin(admin.ModelAdmin): - prepopulated_fields = {'slug': ('title',)} - filter_horizontal = ('links', 'files', 'images',) - inlines = [ProfileInline] - -class ScriptAdmin(admin.ModelAdmin): - filter_horizontal = ('links', 'downloads') - - -#admin.site.register(Nickname3) -#admin.site.register(Nickname2) -admin.site.register(Performance, PerformanceAdmin) -admin.site.register(Event, EventAdmin) -admin.site.register(Location) -admin.site.register(ProfileProfile) -admin.site.register(ProfileGroup) -admin.site.register(Script, ScriptAdmin) -admin.site.register(Production, ProductionAdmin) -admin.site.register(Image) -admin.site.register(File) -admin.site.register(Link) -admin.site.register(RandomQuote) -admin.site.register(ProfileProduction) -admin.site.register(Nickname) -admin.site.register(TheatreGroup, TheatreGroupAdmin) -admin.site.register(Venue, VenueAdmin) -admin.site.register(Profile, ProfileAdmin) -''' diff --git a/gazetteer/places/imports.py b/gazetteer/places/imports.py index 078fecc..d91e729 100644 --- a/gazetteer/places/imports.py +++ b/gazetteer/places/imports.py @@ -1,29 +1,45 @@ import csv from settings import DATA_DIR from os.path import join -from places.models import Place, FeatureCode +from places.models import Feature, FeatureType from django.contrib.gis.geos import Point -def import_gazetteer(): - f = open(join(DATA_DIR, "gazetteer.txt")) +def import_ftypes(f): t = csv.reader(f, delimiter="\t") for row in t: - p = Place() - if Place.objects.filter(url=row[0]).count() > 0: + ft = FeatureType() + ft.feature_class = row[0] + ft.code = row[1] + ft.name = row[2] + try: + ft.description = row[3] + except: + ft.description = '' + ft.save() + print "saved " + ft.name + + +def import_gazetteer(f, limit): + t = csv.reader(f, delimiter="\t") + i = 0 + for row in t: + ft = Feature() + if Feature.objects.filter(url=row[0]).count() > 0: print "duplicate row " + row[0] else: - p.url = row[0] - p.name = row[1] + ft.url = row[0] + ft.preferred_name = row[1] + try: - fcode = FeatureCode.objects.get(pk=row[2]) + fcode = FeatureType.objects.get(code=row[2]) except: - print row[2] - continue - p.fcode = fcode - p.country = row[3] - p.admin1 = row[4] - p.point = Point(float(row[6]), float(row[5])) - p.save() - print "saved " + p.name + fcode = None + ft.feature_type = fcode + ft.geometry = Point(float(row[6]), float(row[5])) + ft.save() + print "saved " + ft.preferred_name + i += 1 + if i > limit: + break diff --git a/gazetteer/places/models.py b/gazetteer/places/models.py index ef9b675..5ff5cd5 100644 --- a/gazetteer/places/models.py +++ b/gazetteer/places/models.py @@ -3,6 +3,100 @@ from django.contrib.gis.db import models # Create your models here. +class AuthorityRecord(models.Model): + uri = models.CharField(max_length=512) + preferred_name = models.CharField(max_length=255) + features = models.ManyToManyField("Feature", blank=True) + + def __unicode__(self): + return self.preferred_name + + +class Feature(models.Model): + url = models.CharField(max_length=512, unique=True) + preferred_name = models.CharField(max_length=512) + feature_type = models.ForeignKey("FeatureType", null=True, blank=True) + geometry = models.GeometryField() + is_primary = models.BooleanField(default=True) + time_frame = models.OneToOneField("TimeFrame", null=True, blank=True) + relationships = models.ManyToManyField("Feature", through='Relationship', blank=True) + objects = models.GeoManager() + + class Meta: + ordering = ['preferred_name'] + + def __unicode__(self): + return self.preferred_name + + + +LANGUAGE_CHOICES = ( + ('en', 'English'), + ('es', 'Spanish'), +) + +NAME_TYPE_CHOICES = ( + ('official', 'official'), + ('historic', 'historic'), + ('colloquial', 'colloquial'), + ('-blank-', '-blank-'), +) + +class Name(models.Model): + feature = models.ForeignKey(Feature) + text = models.CharField(max_length=512) + language = models.CharField(max_length=2, choices=LANGUAGE_CHOICES) + name_type = models.CharField(max_length=64, choices=NAME_TYPE_CHOICES) + + def __unicode__(self): + return self.text + +GRANULARITY_CHOICES = ( + ('day', 'day'), + ('month', 'month'), + ('year', 'year'), + ('decade', 'decade'), + ('century', 'century'), +) + +class TimeFrame(models.Model): + description = models.TextField(blank=True) + start_date = models.DateField() + end_date = models.DateField() #add default to now + start_granularity = models.CharField(max_length=64, choices=GRANULARITY_CHOICES) + end_granularity = models.CharField(max_length=64, choices=GRANULARITY_CHOICES) + + def __unicode__(self): + return self.description + + +class FeatureType(models.Model): + feature_class = models.CharField(max_length=1) + code = models.CharField(max_length=5, unique=True) + name = models.CharField(max_length=128) + description = models.TextField() + + class Meta: + ordering = ['code'] + + def __unicode__(self): + return self.code + ": " + self.name + + +RELATIONSHIP_CHOICES = ( + ('conflates', 'conflates'), + ('contains', 'contains'), + ('subsumes', 'subsumes'), + ('supersedes', 'supersedes'), +) + +class Relationship(models.Model): + feature1 = models.ForeignKey(Feature, related_name="feauture_from") + feature2 = models.ForeignKey(Feature, related_name="feature_to") + relationship_type = models.CharField(max_length=64, choices=RELATIONSHIP_CHOICES) + + +''' class Place(models.Model): url = models.CharField(max_length=255, primary_key=True, unique=True) name = models.CharField(max_length=255) @@ -23,6 +117,6 @@ class FeatureCode(models.Model): def __unicode__(self): return self.fcode + ": " + self.short_name - +''' diff --git a/gazetteer/settings.py b/gazetteer/settings.py index 6bee380..9cd5ce4 100644 --- a/gazetteer/settings.py +++ b/gazetteer/settings.py @@ -14,7 +14,7 @@ MANAGERS = ADMINS DATABASES = { 'default': { 'ENGINE': 'django.contrib.gis.db.backends.postgis', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'. - 'NAME': 'gazetteer', # Or path to database file if using sqlite3. + 'NAME': 'gazzette', # Or path to database file if using sqlite3. 'USER': 'sanj', # Not used with sqlite3. 'PASSWORD': '', # Not used with sqlite3. 'HOST': '', # Set to empty string for localhost. Not used with sqlite3.