#!/usr/bin/env python

from tastypie import fields
from tastypie.authorization import DjangoAuthorization
from tastypie.exceptions import ImmediateHttpResponse
from tastypie.http import HttpConflict

from panda.api.utils import PandaApiKeyAuthentication, PandaModelResource, PandaSerializer
from django.db import IntegrityError
from panda.models import ActivityLog 

class ActivityLogResource(PandaModelResource):
    """
    API resource for DataUploads.
    """
    from panda.api.users import UserResource

    creator = fields.ForeignKey(UserResource, 'user', full=True)

    class Meta:
        queryset = ActivityLog.objects.all()
        resource_name = 'activity_log'
        allowed_methods = ['get', 'post']

        authentication = PandaApiKeyAuthentication()
        authorization = DjangoAuthorization()
        serializer = PandaSerializer()

    def obj_create(self, bundle, request=None, **kwargs):
        """
        Create an activity log for the accessing user.
        """
        try:
            bundle = super(ActivityLogResource, self).obj_create(bundle, request=request, user=request.user, **kwargs)
        except IntegrityError:
            raise ImmediateHttpResponse(response=HttpConflict('Activity has already been recorded.'))

        return bundle


#!/usr/bin/env python

from tastypie import fields
from tastypie.authorization import DjangoAuthorization

from panda.api.utils import PandaApiKeyAuthentication, PandaModelResource, PandaSerializer
from panda.models import ActivityLog 

class ActivityLogResource(PandaModelResource):
    """
    API resource for DataUploads.
    """
    from panda.api.users import UserResource

    creator = fields.ForeignKey(UserResource, 'user', full=True)

    class Meta:
        queryset = ActivityLog.objects.all()
        resource_name = 'activity_log'
        allowed_methods = ['get', 'post']

        authentication = PandaApiKeyAuthentication()
        authorization = DjangoAuthorization()
        serializer = PandaSerializer()

    def obj_create(self, bundle, request=None, **kwargs):
        """
        Create an activity log for the accessing user.
        """
        bundle = super(ActivityLogResource, self).obj_create(bundle, request=request, user=request.user, **kwargs)

        return bundle


--- 
+++ 
@@ -2,8 +2,11 @@
 
 from tastypie import fields
 from tastypie.authorization import DjangoAuthorization
+from tastypie.exceptions import ImmediateHttpResponse
+from tastypie.http import HttpConflict
 
 from panda.api.utils import PandaApiKeyAuthentication, PandaModelResource, PandaSerializer
+from django.db import IntegrityError
 from panda.models import ActivityLog 
 
 class ActivityLogResource(PandaModelResource):
@@ -27,7 +30,10 @@
         """
         Create an activity log for the accessing user.
         """
-        bundle = super(ActivityLogResource, self).obj_create(bundle, request=request, user=request.user, **kwargs)
+        try:
+            bundle = super(ActivityLogResource, self).obj_create(bundle, request=request, user=request.user, **kwargs)
+        except IntegrityError:
+            raise ImmediateHttpResponse(response=HttpConflict('Activity has already been recorded.'))
 
         return bundle
 
Return 409 for duplicate activity logging.
