Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"""
3. def for post Diem danh to server:
post:
"classId": "xxx"
"date":"20-04-2012"
"list":"studentId-status-20-04-2012%studentId-status-20-04-2012%studentId-status-20-04-2012"
status la trang thai di hoc: K-Nghi ko phep; P- Nghi co phep; M-Di muon;
"""
class_id = request.POST['classId']
day = request.POST['date']
data = request.POST['list']
try:
_class = Class.objects.get(id = int(class_id))
except ObjectDoesNotExist:
return Response(status.HTTP_404_NOT_FOUND)
if (is_teacher(request)
and request.user.teacher in _class.associated_teacher()):
date_parts = day.split('-')
try:
response = dd(request,
class_id,
date_parts[0],
date_parts[1],
date_parts[2],
api_called=True,
data=data)
if response.status_code == 200:
return Response(status.HTTP_200_OK)
else:
return Response(status.HTTP_400_BAD_REQUEST)
except Exception as e:
def delete(self, request, *args, **kwargs):
model = self.resource.model
try:
if args:
# If we have any none kwargs then assume the last represents the primrary key
instance = model.objects.get(pk=args[-1], **kwargs)
else:
# Otherwise assume the kwargs uniquely identify the model
instance = model.objects.get(**kwargs)
except model.DoesNotExist:
raise ErrorResponse(status.HTTP_404_NOT_FOUND, None, {})
instance.delete()
return
def delete(self, request, *args, **kwargs):
try:
if args:
# If we have any none kwargs then assume the last represents the primrary key
instance = self.model.objects.get(pk=args[-1], **kwargs)
else:
# Otherwise assume the kwargs uniquely identify the model
instance = self.model.objects.get(**kwargs)
except self.model.DoesNotExist:
raise ErrorResponse(status.HTTP_404_NOT_FOUND, None, {})
instance.delete()
return
def handle_exception(self, exc):
"""
Handle any exception that occurs, by returning an appropriate response,
or re-raising the error.
"""
if isinstance(exc, exceptions.Throttled):
self.headers['X-Throttle-Wait-Seconds'] = '%d' % exc.wait
if isinstance(exc, exceptions.APIException):
return Response({'detail': exc.detail}, status=exc.status_code)
elif isinstance(exc, Http404):
return Response({'detail': 'Not found'},
status=status.HTTP_404_NOT_FOUND)
elif isinstance(exc, PermissionDenied):
return Response({'detail': 'Permission denied'},
status=status.HTTP_403_FORBIDDEN)
raise
@need_login
@school_function
def get(self, request, class_id):
try:
cl = Class.objects.get(id=class_id)
except ObjectDoesNotExist:
return Response(status=status.HTTP_404_NOT_FOUND)
subjects = cl.subject_set.all()
result = []
for s in subjects:
result.append({
'name': s.name,
'type': s.type,
'hs': s.hs,
'nx': s.nx,
'primary': s.primary,
'index': s.index,
'number_lession': s.number_lession,
'teacher': s.teacher_id.full_name()})
return Response(status=status.HTTP_200_OK, content=result)
def _get_instance_or_404(self, *args, **kwargs):
from djangorestframework.response import ErrorResponse
from djangorestframework import status
model = self.resource.model
query_kwargs = self.get_query_kwargs(self.request, *args, **kwargs)
try:
return self.get_instance(**query_kwargs)
except model.DoesNotExist:
raise ErrorResponse(status.HTTP_404_NOT_FOUND, None, {})
def get(self, request, *args, **kwargs):
try:
if args:
# If we have any none kwargs then assume the last represents the primrary key
instance = self.model.objects.get(pk=args[-1], **kwargs)
else:
# Otherwise assume the kwargs uniquely identify the model
instance = self.model.objects.get(**kwargs)
except self.model.DoesNotExist:
raise ErrorResponse(status.HTTP_404_NOT_FOUND)
return instance
def delete(self, request, key):
"""
Delete a stored object, by removing it's pickled file.
"""
filename = get_filename(key)
if not os.path.exists(filename):
return Response(status.HTTP_404_NOT_FOUND)
os.remove(filename)
def get(self, request, num):
"""
Handle GET requests.
Returns a simple string indicating which view the GET request was for.
"""
if int(num) > 2:
return Response(status.HTTP_404_NOT_FOUND)
return "GET request to AnotherExampleResource %s" % num
def delete(self, request, *args, **kwargs):
"""
Only the creator should be able to delete an instance.
"""
model = self.resource.model
query_kwargs = self.get_query_kwargs(request, *args, **kwargs)
try:
instance = self.get_instance(**query_kwargs)
except model.DoesNotExist:
raise ErrorResponse(status.HTTP_404_NOT_FOUND, None, {})
if instance.created_by == self.user:
instance.delete()
else:
raise ErrorResponse(status.HTTP_401_UNAUTHORIZED, None, {})