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

Update on models

parent d249e696
......@@ -5,8 +5,8 @@
<driver-ref>postgresql</driver-ref>
<jdbc-driver>org.postgresql.Driver</jdbc-driver>
<jdbc-url>jdbc:postgresql://127.0.0.1/ff</jdbc-url>
<user-name>ffuser</user-name>
<user-password>df9bdf9edfccdfccdf8b</user-password>
<user-name>ffUser</user-name>
<user-password>df9bdf9edfccdfccdfffdfd9dfcfdfd8df8b</user-password>
<libraries />
</data-source>
</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 @@
</component>
<component name="NewModuleRootManager">
<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" />
</component>
<component name="ReSTService">
<option name="workdir" value="docs" />
<option name="DOC_DIR" value="docs" />
</component>
<component name="TemplatesService">
<option name="TEMPLATE_CONFIGURATION" value="Django" />
</component>
<component name="TestRunnerService">
<option name="PROJECT_TEST_RUNNER" value="Unittests" />
</component>
</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
from django.dispatch import receiver
from rest_framework.authtoken.models import Token
from rest_framework import serializers
from model_utils.models import TimeStampedModel
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())
def create_auth_token(sender, instance=None, created=False, **kwargs):
if created:
......@@ -16,4 +31,4 @@ def create_auth_token(sender, instance=None, created=False, **kwargs):
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ('id', 'username', 'email')
fields = ('id', 'username', 'first_name', 'last_name', 'email')
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.
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
POSTGIS_VERSION =(2,1,2)
DATABASE_ROUTERS = ['geo.router.GeoRouter']
# Application definition
......@@ -47,7 +46,6 @@ INSTALLED_APPS = (
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.gis',
'push_notifications',
'rest_framework',
'rest_framework.authtoken',
......@@ -78,17 +76,10 @@ DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'ff',
'USER': 'ffuser',
'PASSWORD': '14ff!',
'USER': 'ffUser',
'PASSWORD': '14ffUser!',
'HOST': 'localhost',
},
'location': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
'NAME': 'geo',
'USER': 'geoUser',
'PASSWORD': '14geoUser!',
'HOST': 'localhost',
}
}
# Internationalization
......
No preview for this file type
......@@ -5,11 +5,16 @@ from rest_framework import routers
from django.contrib import admin
from geo.views import LocationViewSet
from geo.views import LocationViewSet, UserLocationViewSet
from core.views import UniversityViewSet, UserViewSet
admin.autodiscover()
router = routers.DefaultRouter()
router.register(r'locations', LocationViewSet)
router.register(r'universities', UniversityViewSet)
router.register(r'user-locations', UserLocationViewSet)
router.register(r'users', UserViewSet)
urlpatterns = patterns('',
# Examples:
......
from django.contrib.gis import admin
from django.contrib import admin
from .models import Location
# Register your models here.
class LocationAdmin(admin.GeoModelAdmin):
pass
class LocationAdmin(admin.ModelAdmin):
pass
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 model_utils.models import TimeStampedModel
# Create your models here.
class Location(models.Model):
name = models.CharField(max_length=50)
area = models.IntegerField()
uni = models.ForeignKey('core.University')
minLon = models.FloatField()
maxLon = models.FloatField()
minLat = models.FloatField()
minLat = models.FloatField()
objects = models.GeoManager()
maxLat = models.FloatField()
def __unicode__(self):
return self.name
class LocationSerializer(serializers.ModelSerializer):
class Meta:
model = Location
fields = ('id', 'name', 'area', 'minLon', 'maxLon', 'minLat', 'maxLat')
uni = serializers.RelatedField(many=False)
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):
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.
......@@ -24,8 +25,8 @@ class GeoRouter(object):
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
obj2._meta.app_label == 'geo':
return True
return None
def allow_migrate(self, db, model):
......
from django.test import TestCase
# Create your tests here.
from django.shortcuts import render
from rest_framework import viewsets
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 rest_framework.decorators import detail_route
from .models import Location, LocationSerializer, UserLocation, UserLocationSerializer
from .models import Location, LocationSerializer
# Create your views here.
class LocationViewSet(viewsets.ModelViewSet):
class LocationViewSet(viewsets.ModelViewSet):
queryset = Location.objects.all()
serializer_class = LocationSerializer
@api_view(['PUT'])
def location_find(request, minLon, maxLon, minLat, maxLat):
if request.method == 'POST':
@detail_route(methods=['post'])
def get_location(self,request,pk=None):
location = self.get_object()
serializer = LocationSerializer(data=request.DATA)
if serializer.is_valid():
return Response(serializer.data, status=status.HTTP_200_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
lat = serializer.data['lat']
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