Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_fifo_behaviour(self):
'''It's fine that the Blogposts are capped off at MAX_POSTS. But we want to make sure we see FIFO behaviour.'''
for post in range(15):
form_data = {'title': '%s' % post, 'content': 'This is the content of post #%s' % post}
request = self.factory.post('/blog-post', data=form_data)
view = ListOrCreateModelView.as_view(resource=urls.BlogPostResource)
view(request)
request = self.factory.get('/blog-post')
view = ListOrCreateModelView.as_view(resource=urls.BlogPostResource)
response = view(request)
response_posts = json.loads(response.content)
response_titles = [d['title'] for d in response_posts]
response_titles.reverse()
self.assertEquals(response_titles, ['%s' % i for i in range(models.MAX_POSTS - 5, models.MAX_POSTS + 5)])
def initial(self, request, *args, **kwargs):
"""
Set the currently authenticated user on the resource
"""
CurrentUserSingleton.set_user(request.user)
return super(ModelMixin, self).initial(request, *args, **kwargs)
def final(self, request, response, *args, **kargs):
"""
Clear the current user singleton to make sure it doesn't leak
"""
CurrentUserSingleton.set_user(None)
return super(ModelMixin, self).final(request, response, *args, **kargs)
class PasswordListView(RestrictPasswordToUserMixin, ListOrCreateModelView):
"""
List view for Password objects.
"""
resource = PasswordResource
permissions = (IsAuthenticated, )
class PasswordInstanceView(RestrictPasswordToUserMixin, InstanceModelView):
"""
View for individual Password instances
"""
resource = PasswordResource
permissions = (IsAuthenticated, )
def put(self, request, *args, **kwargs):
"""
from .resources import *
from .views import *
urlpatterns = patterns("api1.views", (r"^$", "api"),
(r"^setting/$", SettingRootView.as_view()),
(r"^setting/(?P[\w\d]+)$", SettingView.as_view()),
(r"^feed/$", FeedRootView.as_view()),
(r"^feed/(?P[\d]+)$", FeedView.as_view()),
(r"^hidden/$", HideRootView.as_view()),
(r"^hidden/(?P[\d]+)?$", HideView.as_view()),
(r"^thread/$",
ListOrCreateModelView.as_view(resource=ThreadResource)),
(r"^thread/(?P\w+)/$",
ListOrCreateModelView.as_view(resource=ThreadResource)),
(r"^thread/(?P\d+)$",
ThreadInstanceView.as_view(resource=ThreadResource)),
(r"^thread/(?P\w+)/(?P\d+)$",
ThreadInstanceView.as_view(resource=ThreadResource)),
(r"^post/$",
PostListOrCreateView.as_view(resource=PostResource)),
(r"^post/(?P\w+)/$",
PostListOrCreateView.as_view(resource=PostResource)),
(r"^post/(?P\w+)/first/$",
PostListOrCreateView.as_view(resource=PostResource),
{"is_op_post": True}),
(r"^post/(?P\d+)$",
PostInstanceView.as_view(resource=PostResource)),
'candidates': {'help': candidates_docs,
'meta': 'list'},
'examiners': {'help': examiners_docs,
'meta': 'list'},
'tags': {'help': tags_docs,
'meta': 'list'},
'deadlines': {'help': deadlines_docs,
'meta': 'list'},
}
responsetable = self.html_create_attrtable(response)
parameterstable = self.htmlformat_parameters_from_form()
return docs.format(responsetable=responsetable,
parameterstable=parameterstable)
class ListOrCreateGroupRest(SelfdocumentingGroupApiMixin, ListOrCreateModelView):
resource = GroupResource
form = GroupForm
permissions = (IsAuthenticated, IsAssignmentAdminAssignmentIdKwarg)
def get_queryset(self):
assignment_id = self.kwargs['assignment_id']
qry = self.resource.model.objects.filter(parentnode=assignment_id)
qry = qry.select_related('feedback')
qry = qry.annotate(num_deliveries=Count('deadlines__deliveries'))
qry = qry.prefetch_related(
'deadlines',
'tags',
'examiners', 'examiners__user',
'examiners__user__devilryuserprofile',
'candidates', 'candidates__student',
'candidates__student__devilryuserprofile')
'candidates': {'help': candidates_docs,
'meta': 'list'},
'examiners': {'help': examiners_docs,
'meta': 'list'},
'tags': {'help': tags_docs,
'meta': 'list'},
'deadlines': {'help': deadlines_docs,
'meta': 'list'},
}
responsetable = self.html_create_attrtable(response)
parameterstable = self.htmlformat_parameters_from_form()
return docs.format(responsetable=responsetable,
parameterstable=parameterstable)
class ListOrCreateGroupRest(SelfdocumentingGroupApiMixin, ListOrCreateModelView):
resource = GroupResource
form = GroupForm
permissions = (IsAuthenticated, IsAssignmentAdminAssignmentIdKwarg)
def get_queryset(self):
assignment_id = self.kwargs['assignment_id']
qry = self.resource.model.objects.filter(parentnode=assignment_id)
qry = qry.select_related('feedback')
qry = qry.annotate(num_deliveries=Count('deadlines__deliveries'))
qry = qry.prefetch_related(
'deadlines',
'tags',
'examiners', 'examiners__user',
'examiners__user__devilryuserprofile',
'candidates', 'candidates__student',
'candidates__student__devilryuserprofile')
from djangorestframework import views
from . import resources
class PlaceCollectionView (views.ListOrCreateModelView):
# TODO: Decide whether pagination is appropriate/necessary.
resource = resources.PlaceResource
from django.http import QueryDict
DEFAULT_ERROR_RESPONSE = ErrorResponse(status.HTTP_404_NOT_FOUND, {'total':'0', 'data':[], 'success':'false'})
class ExtJsInstanceModelView(InstanceModelView):
permissions = (permissions.IsAdminUser,)
def get(self, request, *args, **kwargs):
try:
return super(self.__class__, self).get(request, *args, **kwargs)
except:
raise DEFAULT_ERROR_RESPONSE
class ExtJsCreateModelView(ListOrCreateModelView):
permissions = (permissions.IsAdminUser,)
def initial(self, request, *args, **kwargs):
params_str = request.POST.values()[0]
formatted_str = params_str.replace("'","").replace(": ","=").replace(", ","&").replace("{","").replace("}","")
formatted_str = formatted_str + ('&user=%s' % request.user.id)
request.POST = QueryDict(formatted_str)
self.request = request
def post(self, request, *args, **kwargs):
try:
return super(self.__class__, self).post(self.request, *args, **kwargs)
except:
raise DEFAULT_ERROR_RESPONSE
return reverse('comments', kwargs={'blogpost': instance.key})
class CommentResource(ModelResource):
"""
A Comment is associated with a given Blog Post and has a *username* and *comment*, and optionally a *rating*.
"""
model = Comment
fields = ('username', 'comment', 'created', 'rating', 'url', 'blogpost')
ordering = ('-created',)
urlpatterns = patterns('',
url(r'^$', ListOrCreateModelView.as_view(resource=BlogPostResource), name='blog-posts-root'),
url(r'^(?P[^/]+)/$', InstanceModelView.as_view(resource=BlogPostResource)),
url(r'^(?P[^/]+)/comments/$', ListOrCreateModelView.as_view(resource=CommentResource), name='comments'),
url(r'^(?P[^/]+)/comments/(?P[^/]+)/$', InstanceModelView.as_view(resource=CommentResource)),
)