mirror of
https://github.com/fergalmoran/dss.api.git
synced 2026-02-04 06:45:32 +00:00
Python 3 upgrade finished
This commit is contained in:
@@ -1,48 +1,48 @@
|
||||
import humanize
|
||||
from tastypie.authentication import Authentication
|
||||
from tastypie.authorization import Authorization
|
||||
from tastypie.constants import ALL, ALL_WITH_RELATIONS
|
||||
from spa.api.v1.BaseResource import BaseResource
|
||||
from spa.models import UserProfile
|
||||
from spa.models.activity import Activity
|
||||
|
||||
|
||||
class ActivityResource(BaseResource):
|
||||
class Meta:
|
||||
queryset = Activity.objects.select_subclasses().order_by('-id')
|
||||
resource_name = 'activity'
|
||||
authorization = Authorization()
|
||||
authentication = Authentication()
|
||||
always_return_data = True
|
||||
filtering = {
|
||||
'user': ALL_WITH_RELATIONS
|
||||
}
|
||||
|
||||
def dehydrate(self, bundle):
|
||||
try:
|
||||
if bundle.obj.user is not None:
|
||||
user_name = bundle.obj.user.get_nice_name()
|
||||
user_image = bundle.obj.user.get_small_profile_image()
|
||||
user_profile = bundle.obj.user.get_profile_url()
|
||||
else:
|
||||
user_name = UserProfile.get_default_display_name()
|
||||
user_image = UserProfile.get_default_avatar_image()
|
||||
user_profile = ""
|
||||
|
||||
bundle.data["verb"] = bundle.obj.get_verb_past(),
|
||||
bundle.data["object"] = bundle.obj.get_object_singular(),
|
||||
bundle.data["item_name"] = bundle.obj.get_object_name(),
|
||||
bundle.data["item_url"] = bundle.obj.get_object_url(),
|
||||
bundle.data["user_name"] = user_name,
|
||||
bundle.data["user_profile"] = user_profile,
|
||||
bundle.data["user_image"] = user_image
|
||||
return bundle
|
||||
|
||||
except AttributeError, ae:
|
||||
self.logger.debug("AttributeError: Error dehydrating activity, %s" % ae.message)
|
||||
except TypeError, te:
|
||||
self.logger.debug("TypeError: Error dehydrating activity, %s" % te.message)
|
||||
except Exception, ee:
|
||||
self.logger.debug("Exception: Error dehydrating activity, %s" % ee.message)
|
||||
return None
|
||||
|
||||
import humanize
|
||||
from tastypie.authentication import Authentication
|
||||
from tastypie.authorization import Authorization
|
||||
from tastypie.constants import ALL, ALL_WITH_RELATIONS
|
||||
from spa.api.v1.BaseResource import BaseResource
|
||||
from spa.models import UserProfile
|
||||
from spa.models.activity import Activity
|
||||
|
||||
|
||||
class ActivityResource(BaseResource):
|
||||
class Meta:
|
||||
queryset = Activity.objects.select_subclasses().order_by('-id')
|
||||
resource_name = 'activity'
|
||||
authorization = Authorization()
|
||||
authentication = Authentication()
|
||||
always_return_data = True
|
||||
filtering = {
|
||||
'user': ALL_WITH_RELATIONS
|
||||
}
|
||||
|
||||
def dehydrate(self, bundle):
|
||||
try:
|
||||
if bundle.obj.user is not None:
|
||||
user_name = bundle.obj.user.get_nice_name()
|
||||
user_image = bundle.obj.user.get_small_profile_image()
|
||||
user_profile = bundle.obj.user.get_profile_url()
|
||||
else:
|
||||
user_name = UserProfile.get_default_display_name()
|
||||
user_image = UserProfile.get_default_avatar_image()
|
||||
user_profile = ""
|
||||
|
||||
bundle.data["verb"] = bundle.obj.get_verb_past(),
|
||||
bundle.data["object"] = bundle.obj.get_object_singular(),
|
||||
bundle.data["item_name"] = bundle.obj.get_object_name(),
|
||||
bundle.data["item_url"] = bundle.obj.get_object_url(),
|
||||
bundle.data["user_name"] = user_name,
|
||||
bundle.data["user_profile"] = user_profile,
|
||||
bundle.data["user_image"] = user_image
|
||||
return bundle
|
||||
|
||||
except AttributeError as ae:
|
||||
self.logger.debug("AttributeError: Error dehydrating activity, %s" % ae)
|
||||
except TypeError as te:
|
||||
self.logger.debug("TypeError: Error dehydrating activity, %s" % te)
|
||||
except Exception as ee:
|
||||
self.logger.debug("Exception: Error dehydrating activity, %s" % ee)
|
||||
return None
|
||||
|
||||
|
||||
@@ -1,66 +1,66 @@
|
||||
from tastypie import fields
|
||||
from tastypie.authentication import Authentication
|
||||
from tastypie.authorization import Authorization
|
||||
from tastypie.exceptions import ImmediateHttpResponse
|
||||
from tastypie.http import HttpBadRequest, HttpMethodNotAllowed, HttpUnauthorized, HttpApplicationError, HttpNotImplemented
|
||||
from spa.api.v1.BaseResource import BaseResource
|
||||
from spa.models import Mix, UserProfile
|
||||
from spa.models.activity import ActivityComment
|
||||
from spa.models.comment import Comment
|
||||
|
||||
|
||||
class CommentResource(BaseResource):
|
||||
mix = fields.ToOneField('spa.api.v1.MixResource.MixResource', 'mix')
|
||||
likes = fields.ToManyField('spa.api.v1.UserResource.UserResource',
|
||||
'likes', related_name='favourites',
|
||||
full=False, null=True)
|
||||
|
||||
class Meta:
|
||||
queryset = Comment.objects.all().order_by('-date_created')
|
||||
resource_name = 'comments'
|
||||
filtering = {
|
||||
"mix": ('exact',),
|
||||
}
|
||||
authorization = Authorization()
|
||||
authentication = Authentication()
|
||||
always_return_data = True
|
||||
|
||||
def dehydrate(self, bundle):
|
||||
if bundle.obj.user is not None:
|
||||
bundle.data['avatar_image'] = bundle.obj.user.get_profile().get_avatar_image()
|
||||
bundle.data['user_url'] = bundle.obj.user.get_profile().get_absolute_url()
|
||||
bundle.data['user_name'] = bundle.obj.user.get_profile().get_nice_name()
|
||||
else:
|
||||
bundle.data['avatar_image'] = UserProfile.get_default_avatar_image()
|
||||
bundle.data['user_url'] = "/"
|
||||
bundle.data['user_name'] = "Anonymouse"
|
||||
|
||||
if bundle.request.user.is_authenticated():
|
||||
bundle.data['can_edit'] = bundle.request.user.is_staff or bundle.obj.user_id == bundle.request.user.id
|
||||
else:
|
||||
bundle.data['can_edit'] = False
|
||||
|
||||
return bundle
|
||||
|
||||
def obj_create(self, bundle, **kwargs):
|
||||
bundle.data['user'] = bundle.request.user
|
||||
try:
|
||||
if 'mix_id' in bundle.data:
|
||||
mix = Mix.objects.get_by_id_or_slug(bundle.data['mix_id'])
|
||||
if mix is not None:
|
||||
if bundle.request.user.is_authenticated():
|
||||
ActivityComment(user=bundle.request.user.get_profile(), mix=mix).save()
|
||||
return super(CommentResource, self).obj_create(bundle, user=bundle.request.user or None, mix=mix)
|
||||
else:
|
||||
ActivityComment(mix=mix).save()
|
||||
return super(CommentResource, self).obj_create(bundle, mix=mix)
|
||||
else:
|
||||
return HttpBadRequest("Unable to find mix for supplied mix_id (candidate fields are slug & id).")
|
||||
|
||||
return HttpBadRequest("Missing mix_id field.")
|
||||
except ImmediateHttpResponse, e:
|
||||
self.logger.error("Error creating comment (%s)" % e.message)
|
||||
return HttpUnauthorized("Git tae fuck!")
|
||||
except Exception, e:
|
||||
self.logger.error("Error creating comment (%s)" % e.message)
|
||||
return HttpApplicationError("Unable to hydrate comment from supplied data.")
|
||||
from tastypie import fields
|
||||
from tastypie.authentication import Authentication
|
||||
from tastypie.authorization import Authorization
|
||||
from tastypie.exceptions import ImmediateHttpResponse
|
||||
from tastypie.http import HttpBadRequest, HttpMethodNotAllowed, HttpUnauthorized, HttpApplicationError, HttpNotImplemented
|
||||
from spa.api.v1.BaseResource import BaseResource
|
||||
from spa.models import Mix, UserProfile
|
||||
from spa.models.activity import ActivityComment
|
||||
from spa.models.comment import Comment
|
||||
|
||||
|
||||
class CommentResource(BaseResource):
|
||||
mix = fields.ToOneField('spa.api.v1.MixResource.MixResource', 'mix')
|
||||
likes = fields.ToManyField('spa.api.v1.UserResource.UserResource',
|
||||
'likes', related_name='favourites',
|
||||
full=False, null=True)
|
||||
|
||||
class Meta:
|
||||
queryset = Comment.objects.all().order_by('-date_created')
|
||||
resource_name = 'comments'
|
||||
filtering = {
|
||||
"mix": ('exact',),
|
||||
}
|
||||
authorization = Authorization()
|
||||
authentication = Authentication()
|
||||
always_return_data = True
|
||||
|
||||
def dehydrate(self, bundle):
|
||||
if bundle.obj.user is not None:
|
||||
bundle.data['avatar_image'] = bundle.obj.user.get_profile().get_avatar_image()
|
||||
bundle.data['user_url'] = bundle.obj.user.get_profile().get_absolute_url()
|
||||
bundle.data['user_name'] = bundle.obj.user.get_profile().get_nice_name()
|
||||
else:
|
||||
bundle.data['avatar_image'] = UserProfile.get_default_avatar_image()
|
||||
bundle.data['user_url'] = "/"
|
||||
bundle.data['user_name'] = "Anonymouse"
|
||||
|
||||
if bundle.request.user.is_authenticated():
|
||||
bundle.data['can_edit'] = bundle.request.user.is_staff or bundle.obj.user_id == bundle.request.user.id
|
||||
else:
|
||||
bundle.data['can_edit'] = False
|
||||
|
||||
return bundle
|
||||
|
||||
def obj_create(self, bundle, **kwargs):
|
||||
bundle.data['user'] = bundle.request.user
|
||||
try:
|
||||
if 'mix_id' in bundle.data:
|
||||
mix = Mix.objects.get_by_id_or_slug(bundle.data['mix_id'])
|
||||
if mix is not None:
|
||||
if bundle.request.user.is_authenticated():
|
||||
ActivityComment(user=bundle.request.user.get_profile(), mix=mix).save()
|
||||
return super(CommentResource, self).obj_create(bundle, user=bundle.request.user or None, mix=mix)
|
||||
else:
|
||||
ActivityComment(mix=mix).save()
|
||||
return super(CommentResource, self).obj_create(bundle, mix=mix)
|
||||
else:
|
||||
return HttpBadRequest("Unable to find mix for supplied mix_id (candidate fields are slug & id).")
|
||||
|
||||
return HttpBadRequest("Missing mix_id field.")
|
||||
except ImmediateHttpResponse as e:
|
||||
self.logger.error("Error creating comment (%s)" % e)
|
||||
return HttpUnauthorized("Git tae fuck!")
|
||||
except Exception as e:
|
||||
self.logger.error("Error creating comment (%s)" % e)
|
||||
return HttpApplicationError("Unable to hydrate comment from supplied data.")
|
||||
|
||||
@@ -78,5 +78,5 @@ class PlaylistResource(BaseResource):
|
||||
result.obj.save()
|
||||
|
||||
return result
|
||||
except Exception, ex:
|
||||
print ex
|
||||
except Exception as ex:
|
||||
print(ex)
|
||||
@@ -1,34 +1,34 @@
|
||||
from tastypie import fields
|
||||
from tastypie.authorization import Authorization
|
||||
from tastypie.exceptions import ImmediateHttpResponse
|
||||
from tastypie.http import HttpBadRequest
|
||||
|
||||
from spa.api.v1.BaseResource import BaseResource
|
||||
|
||||
from spa.models import Show
|
||||
from spa.models.show import ShowOverlapException
|
||||
|
||||
DATE_FORMAT = '%d/%m/%Y %H:%M:%S'
|
||||
|
||||
|
||||
class ShowResource(BaseResource):
|
||||
mix = fields.ToOneField('spa.api.v1.MixResource.MixResource',
|
||||
'mix', null=False, full=False)
|
||||
|
||||
class Meta:
|
||||
queryset = Show.objects.all()
|
||||
authorization = Authorization()
|
||||
resource_name = 'shows'
|
||||
|
||||
def obj_create(self, bundle, **kwargs):
|
||||
try:
|
||||
return super(ShowResource, self).obj_create(bundle, **kwargs)
|
||||
except ShowOverlapException:
|
||||
raise ImmediateHttpResponse(
|
||||
HttpBadRequest("This event overlaps with an existing event")
|
||||
)
|
||||
except Exception, ex:
|
||||
raise ImmediateHttpResponse(
|
||||
HttpBadRequest(ex.message)
|
||||
)
|
||||
|
||||
from tastypie import fields
|
||||
from tastypie.authorization import Authorization
|
||||
from tastypie.exceptions import ImmediateHttpResponse
|
||||
from tastypie.http import HttpBadRequest
|
||||
|
||||
from spa.api.v1.BaseResource import BaseResource
|
||||
|
||||
from spa.models import Show
|
||||
from spa.models.show import ShowOverlapException
|
||||
|
||||
DATE_FORMAT = '%d/%m/%Y %H:%M:%S'
|
||||
|
||||
|
||||
class ShowResource(BaseResource):
|
||||
mix = fields.ToOneField('spa.api.v1.MixResource.MixResource',
|
||||
'mix', null=False, full=False)
|
||||
|
||||
class Meta:
|
||||
queryset = Show.objects.all()
|
||||
authorization = Authorization()
|
||||
resource_name = 'shows'
|
||||
|
||||
def obj_create(self, bundle, **kwargs):
|
||||
try:
|
||||
return super(ShowResource, self).obj_create(bundle, **kwargs)
|
||||
except ShowOverlapException:
|
||||
raise ImmediateHttpResponse(
|
||||
HttpBadRequest("This event overlaps with an existing event")
|
||||
)
|
||||
except Exception as ex:
|
||||
raise ImmediateHttpResponse(
|
||||
HttpBadRequest(ex)
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user