Skip to content

Commit

Permalink
first stab at api
Browse files Browse the repository at this point in the history
  • Loading branch information
rloomans committed Jun 17, 2016
1 parent 587c9c1 commit 0e7a862
Show file tree
Hide file tree
Showing 8 changed files with 264 additions and 43 deletions.
12 changes: 8 additions & 4 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
Django==1.9.7
django-bootstrap3==7.0.1
dj-database-url==0.4.1
dj-static==0.0.6
django-crispy-forms==1.6.0
django-csp==2.0.3
django-filter==0.13.0
djangorestframework==3.3.3
django-secure==1.0.1
django-toolbelt==0.0.1
dj-database-url==0.4.1
dj-static==0.0.6
gunicorn==19.6.0
https://google-visualization-python.googlecode.com/files/gviz_api_py-1.8.2.tar.gz
markdown==2.6.6
Pillow==3.2.0
poster==0.8.1
py-bcrypt==0.4
psycopg2==2.6.1
py-bcrypt==0.4
pytz==2016.4
requests==2.10.0
static==1.1.1
Unirest==1.1.7
wsgiref==0.1.2
whitenoise==3.2
wsgiref==0.1.2
44 changes: 44 additions & 0 deletions teamtemp/responses/migrations/0003_auto_20160617_1112.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.7 on 2016-06-17 01:12
from __future__ import unicode_literals

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('responses', '0002_auto_20160614_2324'),
]

operations = [
migrations.AlterModelOptions(
name='teamresponsehistory',
options={'verbose_name_plural': 'Team response histories'},
),
migrations.AlterModelOptions(
name='teams',
options={'verbose_name_plural': 'Teams'},
),
migrations.AlterField(
model_name='teamresponsehistory',
name='request',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='team_response_histories', to='responses.TeamTemperature'),
),
migrations.AlterField(
model_name='teams',
name='request',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='teams', to='responses.TeamTemperature'),
),
migrations.AlterField(
model_name='teamtemperature',
name='creator',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='team_temperatures', to='responses.User'),
),
migrations.AlterField(
model_name='temperatureresponse',
name='request',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='temperature_responses', to='responses.TeamTemperature'),
),
]
49 changes: 49 additions & 0 deletions teamtemp/responses/migrations/0004_auto_20160617_1324.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.7 on 2016-06-17 03:24
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('responses', '0003_auto_20160617_1112'),
]

operations = [
migrations.AlterField(
model_name='teamresponsehistory',
name='team_name',
field=models.CharField(db_index=True, max_length=64, null=True),
),
migrations.AlterField(
model_name='teams',
name='team_name',
field=models.CharField(db_index=True, max_length=64, null=True),
),
migrations.AlterField(
model_name='temperatureresponse',
name='archive_date',
field=models.DateTimeField(db_index=True, null=True),
),
migrations.AlterField(
model_name='temperatureresponse',
name='archived',
field=models.BooleanField(db_index=True, default=False),
),
migrations.AlterField(
model_name='temperatureresponse',
name='team_name',
field=models.CharField(db_index=True, max_length=64, null=True),
),
migrations.AlterField(
model_name='wordcloudimage',
name='word_list',
field=models.CharField(db_index=True, max_length=5000),
),
migrations.AlterUniqueTogether(
name='teams',
unique_together=set([('request', 'team_name')]),
),
]
33 changes: 17 additions & 16 deletions teamtemp/responses/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

class WordCloudImage(models.Model):
creation_date = models.DateField()
word_list = models.CharField(max_length=5000)
word_list = models.CharField(max_length=5000, db_index=True)
image_url = models.CharField(max_length=255)

def __unicode__(self):
Expand All @@ -20,7 +20,7 @@ def __unicode__(self):
class TeamTemperature(models.Model):
id = models.CharField(max_length=8, primary_key=True)
creation_date = models.DateField()
creator = models.ForeignKey(User)
creator = models.ForeignKey(User, related_name="team_temperatures")
password = models.CharField(max_length=256)
archive_schedule = models.IntegerField(default=0)
archive_date = models.DateTimeField(null=True)
Expand All @@ -40,32 +40,32 @@ def _stats_for(self, query_set):

def stats(self):
result = dict()
allresponses = self.temperatureresponse_set.filter(archived = False)
allresponses = self.temperature_responses.filter(archived = False)
return self._stats_for(allresponses)

def team_stats(self, team_name):
result = dict()
allresponses = self.temperatureresponse_set.filter(team_name__in = team_name, archived = False)
allresponses = self.temperature_responses.filter(team_name__in = team_name, archived = False)
return self._stats_for(allresponses)

def archive_stats(self, archive_date):
result = dict()
allresponses = self.temperatureresponse_set.filter(archived = True,archive_date = archive_date)
allresponses = self.temperature_responses.filter(archived = True,archive_date = archive_date)
return self._stats_for(allresponses)

def archive_team_stats(self, team_name, archive_date):
result = dict()
allresponses = self.temperatureresponse_set.filter(team_name__in = team_name, archive_date = archive_date, archived = True)
allresponses = self.temperature_responses.filter(team_name__in = team_name, archive_date = archive_date, archived = True)
return self._stats_for(allresponses)

def accumulated_stats(self, start_date, end_date):
result = dict()
allresponses = self.temperatureresponse_set.filter(response_date__gte=end_date, response_date__lte=start_date)
allresponses = self.temperature_responses.filter(response_date__gte=end_date, response_date__lte=start_date)
return self._stats_for(allresponses)

def accumulated_team_stats(self, team_name, start_date, end_date):
result = dict()
allresponses = self.temperatureresponse_set.filter(team_name__in = team_name, response_date__gte=end_date, response_date__lte=start_date)
allresponses = self.temperature_responses.filter(team_name__in = team_name, response_date__gte=end_date, response_date__lte=start_date)
return self._stats_for(allresponses)

def __unicode__(self):
Expand All @@ -76,14 +76,14 @@ def __unicode__(self):


class TemperatureResponse(models.Model):
request = models.ForeignKey(TeamTemperature)
request = models.ForeignKey(TeamTemperature, related_name="temperature_responses")
responder = models.ForeignKey(User)
score = models.IntegerField()
word = models.CharField(max_length=32)
team_name = models.CharField(max_length=64, null=True)
archived = models.BooleanField(default=False)
team_name = models.CharField(max_length=64, null=True, db_index=True)
archived = models.BooleanField(default=False, db_index=True)
response_date = models.DateTimeField(null=True)
archive_date = models.DateTimeField(null=True)
archive_date = models.DateTimeField(null=True, db_index=True)

def __unicode__(self):
return u"{}: {} {} {} {} {} {} {} {}".format(self.id, self.request.id,
Expand All @@ -96,11 +96,11 @@ class TeamResponseHistory(models.Model):
class Meta:
verbose_name_plural = "Team response histories"

request = models.ForeignKey(TeamTemperature)
request = models.ForeignKey(TeamTemperature, related_name="team_response_histories")
average_score = models.DecimalField(decimal_places=5, max_digits=10)
word_list = models.CharField(max_length=5000)
responder_count = models.IntegerField()
team_name = models.CharField(max_length=64, null=True)
team_name = models.CharField(max_length=64, null=True, db_index=True)
archive_date = models.DateTimeField()

def __unicode__(self):
Expand All @@ -112,9 +112,10 @@ def __unicode__(self):
class Teams(models.Model):
class Meta:
verbose_name_plural = "Teams"
unique_together = ("request", "team_name")

request = models.ForeignKey(TeamTemperature)
team_name = models.CharField(max_length=64, null=True)
request = models.ForeignKey(TeamTemperature, related_name="teams")
team_name = models.CharField(max_length=64, null=True, db_index=True)
dept_name = models.CharField(max_length=64, null=True)
site_name = models.CharField(max_length=64, null=True)
region_name = models.CharField(max_length=64, null=True)
Expand Down
50 changes: 50 additions & 0 deletions teamtemp/responses/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from rest_framework import serializers
from models import User, TeamTemperature, TemperatureResponse, TeamResponseHistory, Teams, WordCloudImage


class UserSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = User
fields = ('id',)


class WordCloudImageSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = WordCloudImage
fields = ('creation_date', 'word_list', 'image_url')


class TeamTemperatureSerializer(serializers.HyperlinkedModelSerializer):

class Meta:
model = TeamTemperature
fields = ('id', 'creation_date', 'creator', 'archive_schedule',
'archive_date', 'survey_type', 'default_tz',
'max_word_count', 'dept_names', 'site_names',
'region_names', 'teams', 'team_response_histories',
'temperature_responses')


class TemperatureResponseSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = TemperatureResponse
fields = ('request', 'responder', 'score', 'word', 'team_name',
'archived', 'response_date', 'archive_date')


class TeamSerializer(serializers.HyperlinkedModelSerializer):

class Meta:
model = Teams
fields = ('request', 'team_name', 'dept_name', 'site_name',
'region_name')


class TeamResponseHistorySerializer(serializers.HyperlinkedModelSerializer):

class Meta:
model = TeamResponseHistory
fields = ('request', 'average_score', 'word_list', 'responder_count',
'team_name', 'archive_date')


11 changes: 11 additions & 0 deletions teamtemp/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@
'django.contrib.admindocs',
'teamtemp.responses',
'bootstrap3',
'rest_framework',
'crispy_forms',
)

PASSWORD_HASHERS = (
Expand Down Expand Up @@ -193,3 +195,12 @@
CSP_STYLE_SRC = ('*.google.com', '*.googleapis.com', 'code.jquery.com', 'maxcdn.bootstrapcdn.com', "'unsafe-inline'","'self'",)
CSP_IMG_SRC = ("'self'",'data:','blob:',)
CSP_FONT_SRC = ('maxcdn.bootstrapcdn.com', "'self'",)

REST_FRAMEWORK = {
# Use Django's standard `django.contrib.auth` permissions,
# or allow read-only access for unauthenticated users.
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly'
]
}

14 changes: 13 additions & 1 deletion teamtemp/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,18 @@
from django.views.generic.base import RedirectView
from django.contrib.staticfiles.storage import staticfiles_storage
from django.contrib import admin as djadmin
from rest_framework import routers

from teamtemp.views import home, admin, submit, reset, bvc, team, cron, set, filter, healthcheck, robots_txt, WordCloudImageViewSet, UserViewSet, TeamTemperatureViewSet, TemperatureResponseViewSet, TeamResponseHistoryViewSet, TeamsViewSet

router = routers.DefaultRouter()
router.register(r'word_cloud_images', WordCloudImageViewSet)
router.register(r'users', UserViewSet)
router.register(r'team_temperatures', TeamTemperatureViewSet)
router.register(r'temperature_responses', TemperatureResponseViewSet)
router.register(r'team_response_histories', TeamResponseHistoryViewSet)
router.register(r'teams', TeamsViewSet)

from teamtemp.views import home, admin, submit, reset, bvc, team, cron, set, filter, healthcheck, robots_txt

urlpatterns = [
url(r'^cs$', home, {'survey_type' : 'CUSTOMERFEEDBACK'}),
Expand Down Expand Up @@ -48,4 +58,6 @@
url(r'^robots\.txt', robots_txt, name='robots_txt'),
url(r'^favicon\.ico', RedirectView.as_view(url=staticfiles_storage.url('favicon.ico')), name='favicon'),
url(r'^djadmin/', include(djadmin.site.urls)),
url(r'^api/', include(router.urls)),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
]
Loading

0 comments on commit 0e7a862

Please sign in to comment.