Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
from devilry.apps.core.models import Node, Subject, DevilryUserProfile, Period, RelatedStudent
from djangorestframework.views import ModelView, ListModelView, InstanceModelView
from djangorestframework.mixins import InstanceMixin, ReadModelMixin
from djangorestframework.resources import ModelResource
from djangorestframework.permissions import IsAuthenticated
from devilry_subjectadmin.rest.auth import BaseIsAdmin, nodeadmin_required
from django.db.models import Count, Min, Max
class NodeResource( ModelResource ):
model = Node
fields = ( 'id', 'short_name', 'long_name',
'etag', 'predecessor', 'children', 'most_recent_start_time', )
allowed_methods = ('get' ,)
# [->] to RelatedNodeDetails
def most_recent_start_time( self, instance ):
# [/] strftime
# [!] needs a good way of collecting the most recent period
# that on a later stage can be developed into an ordering operator
result = Period.objects.filter( parentnode__parentnode=instance ).\
aggregate( Max( 'start_time' ) )
return result['start_time__max']
# Hierarchy data
from django.db.models import Q
from djangorestframework.views import ListModelView
from djangorestframework.resources import ModelResource
from djangorestframework.permissions import IsAuthenticated
from devilry.apps.core.models import AssignmentGroup
from .helpers import GroupResourceHelpersMixin
class FindGroupsResource(ModelResource, GroupResourceHelpersMixin):
fields = ('id', 'name', 'assignment', 'period', 'subject')
model = AssignmentGroup
def assignment(self, instance):
return self.format_basenode(instance.parentnode)
def period(self, instance):
return self.format_basenode(instance.parentnode.parentnode)
def subject(self, instance):
return self.format_basenode(instance.parentnode.parentnode.parentnode)
class FindGroupsView(ListModelView):
"""
candidate = instance
candidates = Node.where_is_admin_or_superadmin( self.view.request.user )
while candidate in candidates and counter < PATH_MAX_LENGTH:
path.append( candidate )
candidate = candidate.parentnode
counter += 1
path.reverse()
serializer = PathElementResource()
return serializer.serialize_iter( path )
class PathElementResource( ModelResource ):
model = Node
fields = ( 'id', 'short_name', )
class Path( InstanceModelView ):
resource = PathResource
permissions = ( IsAuthenticated, )
allowed_methods = ('get' ,)
"""A read resource for Section."""
model = models.Section
fields = (
"id", "last_post_pid", "bumplimit", "description",
"filesize_limit", "default_name", "anonymity", "threadlimit",
"group_id", "type", "slug", "name"
)
class SectionGroupResource(ModelResource):
"""A read resource for SectionGroup."""
model = models.SectionGroup
fields = ("id", "name", "order", "is_hidden")
class FileResource(ModelResource):
"""A list resource for File."""
model = models.File
fields = ("id", "post", "name", "type", "size", "image_width",
"image_height", "hash", "file", "thumb")
class FileTypeResource(ModelResource):
"""A read resource for FileType."""
model = models.FileType
fields = ("id", "category_id", "type", "extension")
class FileTypeGroupResource(ModelResource):
"""A read resource for FileTypeGroup."""
model = models.FileTypeGroup
nodeadmin_required( user, nodeid )
class RelatedNodeDetails( InstanceModelView ):
resource = NodeDetailsResource
permissions = ( IsAuthenticated, IsNodeAdmin, )
allowed_methods = ('get' ,)
def get_instance_data( self, instance ):
return instance
class PathResource( ModelResource ):
model = Node
fields = ( 'id', 'path', )
def path( self, instance ):
PATH_MAX_LENGTH = 8
path = []
counter = 1
candidate = instance
candidates = Node.where_is_admin_or_superadmin( self.view.request.user )
while candidate in candidates and counter < PATH_MAX_LENGTH:
path.append( candidate )
candidate = candidate.parentnode
counter += 1
class FileResource(ModelResource):
"""A list resource for File."""
model = models.File
fields = ("id", "post", "name", "type", "size", "image_width",
"image_height", "hash", "file", "thumb")
class FileTypeResource(ModelResource):
"""A read resource for FileType."""
model = models.FileType
fields = ("id", "category_id", "type", "extension")
class FileTypeGroupResource(ModelResource):
"""A read resource for FileTypeGroup."""
model = models.FileTypeGroup
from datetime import datetime
from django.db.models import Max, Q, Count
from djangorestframework.views import ListModelView
from djangorestframework.resources import ModelResource
from djangorestframework.permissions import IsAuthenticated
from devilry.apps.core.models import AssignmentGroup
from devilry.apps.core.models import Delivery
from devilry.devilry_rest.serializehelpers import format_datetime
from devilry.devilry_rest.serializehelpers import format_timedelta
from .helpers import GroupResourceHelpersMixin
from .errors import BadRequestError
class OpenGroupsResource(ModelResource, GroupResourceHelpersMixin):
fields = ('id', 'name', 'assignment', 'period', 'subject', 'deliveries',
'active_deadline')
model = AssignmentGroup
def assignment(self, instance):
return self.format_basenode(instance.parentnode)
def period(self, instance):
return self.format_basenode(instance.parentnode.parentnode)
def subject(self, instance):
return self.format_basenode(instance.parentnode.parentnode.parentnode)
def deliveries(self, instance):
return Delivery.objects.filter(deadline__assignment_group=instance,
successful=True).count()
from djangorestframework.resources import ModelResource
from djangorestframework.serializer import Serializer
from django.core.urlresolvers import reverse
from apps.users.resources import UserResource
from models import Password, PasswordContact
class PasswordContactResource(ModelResource):
model = PasswordContact
ordering = ('to_user__first_name',)
fields = ('id', 'url', ('to_user', 'UserResource'), ('from_user', 'UserResource'))
ignore_fields = ('id',)
def validate_request(self, data, files=None):
"""
Backbone.js will submit all fields in the model back to us, but
some fields are set as uneditable in our Django model. So we need
to remove those extra fields before performing validation.
"""
for key in self.ignore_fields:
if key in data:
del data[key]
return super(PasswordContactResource, self).validate_request(data, files)
from blogpost.models import BlogPost, Comment
class BlogPostResource(ModelResource):
"""
A Blog Post has a *title* and *content*, and can be associated with zero or more comments.
"""
model = BlogPost
fields = ('created', 'title', 'slug', 'content', 'url', 'comments')
ordering = ('-created',)
def comments(self, instance):
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)),
)
from djangorestframework.resources import ModelResource
from django.contrib.auth.models import User
class UserResource(ModelResource):
"""
Lets users search for other users by username.
"""
model = User
fields = ('id', 'first_name', 'last_name', 'username', 'url')
def validate_request(self, data, files=None):
"""
Backbone.js will submit all fields in the model back to us, but
some fields are set as uneditable in our Django model. So we need
to remove those extra fields before performing validation.
"""
for key in self.ignore_fields:
if key in data:
del data[key]