def import_file(self, request, *args, **kwargs): for row in request.data.get('rows'): if row.get('success'): UserService.send_mail(email=row.get('email'), name=row.get('name'), phone=row.get('phone'), send_email=True) return Response({'success': True})
def invite_list_user(self, request, *args, **kwargs): serializer = InviteListSerializer(data=request.data, many=True) serializer.is_valid(raise_exception=True) invite_list_service = InviteListService() valid_user, invalid_user = invite_list_service.separation_data( serializer.validated_data) for user in valid_user: UserService.invite(email=user.get('email'), name=user.get('name')) data = {'valid_user': valid_user, 'invalid_user': invalid_user} return Response(data=data, status=status.HTTP_201_CREATED)
def create(self, request, *args, **kwargs): invite_serializer = InviteSerializer(data=request.data) if invite_serializer.is_valid(raise_exception=True): email = request.data.get('email') name = request.data.get('name') res = UserService.invite(email, name) return Response(res)
def change_password(self, request, *args, **kwargs): user = self.get_object() user = UserService.update_password(request.data, user) if user: self.perform_update(user) return Response({"Success": True}) return Response({"Success": False})
def set_project_manager(cls, team, **kwargs): email = kwargs.get('email') user = UserService.get_user_by_email(email) if user: team.project_manager = user team.save() cls.update_line_manager_in_team(team) cls.update_user_team(user, team.id)
def forgot_password(cls, email): user = UserService.get_user_by_email(email) if user: token = TokenUtil.encode(user) link = f'http://{settings.UI_HOST}/resetPassword?token={token}' content = render_to_string('../templates/password_email.html', {'link': link}) SendMail.start([email], 'Forgot Password', content) return True return False
def verify(self, request, *args, **kwargs): password = request.data.get('password') user_data = TokenUtil.verify(request.data.get('token')) invite_serializer = InviteSerializer(data=user_data) if invite_serializer.is_valid(raise_exception=True): email = user_data.get('email') user = UserService.get_user_by_email(email) if user: LoginService.change_password(user, password) return Response({'success': True}) raise ValidationError("Verify error")
def cmd_set_password(): user_id = input("Enter user ID: ") pwd = input("Enter a new password: "******"No user with id {user_id}") except Exception as e: print(str(e))
def decode(token, token_hours=12): payload = jwt.decode(token, TokenUtil.get_secret_key(), algorithms=['HS256']) iat = int(payload.get("iat")) email = payload.get('email') if iat + token_hours * 60 * 60 < datetime.now().timestamp(): return None from api_user.services import UserService user = UserService.get_user_by_email(email) if user: return user return None
def login(cls, **kwargs): try: email = kwargs.get('email') password = kwargs.get('password') user = UserService.get_user_by_email(email) if not user or not user.check_password(password): raise ValidationError("Invalid username or password") token = TokenUtil.encode(user) res = dict(success=True, token=token, user=user.id, profile_id=user.profile.id, email=user.email, active=user.active, admin=user.is_admin, image=None, pm=user.is_project_manager) if user.profile.image: res.update(image=settings.MEDIA_IMAGE + '/media/' + str(user.profile.image)) return res except Exception as e: raise e
def remove_member(cls, data, instance): user = UserService.get_user_by_email(data.get('email')) if user: cls._remove_user_team(user, instance.id) user.profile.save()
def add_new_member(cls, instance, **kwargs): user = UserService.get_user_by_email(kwargs.get('email')) if user: cls.update_user_team(user, instance.id)
def activate(self, request, *args, **kwargs): user = self.get_object() UserService.activate_user(user) return Response({"Success": True})
def search_non_paginate(self, request, *args, **kwargs): queryset = UserService.get_filter_query(request) data = self.get_serializer(queryset, many=True).data return Response(data)
def search(self, request, *args, **kwargs): queryset = UserService.get_filter_query(request) page = self.paginate_queryset(queryset) data = self.get_serializer(page, many=True).data return self.get_paginated_response(data)
def destroy(self, request, *args, **kwargs): user = self.get_object() UserService.deactivate_user(user) return Response({"Success": True}, status=status.HTTP_204_NO_CONTENT)