Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logging Feature #72

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Empty file added src/data_collection/__init__.py
Empty file.
8 changes: 8 additions & 0 deletions src/data_collection/admin.py
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions src/data_collection/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class DataCollectionConfig(AppConfig):
name = 'data_collection'
9 changes: 9 additions & 0 deletions src/data_collection/factories.py
Original file line number Diff line number Diff line change
@@ -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)
9 changes: 9 additions & 0 deletions src/data_collection/helpers.py
Original file line number Diff line number Diff line change
@@ -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
26 changes: 26 additions & 0 deletions src/data_collection/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -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()),
],
),
]
Empty file.
12 changes: 12 additions & 0 deletions src/data_collection/models.py
Original file line number Diff line number Diff line change
@@ -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)
10 changes: 10 additions & 0 deletions src/data_collection/serializers.py
Original file line number Diff line number Diff line change
@@ -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',)
8 changes: 8 additions & 0 deletions src/data_collection/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.conf.urls import url

from . import views


urlpatterns = [
url(r'^log/$', views.DataLogView.as_view(), name='log_data'),
]
22 changes: 22 additions & 0 deletions src/data_collection/views.py
Original file line number Diff line number Diff line change
@@ -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)
3 changes: 1 addition & 2 deletions src/pickmybruin/settings.py
Original file line number Diff line number Diff line change
@@ -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/
"""
Expand Down Expand Up @@ -46,6 +44,7 @@
'users',
'email_requests',
'messaging',
'data_collection',
)

AUTHENTICATION_BACKENDS = (
Expand Down
3 changes: 2 additions & 1 deletion src/pickmybruin/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')),
]
5 changes: 4 additions & 1 deletion src/users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,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):
"""
Expand Down Expand Up @@ -236,6 +237,8 @@ def filter_queryset(self, queryset):
if 'year' in self.request.GET:
year = self.request.GET['year']

log('Majors', {'major': major})

q = Q()
if major != 'all':
q &= Q(major__name=major)
Expand Down Expand Up @@ -285,4 +288,4 @@ def post (self,request):
mentor.active = True
mentor.save()

return Response(MentorSerializer(mentor).data)
return Response(MentorSerializer(mentor).data)