def register_user(request): form = RegisterForm(request.data) if not form.is_valid(): return Response({'message': form.error_message()}, status=status.HTTP_406_NOT_ACCEPTABLE) # Create the account. user = form.save() # Create an account for the user. Account.objects.create(user=user) # Login user after registration. user = authenticate( username=form.cleaned_data['username'], password=form.cleaned_data['password1'], ) # Login user. login(request, user) return Response( {'account': serializer.single(user.account, detailed=True)}, status=status.HTTP_201_CREATED)
def login_user(request): form = AuthenticationForm(data=request.data) if form.is_valid(): user = form.get_user() if not Account.objects.filter(user=user).exists(): return Response({ 'message': 'The user does not have an associated Account object. Contact your system administrator.' }, status=status.HTTP_406_NOT_ACCEPTABLE ) # Perform the actual login. login(request, user) return Response({ 'account': serializer.single(user.account, detailed=True) }, status=status.HTTP_202_ACCEPTED ) else: return Response({ 'message': 'Username or password is incorrect.' }, status=status.HTTP_406_NOT_ACCEPTABLE )
def delete(request, pk): try: comment = Comment.objects.get( id=pk, project=request.user.account.active_project) except Comment.DoesNotExist: log.exception() return Response({'message': 'That comment does not exist'}, status=status.HTTP_406_NOT_ACCEPTABLE) if not hold_permission(request.user, 'delete_all_comment'): if (not hold_permission(request.user, 'delete_comment') or comment.user.id != request.user.id): return Response({'message': 'You can only delete your comments.'}, status=status.HTTP_403_FORBIDDEN) # If a comment has children, keep comment but label # it as deleted in order to retain children. if comment.children: comment.message = 'Deleted' comment.is_deleted = True comment.save() serialized_comment = serializer.single(comment) send_project_wide(request.user.account.active_project, { 'type': 'COMMENT-DELETE', 'comment': serialized_comment, }) return Response(data=serialized_comment, status=status.HTTP_202_ACCEPTED) # Fully delete comments that do not have children. else: serialized_comment = serializer.single(comment) send_project_wide(request.user.account.active_project, { 'type': 'COMMENT-DELETE', 'comment': serialized_comment, }) comment.delete() return Response({'message': 'comment deleted.'}, status=status.HTTP_202_ACCEPTED)
def patch(self, request, pk): try: user = User.objects.get(id=pk) photo = request.data['photo'] file_name = '%s_photo.jpg' % user.id if photo: try: # Photo is expected in a javascript, base64 encoded format. # Javascript encodes in the following format: # data:[<MIME-type>][;charset=<encoding>][;base64],<data> file_info, blob = photo.split(',') # Delete previous photo if user has one. if user.account.photo: user.account.photo.delete() user.account.photo.save( file_name, ContentFile(base64.b64decode(blob)) ) user.account.save() except ValueError: log.error('Could not edit user.') log.error('Photo received is in an incorrect format.') log.exception() first_name = request.data.get('first_name', None) if first_name is not None: user.first_name = first_name last_name = request.data.get('last_name', None) if last_name is not None: user.last_name = last_name email = request.data.get('email', None) if email is not None: user.email = email user.save() except KeyError: log.error('Edit user failed: KeyError.') log.error('Arguments received:', request.data) log.exception() return Response({ 'message': 'Not all fields received.' }, status=status.HTTP_406_NOT_ACCEPTABLE ) return Response(serializer.single(user.account), status=status.HTTP_202_ACCEPTED)
def get(request, pk): try: comment = Comment.objects.get( id=pk, project=request.user.account.active_project) except Comment.DoesNotExist: log.exception() return Response({'message': 'Could not find that comment.'}, status=status.HTTP_404_NOT_FOUND) return Response( {'comment': serializer.single(comment)}, status=status.HTTP_200_OK, )
def post(request): error_message = validate_comment(request.data) if error_message: log.error('Validation error:', error_message) return Response( {'message': error_message}, status=status.HTTP_406_NOT_ACCEPTABLE, ) # Comment was validated succesfully. # Create comment object. comment = Comment.objects.create( account=request.user.account, project=request.user.account.active_project, message=request.data['message']) # A comment with a parent id signafies that it is # a reply. This is an optional argument. parent_id = request.data.get('parentId', None) if parent_id: comment.parent_id = parent_id comment.save() # Assigns content object as a subject. comment = assign_subject(request, comment) send_project_wide(request.user.account.active_project, { 'type': 'COMMENT-LOAD', 'comment': serializer.single(comment) }) return Response( {'comment': serializer.single(comment)}, status=status.HTTP_201_CREATED, )
def post(request): print 'Post timesheet:', request.data date = request.data.get('date', None) if not date: return Response({ 'message': 'Where is the date?' }, status=status.HTTP_406_NOT_ACCEPTABLE ) timesheet = Timesheet.objects.get_or_create( account=request.user.account, date=date, )[0] return Response({ 'timesheet': serializer.single(timesheet) }, status=status.HTTP_200_OK )
def patch(request, pk): try: comment = Comment.objects.get( id=pk, project=request.user.account.active_project) except Comment.DoesNotExist: log.exception() return Response({'message': 'Could not find that comment.'}, status=status.HTTP_404_NOT_FOUND) if not hold_permission(request.user, 'change_all_comment'): if not comment.account == request.user.account: return Response( {'message': 'You can only change your own comments.'}, status=status.HTTP_403_FORBIDDEN) message = request.data.get('message', None) if not message: log.error('Recieved a blank message field') return Response( {'message': 'Received blank message field.'}, status=status.HTTP_406_NOT_ACCEPTABLE, ) # All ok, save message edit. comment.message = message comment.is_edited = True comment.save() serialized_comment = serializer.single(comment) redux_action = {'type': 'COMMENT-LOAD', 'comment': serialized_comment} # TODO: Rewrite as element action. send_project_wide(request.user.account.active_project, redux_action) return Response({'comment': serialized_comment}, status=status.HTTP_202_ACCEPTED)