Commit 0a54fbd3 authored by Ben Waters's avatar Ben Waters

Working on south migrations

parent 242ecdab
......@@ -31,6 +31,12 @@ TEMPLATE_DIRS =(os.path.join('templates'))
STATIC_ROOT = 'static'
DATABASE_ROUTERS = ['geo.router.GeoRouter']
# Application definition
......@@ -78,8 +84,10 @@ DATABASES = {
'location': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
'NAME': 'geodjango',
'USER': 'geo',
'NAME': 'geo',
'USER': 'geoUser',
'PASSWORD': '14geoUser!',
'HOST': 'localhost',
No preview for this file type
from django.contrib import admin
from django.contrib.gis import admin
from .models import Location
# Register your models here.
class LocationAdmin(admin.ModelAdmin):
class LocationAdmin(admin.GeoModelAdmin):
pass, LocationAdmin)
# -*- 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 model 'Location'
db.create_table(u'geo_location', (
db.send_create_signal(u'geo', ['Location'])
def backwards(self, orm):
# Deleting model 'Location'
models = {
u'geo.location': {
'Meta': {'object_name': 'Location'},
'area': ('django.db.models.fields.IntegerField', [], {}),
u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'maxLon': ('django.db.models.fields.FloatField', [], {}),
'minLat': ('django.db.models.fields.FloatField', [], {}),
'minLon': ('django.db.models.fields.FloatField', [], {}),
'name': ('django.db.models.fields.CharField', [], {'max_length': '50'})
complete_apps = ['geo']
\ No newline at end of file
from django.contrib.gis.db import models
from rest_framework import serializers
# Create your models here.
class Location(models.Model):
name = models.CharField(max_length=50)
area = models.IntegerField()
lon = models.FloatField()
lat = models.FloatField()
minLon = models.FloatField()
maxLon = models.FloatField()
minLat = models.FloatField()
minLat = models.FloatField()
objects = models.GeoManager()
......@@ -15,4 +18,4 @@ class Location(models.Model):
class LocationSerializer(serializers.ModelSerializer):
class Meta:
model = Location
fields = ('id', 'name', 'area', 'lon', 'lat')
fields = ('id', 'name', 'area', 'minLon', 'maxLon', 'minLat', 'maxLat')
class GeoRouter(object):
A router to control all database operations on models in the
geo application.
def db_for_read(self, model, **hints):
Attempts to read geo models go to location.
if model._meta.app_label == 'geo':
return 'location'
return None
def db_for_write(self, model, **hints):
Attempts to write geo models go to location.
if model._meta.app_label == 'geo':
return 'location'
return None
def allow_relation(self, obj1, obj2, **hints):
Allow relations if a model in the geo app is involved.
if obj1._meta.app_label == 'geo' or \
obj2._meta.app_label == 'geo':
return True
return None
def allow_migrate(self, db, model):
Make sure the geo app only appears in the 'location'
if db == 'location':
return model._meta.app_label == 'geo'
elif model._meta.app_label == 'geo':
return False
return None
from django.shortcuts import render
from rest_framework import viewsets
from rest_framework import permissions
from rest_framework import authentication, permissions
from rest_framework.response import Response
from rest_framework import status
from rest_framework.decorators import api_view
from .models import Location, LocationSerializer
......@@ -8,7 +11,13 @@ from .models import Location, LocationSerializer
class LocationViewSet(viewsets.ModelViewSet):
queryset = Location.objects.all()
serializer_class = LocationSerializer
permission_classes = (permissions.IsAuthenticatedOrReadOnly,
def location_find(request, minLon, maxLon, minLat, maxLat):
if request.method == 'POST':
serializer = LocationSerializer(data=request.DATA)
if serializer.is_valid():
return Response(, status=status.HTTP_200_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment