diff --git a/src/data_collection/__init__.py b/src/data_collection/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/data_collection/admin.py b/src/data_collection/admin.py new file mode 100644 index 0000000..a9bf649 --- /dev/null +++ b/src/data_collection/admin.py @@ -0,0 +1,8 @@ +from django.contrib import admin +from . import models + +# Register your models here. + +@admin.register(models.Data) +class DataAdmin(admin.ModelAdmin): + pass \ No newline at end of file diff --git a/src/data_collection/apps.py b/src/data_collection/apps.py new file mode 100644 index 0000000..638bf5c --- /dev/null +++ b/src/data_collection/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class DataCollectionConfig(AppConfig): + name = 'data_collection' diff --git a/src/data_collection/factories.py b/src/data_collection/factories.py new file mode 100644 index 0000000..d24db16 --- /dev/null +++ b/src/data_collection/factories.py @@ -0,0 +1,9 @@ +import factory +from . import models + + +class DataFactory(factory.django.DjangoModelFactory): + class Meta: + model = models.Data + + date_created = factory.LazyFunction(datetime.datetime.now) \ No newline at end of file diff --git a/src/data_collection/helpers.py b/src/data_collection/helpers.py new file mode 100644 index 0000000..01b17e8 --- /dev/null +++ b/src/data_collection/helpers.py @@ -0,0 +1,9 @@ +from django.conf import settings +from django.db.models import Q +from rest_framework.response import Response +from .models import Data + +def log(data_type, data): + new_data = Data(data_type=data_type, log=data) + new_data.save() + return new_data \ No newline at end of file diff --git a/src/data_collection/migrations/0001_initial.py b/src/data_collection/migrations/0001_initial.py new file mode 100644 index 0000000..c693893 --- /dev/null +++ b/src/data_collection/migrations/0001_initial.py @@ -0,0 +1,26 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.7 on 2018-03-14 02:32 +from __future__ import unicode_literals + +import django.contrib.postgres.fields.jsonb +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='Data', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('data_type', models.CharField(max_length=100)), + ('date_created', models.DateTimeField(auto_now_add=True)), + ('log', django.contrib.postgres.fields.jsonb.JSONField()), + ], + ), + ] diff --git a/src/data_collection/migrations/__init__.py b/src/data_collection/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/data_collection/models.py b/src/data_collection/models.py new file mode 100644 index 0000000..8e4302a --- /dev/null +++ b/src/data_collection/models.py @@ -0,0 +1,12 @@ +from django.db import models +from django.contrib.postgres.fields import JSONField + +# Create your models here. + +class Data(models.Model): + data_type = models.CharField(max_length=100, null=False) + date_created = models.DateTimeField(auto_now_add=True) + log = JSONField() + + def __str__(self): + return '%s: %s %s' % (self.data_type, self.log, self.date_created) diff --git a/src/data_collection/serializers.py b/src/data_collection/serializers.py new file mode 100644 index 0000000..855450b --- /dev/null +++ b/src/data_collection/serializers.py @@ -0,0 +1,10 @@ +from rest_framework import serializers +from drf_writable_nested import WritableNestedModelSerializer + +from .models import Data + +class DataSerializer(WritableNestedModelSerializer): + class Meta: + model = Data + fields = ('data_type', 'date_created', 'log',) + read_only_fields = ('data_ type', 'date_created', 'log',) \ No newline at end of file diff --git a/src/data_collection/urls.py b/src/data_collection/urls.py new file mode 100644 index 0000000..fd2cd86 --- /dev/null +++ b/src/data_collection/urls.py @@ -0,0 +1,8 @@ +from django.conf.urls import url + +from . import views + + +urlpatterns = [ + url(r'^log/$', views.DataLogView.as_view(), name='log_data'), +] diff --git a/src/data_collection/views.py b/src/data_collection/views.py new file mode 100644 index 0000000..415ece7 --- /dev/null +++ b/src/data_collection/views.py @@ -0,0 +1,22 @@ +from django.conf import settings +from django.db.models import Q +from rest_framework.response import Response +from .models import Data +from .serializers import DataSerializer +from rest_framework import generics +from . import helpers + +# Create your views here. +class DataLogView(generics.CreateAPIView): + serializer_class = DataSerializer + + def post (self,request): + result = helpers.log(request.data['data_type'], request.data['log']) + + new_data = Data( + data_type = request.data['data_type'], + log = request.data['log'], + ) + new_data.save() + + return Response(DataSerializer(result).data) \ No newline at end of file diff --git a/src/pickmybruin/settings.py b/src/pickmybruin/settings.py index d50fe7e..454ddb5 100644 --- a/src/pickmybruin/settings.py +++ b/src/pickmybruin/settings.py @@ -1,9 +1,7 @@ """ Django settings for pickmybruin project. - For more information on this file, see https://docs.djangoproject.com/en/1.6/topics/settings/ - For the full list of settings and their values, see https://docs.djangoproject.com/en/1.6/ref/settings/ """ @@ -47,6 +45,7 @@ 'users', 'email_requests', 'messaging', + 'data_collection', ) AUTHENTICATION_BACKENDS = ( diff --git a/src/pickmybruin/urls.py b/src/pickmybruin/urls.py index eae490f..df9aca9 100644 --- a/src/pickmybruin/urls.py +++ b/src/pickmybruin/urls.py @@ -20,4 +20,5 @@ url(r'^o/', include('oauth2_provider.urls', namespace='oauth2_provider')), url(r'^requests/', include('email_requests.urls', namespace='email_requests')), url(r'^messaging/', include('messaging.urls', namespace='messaging')), -] + url(r'^data/', include('data_collection.urls', namespace='data_collection')), +] \ No newline at end of file diff --git a/src/users/views.py b/src/users/views.py index dd7f897..9af90c6 100644 --- a/src/users/views.py +++ b/src/users/views.py @@ -30,6 +30,7 @@ import sendgrid from sendgrid.helpers.mail import Email, Content, Substitution, Mail from pickmybruin.settings import USER_VERIFICATION_TEMPLATE, PASSWORD_RESET_TEMPLATE +from data_collection.helpers import log class UserViewSet(viewsets.ModelViewSet): """ @@ -282,7 +283,6 @@ def filter_queryset(self, queryset): ) ).filter(similarity__gte=0.10).order_by('-similarity') - if 'random' in self.request.GET: num_random = self.request.GET['random'] @@ -291,9 +291,7 @@ def filter_queryset(self, queryset): else: num_random = queryset.count() queryset = queryset.order_by('?')[:num_random] - return queryset - class MentorView(generics.RetrieveAPIView): """