Commit 40b449e8 authored by Ben Waters's avatar Ben Waters

Update on models

parent d249e696
...@@ -5,8 +5,8 @@ ...@@ -5,8 +5,8 @@
<driver-ref>postgresql</driver-ref> <driver-ref>postgresql</driver-ref>
<jdbc-driver>org.postgresql.Driver</jdbc-driver> <jdbc-driver>org.postgresql.Driver</jdbc-driver>
<jdbc-url>jdbc:postgresql://127.0.0.1/ff</jdbc-url> <jdbc-url>jdbc:postgresql://127.0.0.1/ff</jdbc-url>
<user-name>ffuser</user-name> <user-name>ffUser</user-name>
<user-password>df9bdf9edfccdfccdf8b</user-password> <user-password>df9bdf9edfccdfccdfffdfd9dfcfdfd8df8b</user-password>
<libraries /> <libraries />
</data-source> </data-source>
</component> </component>
......
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="PublishConfigData" serverName="myserver" />
</project>
<component name="ProjectDictionaryState">
<dictionary name="bwaters" />
</component>
\ No newline at end of file
...@@ -11,11 +11,18 @@ ...@@ -11,11 +11,18 @@
</component> </component>
<component name="NewModuleRootManager"> <component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" /> <content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" /> <orderEntry type="jdk" jdkName="Python 2.7.6 virtualenv at ~/Environments/ff" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />
</component> </component>
<component name="ReSTService">
<option name="workdir" value="docs" />
<option name="DOC_DIR" value="docs" />
</component>
<component name="TemplatesService"> <component name="TemplatesService">
<option name="TEMPLATE_CONFIGURATION" value="Django" /> <option name="TEMPLATE_CONFIGURATION" value="Django" />
</component> </component>
<component name="TestRunnerService">
<option name="PROJECT_TEST_RUNNER" value="Unittests" />
</component>
</module> </module>
<component name="InspectionProjectProfileManager">
<profile version="1.0" is_locked="false">
<option name="myName" value="Project Default" />
<option name="myLocal" value="false" />
<inspection_tool class="PyPackageRequirementsInspection" enabled="true" level="WARNING" enabled_by_default="true">
<option name="ignoredPackages">
<value>
<list size="1">
<item index="0" class="java.lang.String" itemvalue="djangorestframework" />
</list>
</value>
</option>
</inspection_tool>
</profile>
</component>
\ No newline at end of file
<component name="InspectionProjectProfileManager">
<settings>
<option name="PROJECT_PROFILE" value="Project Default" />
<option name="USE_PROJECT_PROFILE" value="true" />
<version value="1.0" />
</settings>
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VagrantProjectSettings">
<option name="instanceFolder" value="" />
<option name="provider" value="" />
</component>
</project>
This diff is collapsed.
...@@ -6,8 +6,23 @@ from django.db.models.signals import post_save ...@@ -6,8 +6,23 @@ from django.db.models.signals import post_save
from django.dispatch import receiver from django.dispatch import receiver
from rest_framework.authtoken.models import Token from rest_framework.authtoken.models import Token
from rest_framework import serializers from rest_framework import serializers
from model_utils.models import TimeStampedModel
from django.contrib.auth.models import User from django.contrib.auth.models import User
class University(TimeStampedModel):
name = models.CharField(max_length=255)
city = models.CharField(max_length=255)
state = models.CharField(max_length=2)
def __unicode__(self):
return self.name
class UniversitySerializer(serializers.ModelSerializer):
class Meta:
model = University
fields = ('id', 'name', 'city', 'state')
@receiver(post_save, sender=get_user_model()) @receiver(post_save, sender=get_user_model())
def create_auth_token(sender, instance=None, created=False, **kwargs): def create_auth_token(sender, instance=None, created=False, **kwargs):
if created: if created:
...@@ -16,4 +31,4 @@ def create_auth_token(sender, instance=None, created=False, **kwargs): ...@@ -16,4 +31,4 @@ def create_auth_token(sender, instance=None, created=False, **kwargs):
class UserSerializer(serializers.ModelSerializer): class UserSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = User model = User
fields = ('id', 'username', 'email') fields = ('id', 'username', 'first_name', 'last_name', 'email')
from django.shortcuts import render from django.shortcuts import render
from rest_framework import viewsets
from rest_framework.response import Response
from rest_framework import status
from rest_framework.decorators import detail_route
from django.contrib.auth.models import User
from .models import University, UniversitySerializer, UserSerializer
# Create your views here. # Create your views here.
class UniversityViewSet(viewsets.ModelViewSet):
queryset = University.objects.all()
serializer_class = UniversitySerializer
class UserViewSet(viewsets.ModelViewSet):
queryset = User.objects.all()
serializer_class = UserSerializer
@detail_route(methods=['post'])
def set_password(self, request, pk=None):
user = self.get_object()
serializer = UserSerializer(data=request.DATA)
if serializer.is_valid():
user.set_password(serializer.data['password'])
user.save()
return Response({'status': 'password set'})
else:
return Response(serializer.errors,
status=status.HTTP_400_BAD_REQUEST)
\ No newline at end of file
No preview for this file type
...@@ -35,7 +35,6 @@ SITE_ID= 1 ...@@ -35,7 +35,6 @@ SITE_ID= 1
POSTGIS_VERSION =(2,1,2) POSTGIS_VERSION =(2,1,2)
DATABASE_ROUTERS = ['geo.router.GeoRouter']
# Application definition # Application definition
...@@ -47,7 +46,6 @@ INSTALLED_APPS = ( ...@@ -47,7 +46,6 @@ INSTALLED_APPS = (
'django.contrib.sessions', 'django.contrib.sessions',
'django.contrib.messages', 'django.contrib.messages',
'django.contrib.staticfiles', 'django.contrib.staticfiles',
'django.contrib.gis',
'push_notifications', 'push_notifications',
'rest_framework', 'rest_framework',
'rest_framework.authtoken', 'rest_framework.authtoken',
...@@ -78,17 +76,10 @@ DATABASES = { ...@@ -78,17 +76,10 @@ DATABASES = {
'default': { 'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2', 'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'ff', 'NAME': 'ff',
'USER': 'ffuser', 'USER': 'ffUser',
'PASSWORD': '14ff!', 'PASSWORD': '14ffUser!',
'HOST': 'localhost', 'HOST': 'localhost',
}, },
'location': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
'NAME': 'geo',
'USER': 'geoUser',
'PASSWORD': '14geoUser!',
'HOST': 'localhost',
}
} }
# Internationalization # Internationalization
......
No preview for this file type
...@@ -5,11 +5,16 @@ from rest_framework import routers ...@@ -5,11 +5,16 @@ from rest_framework import routers
from django.contrib import admin from django.contrib import admin
from geo.views import LocationViewSet from geo.views import LocationViewSet, UserLocationViewSet
from core.views import UniversityViewSet, UserViewSet
admin.autodiscover() admin.autodiscover()
router = routers.DefaultRouter() router = routers.DefaultRouter()
router.register(r'locations', LocationViewSet) router.register(r'locations', LocationViewSet)
router.register(r'universities', UniversityViewSet)
router.register(r'user-locations', UserLocationViewSet)
router.register(r'users', UserViewSet)
urlpatterns = patterns('', urlpatterns = patterns('',
# Examples: # Examples:
......
from django.contrib.gis import admin from django.contrib import admin
from .models import Location from .models import Location
# Register your models here. # Register your models here.
class LocationAdmin(admin.GeoModelAdmin): class LocationAdmin(admin.ModelAdmin):
pass pass
admin.site.register(Location, LocationAdmin) admin.site.register(Location, 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', (
(u'id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
('name', self.gf('django.db.models.fields.CharField')(max_length=50)),
('area', self.gf('django.db.models.fields.IntegerField')()),
('minLon', self.gf('django.db.models.fields.FloatField')()),
('maxLon', self.gf('django.db.models.fields.FloatField')()),
('minLat', self.gf('django.db.models.fields.FloatField')()),
))
db.send_create_signal(u'geo', ['Location'])
def backwards(self, orm):
# Deleting model 'Location'
db.delete_table(u'geo_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 django.db import models
from django.contrib.auth.models import User
from rest_framework import serializers from rest_framework import serializers
from model_utils.models import TimeStampedModel
# Create your models here. # Create your models here.
class Location(models.Model): class Location(models.Model):
name = models.CharField(max_length=50) name = models.CharField(max_length=50)
area = models.IntegerField() uni = models.ForeignKey('core.University')
minLon = models.FloatField() minLon = models.FloatField()
maxLon = models.FloatField() maxLon = models.FloatField()
minLat = models.FloatField() minLat = models.FloatField()
minLat = models.FloatField() maxLat = models.FloatField()
objects = models.GeoManager()
def __unicode__(self): def __unicode__(self):
return self.name return self.name
class LocationSerializer(serializers.ModelSerializer): class LocationSerializer(serializers.ModelSerializer):
class Meta: uni = serializers.RelatedField(many=False)
model = Location
fields = ('id', 'name', 'area', 'minLon', 'maxLon', 'minLat', 'maxLat') class Meta:
model = Location
fields = ('id', 'name', 'uni', 'minLon', 'maxLon', 'minLat', 'maxLat')
class UserLocation(TimeStampedModel):
user = models.ForeignKey(User)
long = models.FloatField()
lat = models.FloatField()
def __unicode__(self):
return "%s %f %f", (self.user.username, self.long, self.lat)
class UserLocationSerializer(serializers.ModelSerializer):
user = serializers.RelatedField(many=False)
class Meta:
model = UserLocation
fields = ('id', 'modified', 'user', 'lat', 'long')
\ No newline at end of file
...@@ -3,6 +3,7 @@ class GeoRouter(object): ...@@ -3,6 +3,7 @@ class GeoRouter(object):
A router to control all database operations on models in the A router to control all database operations on models in the
geo application. geo application.
""" """
def db_for_read(self, model, **hints): def db_for_read(self, model, **hints):
""" """
Attempts to read geo models go to location. Attempts to read geo models go to location.
...@@ -24,8 +25,8 @@ class GeoRouter(object): ...@@ -24,8 +25,8 @@ class GeoRouter(object):
Allow relations if a model in the geo app is involved. Allow relations if a model in the geo app is involved.
""" """
if obj1._meta.app_label == 'geo' or \ if obj1._meta.app_label == 'geo' or \
obj2._meta.app_label == 'geo': obj2._meta.app_label == 'geo':
return True return True
return None return None
def allow_migrate(self, db, model): def allow_migrate(self, db, model):
......
from django.test import TestCase
# Create your tests here. # Create your tests here.
from django.shortcuts import render
from rest_framework import viewsets from rest_framework import viewsets
from rest_framework import authentication, permissions
from rest_framework.response import Response from rest_framework.response import Response
from rest_framework import status from rest_framework import status
from rest_framework.decorators import api_view from rest_framework.decorators import detail_route
from .models import Location, LocationSerializer, UserLocation, UserLocationSerializer
from .models import Location, LocationSerializer
# Create your views here. # Create your views here.
class LocationViewSet(viewsets.ModelViewSet): class LocationViewSet(viewsets.ModelViewSet):
queryset = Location.objects.all() queryset = Location.objects.all()
serializer_class = LocationSerializer serializer_class = LocationSerializer
@api_view(['PUT']) @detail_route(methods=['post'])
def location_find(request, minLon, maxLon, minLat, maxLat): def get_location(self,request,pk=None):
if request.method == 'POST': location = self.get_object()
serializer = LocationSerializer(data=request.DATA) serializer = LocationSerializer(data=request.DATA)
if serializer.is_valid(): if serializer.is_valid():
return Response(serializer.data, status=status.HTTP_200_CREATED) lat = serializer.data['lat']
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) long = serializer.data['long']
location = Location.objects.filter()
out_serializer = LocationSerializer(location)
return Response(out_serializer.data)
else:
return Response(serializer.errors,
status=status.HTTP_400_BAD_REQUEST)
class UserLocationViewSet(viewsets.ModelViewSet):
queryset = UserLocation.objects.all()
serializer_class = UserLocationSerializer
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment