Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
from .relateduser import RelatedExaminerResource
class IsAssignmentAdminAssignmentIdKwarg(IsAssignmentAdmin):
ID_KWARG = 'assignment_id'
class ListRelatedUsersOnAssignmentMixin(ListRelatedUsersRestMixin):
def get_period_id(self):
assignment_id = self.kwargs['assignment_id']
assignment = Assignment.objects.get(id=assignment_id)
return assignment.parentnode_id
class ListRelatedStudentsOnAssignmentRest(ListRelatedUsersOnAssignmentMixin, ListModelView):
"""
Read-only listing of related students on the period containing the given assignment
(The ID of the assignment is the last segment of the URL). Requires admin
permissions on the assignment.
"""
resource = RelatedStudentResource
permissions = (IsAuthenticated, IsAssignmentAdminAssignmentIdKwarg)
class ListRelatedExaminersOnAssignmentRest(ListRelatedUsersOnAssignmentMixin, ListModelView):
"""
Read-only listing of related examiners on the period containing the given assignment
(The ID of the assignment is the last segment of the URL). Requires admin
permissions on the assignment.
"""
resource = RelatedExaminerResource
if not user_is_admin_or_superadmin(user):
raise ErrorResponse(status.HTTP_403_FORBIDDEN,
{'detail': 'Only administrators have permission to query the user database.'})
class UserResource(ModelResource):
fields = ('id', 'username', 'email', 'full_name', 'languagecode')
model = User
def full_name(self, instance):
return instance.devilryuserprofile.full_name
def languagecode(self, instance):
return instance.devilryuserprofile.languagecode
class SearchForUsers(ListModelView):
"""
Provides an API suited for autocompleting users.
# GET
Search for users by:
- Full name
- Username
- Email
Uses case-ignore-contains search.
## Parameters
The search is specified in the ``query`` parameter in the querystring.
## Response
def last_feedback(self, instance):
last_feedback = instance.feedbacks.only('id', 'save_timestamp', 'grade', 'is_passing_grade')[0]
return {'id': last_feedback.id,
'save_timestamp': format_datetime(last_feedback.save_timestamp),
'grade': last_feedback.grade,
'is_passing_grade': last_feedback.is_passing_grade,
'save_offset_from_now': format_timedelta(datetime.now() - last_feedback.save_timestamp)}
def group(self, instance):
group = instance.deadline.assignment_group
return {'id': group.id,
'name': group.name}
class RecentFeedbacksView(ListModelView):
"""
Lists the 6 most recent feedbacks for the authenticated user.
# GET
List of objects with the following attributes:
- ``id`` (int): Internal Devilry ID of the delivery. Is never ``null``.
- ``assignment`` (object): Information about the assignment.
- ``period`` (object): Information about the period.
- ``subject`` (object): Information about the subject.
- ``number`` (int): Delivery number.
- ``last_feedback`` (object): Information about the last feedback on the delivery.
"""
permissions = (IsAuthenticated,)
resource = RecentFeedbacksResource
return self.format_basenode(instance.deadline.assignment_group.parentnode.parentnode)
def subject(self, instance):
return self.format_basenode(instance.deadline.assignment_group.parentnode.parentnode.parentnode)
def time_of_delivery(self, instance):
return {'offset_from_now': format_timedelta(datetime.now() - instance.time_of_delivery),
'datetime': format_datetime(instance.time_of_delivery)}
def group(self, instance):
group = instance.deadline.assignment_group
return {'id': group.id,
'name': group.name}
class RecentDeliveriesView(ListModelView):
"""
Lists the 6 most recent deliveries made by the authenticated user ordered
by ``time_of_delivery``.
# GET
List of objects with the following attributes:
- ``id`` (int): Internal Devilry ID of the delivery. Is never ``null``.
- ``group`` (object): Information about the group.
- ``number`` (int): Delivery number.
- ``assignment`` (object): Information about the assignment.
- ``period`` (object): Information about the period.
- ``subject`` (object): Information about the subject.
- ``time_of_delivery`` (object): The date and time when the delivery was made, including the offset from _now_.
"""
permissions = (IsAuthenticated,)
return self.format_basenode(instance.deadline.assignment_group.parentnode.parentnode)
def subject(self, instance):
return self.format_basenode(instance.deadline.assignment_group.parentnode.parentnode.parentnode)
def time_of_delivery(self, instance):
return {'offset_from_now': format_timedelta(datetime.now() - instance.time_of_delivery),
'datetime': format_datetime(instance.time_of_delivery)}
def group(self, instance):
group = instance.deadline.assignment_group
return {'id': group.id,
'name': group.name}
class RecentDeliveriesView(ListModelView):
"""
Lists the 6 most recent deliveries made by the authenticated user ordered
by ``time_of_delivery``.
# GET
List of objects with the following attributes:
- ``id`` (int): Internal Devilry ID of the delivery. Is never ``null``.
- ``group`` (object): Information about the group.
- ``number`` (int): Delivery number.
- ``assignment`` (object): Information about the assignment.
- ``period`` (object): Information about the period.
- ``subject`` (object): Information about the subject.
- ``time_of_delivery`` (object): The date and time when the delivery was made, including the offset from _now_.
"""
permissions = (IsAuthenticated,)
from django.conf.urls.defaults import patterns, url
from core.views import ExtJsInstanceModelView, ExtJsCreateModelView, ExtJsUpdateModelView
from djangorestframework.views import ListModelView
from core.resources import AccountResource
urlpatterns = patterns('',
url(r'^list/(?P[^/]+)/$', ListModelView.as_view(resource=AccountResource), name='accounts-list'),
url(r'^read/(?P[^/]+)/$', ExtJsInstanceModelView.as_view(resource=AccountResource), name='accounts-read'),
url(r'^create/$', ExtJsCreateModelView.as_view(resource=AccountResource), name='accounts-create'),
url(r'^update/(?P[^/]+)/$', ExtJsUpdateModelView.as_view(resource=AccountResource), name='accounts-update'),
)
assignment_id = self.kwargs['assignment_id']
assignment = Assignment.objects.get(id=assignment_id)
return assignment.parentnode_id
class ListRelatedStudentsOnAssignmentRest(ListRelatedUsersOnAssignmentMixin, ListModelView):
"""
Read-only listing of related students on the period containing the given assignment
(The ID of the assignment is the last segment of the URL). Requires admin
permissions on the assignment.
"""
resource = RelatedStudentResource
permissions = (IsAuthenticated, IsAssignmentAdminAssignmentIdKwarg)
class ListRelatedExaminersOnAssignmentRest(ListRelatedUsersOnAssignmentMixin, ListModelView):
"""
Read-only listing of related examiners on the period containing the given assignment
(The ID of the assignment is the last segment of the URL). Requires admin
permissions on the assignment.
"""
resource = RelatedExaminerResource
permissions = (IsAuthenticated, IsAssignmentAdminAssignmentIdKwarg)
def deliveries(self, instance):
return Delivery.objects.filter(deadline__assignment_group=instance,
successful=True).count()
def active_deadline(self, instance):
deadline = instance.get_active_deadline()
timedelta = datetime.now() - deadline.deadline
return {'id': deadline.id,
'deadline': format_datetime(deadline.deadline),
'text': deadline.text,
'deadline_expired': deadline.deadline < datetime.now(),
'offset_from_deadline': format_timedelta(timedelta)}
class OpenGroupsView(ListModelView):
"""
Provides an API with information about all the open groups of the
authenticated user in an active period.
# GET
## Parameters
You can get only groups that is within a deadline (the deadline has not
expired), or only groups where the deadline has expired.
To use this feature, specify ``only=deadline_expired`` or
``only=deadline_not_expired`` in the querystring.
## Response
List of objects with the following attributes:
- ``id`` (int): Internal Devilry ID of the group. Is never ``null``.
def last_feedback(self, instance):
last_feedback = instance.feedbacks.only('id', 'save_timestamp', 'grade', 'is_passing_grade')[0]
return {'id': last_feedback.id,
'save_timestamp': format_datetime(last_feedback.save_timestamp),
'grade': last_feedback.grade,
'is_passing_grade': last_feedback.is_passing_grade,
'save_offset_from_now': format_timedelta(datetime.now() - last_feedback.save_timestamp)}
def group(self, instance):
group = instance.deadline.assignment_group
return {'id': group.id,
'name': group.name}
class RecentFeedbacksView(ListModelView):
"""
Lists the 6 most recent feedbacks for the authenticated user.
# GET
List of objects with the following attributes:
- ``id`` (int): Internal Devilry ID of the delivery. Is never ``null``.
- ``assignment`` (object): Information about the assignment.
- ``period`` (object): Information about the period.
- ``subject`` (object): Information about the subject.
- ``number`` (int): Delivery number.
- ``last_feedback`` (object): Information about the last feedback on the delivery.
"""
permissions = (IsAuthenticated,)
resource = RecentFeedbacksResource
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):
"""
Search for groups by the authenticated user.
# GET
## Parameters
Use the ``query`` parameter in the querystring to search for groups by:
- Group name
- Assignment (long and short name)
- Period (long and short name)
- Subject (long and short name)
Uses case-ignore-contains search.