Commit 775c52a9 authored by Laury Bueno's avatar Laury Bueno
Browse files

[Stats] Add a user-access endpoint

parent 73b6d16a
......@@ -10,5 +10,6 @@ class CoursesStatsConfig(AppConfig):
from actstream import registry
registry.register(get_user_model())
registry.register('courses.Course')
registry.register('stats.AccessibleArea')
registry.register('discussion.Topic')
registry.register('discussion.Forum')
......@@ -14,3 +14,6 @@ class AccessibleArea(models.Model):
)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __str__(self):
return self.name
......@@ -7,3 +7,7 @@ class UserActionsSerializer(serializers.Serializer):
action_object_type = serializers.CharField(required=False, max_length=100)
target_id = serializers.IntegerField(required=False)
target_type = serializers.CharField(required=False, max_length=100)
class UserAccessSerializer(serializers.Serializer):
area = serializers.CharField(required=True, max_length=100)
from django.conf.urls import url, include
from courses.stats.views import (
UserActionsView,
UserAccessView
)
app_name = 'courses.stats'
urlpatterns = [
url(r'^user-actions/', UserActionsView.as_view(), name='stats'),
url(r'^user-actions/', UserActionsView.as_view(), name='user-actions'),
url(r'^user-access/', UserAccessView.as_view(), name='user-access'),
url(r'^activity-stream/', include('actstream.urls')),
]
......@@ -4,7 +4,8 @@ from rest_framework.response import Response
from rest_framework.permissions import IsAuthenticated
from discussion.models import Forum, Topic
from .serializers import UserActionsSerializer
from .serializers import UserActionsSerializer, UserAccessSerializer
from .models import AccessibleArea
from ..models import Course
......@@ -37,3 +38,26 @@ class UserActionsView(views.APIView):
action.send(request.user, verb=data['verb'], action_object=action_object, target=target)
return Response(None, status=status.HTTP_201_CREATED)
class UserAccessView(views.APIView):
permission_classes = [permissions.IsAuthenticated]
def post(self, request, format=None):
# Validate incoming data
serializer = UserAccessSerializer(data=request.data)
if not serializer.is_valid():
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
data = serializer.data
# Get the actual model object for action
try:
action_object = AccessibleArea.objects.get(slug=data['area'])
except AccessibleArea.DoesNotExist:
return Response({'error': 'Specified area not found'}, status=status.HTTP_404_NOT_FOUND)
# Save the action
action.send(request.user, verb='accessed', action_object=action_object)
return Response(None, status=status.HTTP_201_CREATED)
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