def post(self, request): email = request.data.get('email') password = request.data.get('password') if email is None or password is None: raise CommonException(detail=gettext(messages.NO_CREDENTIALS), code=codes.NO_CREDENTIALS) try: user = MainUser.objects.get(email=email) if not user.check_password(password): raise CommonException(detail=gettext( messages.WRONG_EMAIL_OR_PASSWORD), code=codes.WRONG_EMAIL_OR_PASSWORD) except MainUser.DoesNotExist: raise CommonException( detail={gettext(messages.EMAIL_DOESNT_EXIST)}, code=codes.EMAIL_DOESNT_EXIST) token = get_token(user) user.last_login = datetime.now() user.save() url = "http://dev.cheesenology.kz:8080/camunda/app/admin/default/#/login" serializer = MainUserSerializer(user) return Response({ 'token': token, 'user': serializer.data }, status=status.HTTP_200_OK)
def login(self, request): serializer = self.get_serializer(data=request.query_params) serializer.is_valid(raise_exception=True) user, token = serializer.login() return Response({ 'user': MainUserSerializer(user).data, 'token': token })
class FavoriteArticleSerializer(serializers.ModelSerializer): article = ArticleSerializer() user = MainUserSerializer() class Meta: model = FavoriteArticle fields = '__all__' read_only_fields = ('user', )
def get(self, request): serializer = LoginSerializer(data=request.query_params) serializer.is_valid(raise_exception=True) token, user = serializer.login() return Response({ 'token': token, 'user': MainUserSerializer(user).data })
def put(self, request): serializer = ChangeDetailsSerializer(data=request.data, context={'request': self.request}) serializer.is_valid(raise_exception=True) serializer.change_details() user = MainUserSerializer(self.request.user) return Response((gettext(USER_DETAILS_CHANGED), user.data), status=status.HTTP_200_OK)
def login(self, request): serializer = LoginSerializer(data=request.data) serializer.is_valid(raise_exception=True) user = serializer.validated_data['user'] token = get_token(user) return Response({ 'user': MainUserSerializer(user).data, 'token': token })
class TaskSerializer(TaskShortSerializer): creator = MainUserSerializer(read_only=True) class Meta(TaskShortSerializer.Meta): fields = TaskShortSerializer.Meta.fields + ('description', 'creator', 'executor', 'priority') def validate_priority(self, value): if value not in PRIORITY: raise serializers.ValidationError('there is no such priority') return value
class TaskSerializer(serializers.ModelSerializer): owner = MainUserSerializer(read_only=True) class Meta: model = Task fields = '__all__' def create(self, validated_data): validated_data['owner'] = self.context['request'].user task = Task.objects.create(**validated_data) task.save() return task
class ProjectSerializer(ProjectShortSerializer): creator = MainUserSerializer(read_only=True) class Meta(ProjectShortSerializer.Meta): fields = ProjectShortSerializer.Meta.fields + ('description', 'creator') def create(self, validated_data): logger.info('start creation of project') project = Project.objects.create(**validated_data) logger.info('end of project creation ') return project
class TaskSerializer(serializers.ModelSerializer): # common task serializer owner = MainUserSerializer(read_only=True) class Meta: model = Task fields = ('id', 'name', 'created', 'due', 'owner', 'completed') def create(self, validated_data): validated_data['owner'] = self.context['request'].user task = Task.objects.create(**validated_data) task.save() return task
class TaskDocumentShortSerializer(serializers.ModelSerializer): creator = MainUserSerializer(read_only=True) # document = serializers.FileField(write_only=True) # document_url = serializers.SerializerMethodField(read_only=True) read_only_fields = ('task', ) class Meta: model = TaskDocument fields = ('id', 'document', 'creator', 'task') def get_document_url(self, obj): if obj.document: return self.context['request'].build_absolute_uri(obj.document.url) return None
def activate(request, uuid): try: activation = Activation.objects.get(uuid=uuid) except Activation.DoesNotExist: raise ValidationError(messages.LINK_INVALID) activation.is_valid(raise_exception=True) serializer = RegistrationSerializer( data={ 'email': activation.email, 'password': activation.password, 'full_name': activation.full_name }) serializer.is_valid(raise_exception=True) user = serializer.complete(activation) return Response({'user': MainUserSerializer(user).data})
def profile(self, request): user = self.get_object() return Response(MainUserSerializer(user).data)
class TaskCommentSerializer(TaskCommentShortSerializer): creator = MainUserSerializer(read_only=True) class Meta(TaskCommentShortSerializer.Meta): fields = TaskCommentShortSerializer.Meta.fields + ('creator', 'created_at')
class CommentGetSerializer(CommentSerializer): task = TaskSerializer() creator = MainUserSerializer() class Meta(CommentSerializer.Meta): fields = CommentSerializer.Meta.fields
def get(self, request): user = self.request.user serializer = MainUserSerializer(user) return Response(serializer.data)
def post(self, request): serializer = RegisterSerializer(data=request.data) serializer.is_valid(raise_exception=True) user = serializer.complete() return Response(MainUserSerializer(user).data)
def signup(self, request): serializer = MainUserSerializer(data=request.data) serializer.is_valid(raise_exception=True) serializer.save() return Response(serializer.data)
def change_password(self, request): serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) user = serializer.change() return Response({'user': MainUserSerializer(user).data})