def setUp(self): # a user must be logged-in self.client.post( '/api/users/', { 'email': '*****@*****.**', 'phone_number': '9484749612', 'password': '******', 'user_type': 'A' }) response = self.client.post('/user_login/', { 'username': '******', 'password': '******' }) user = User.objects.get() user.is_active = True user.save() account = Account(name='Rowegie Lambojon', user=user) account.save() self.api_client = APIClient() self.api_client.credentials(HTTP_AUTHORIZATION='Token ' + Token.objects.get().key) self.api_client.login(email="*****@*****.**", password='******') self.now = datetime.now()
def post(self, request): data = json.loads(request.body) if Relation.objects.filter(from_account=data['from_account_id'], to_account=data['to_account_id']).exists(): follow = Relation.objects.filter( from_account=data['from_account_id'], to_account=data['to_account_id']) follow.delete() else: Relation.objects.create( from_account=Account(id=data['from_account_id']), to_account=Account(id=data['to_account_id'])) from_followees = Relation.objects.filter( from_account=data['from_account_id']).count() to_followers = Relation.objects.filter( to_account=data['to_account_id']).count() from_account = Account.objects.filter(id=data['from_account_id']).get() from_account.followees = from_followees from_account.save() to_account = Account.objects.filter(id=data['to_account_id']).get() to_account.followers = to_followers to_account.save() return JsonResponse({'MESSAGE': 'SUCCESS'}, status=200)
def post(self, request): data = json.loads(request.body) ByComment.objects.create( comment=Comment(id=data['comment_id']), account=Account(id=data['account_id']), contents=data['contents'], ) return JsonResponse({'MESSAGE': 'SUCCESS'}, status=200)
def post(self, request): data = json.loads(request.body) Post.objects.create( account=Account(id=data['id']), contents=data['contents'], img_url=data['img_url'], ) return JsonResponse({'MESSAGE': 'SUCCESS'}, status=200)
def post(self, request): data = json.loads(request.body) if Likes.objects.filter(account=data['account_id'], post=data['post_id']).exists(): like = Likes.objects.filter(account=data['account_id'], post=data['post_id']) like.delete() else: Likes.objects.create( account=Account(id=data['account_id']), post=Post(id=data['post_id']), ) count = Likes.objects.filter(post=data['post_id']).count() likes_cnt = Post.objects.filter(id=data['post_id']).get() likes_cnt.likes_count = count likes_cnt.save() return JsonResponse({'MESSAGE': 'SUCCESS'}, status=200)
def save(self): user = Account( first_name=self.validated_data["first_name"], last_name=self.validated_data["last_name"], email=self.validated_data["email"], ) password = self.validated_data["password"] password2 = self.validated_data["password2"] # Fix the bug here if password != password2: serializers.ValidationError({"password": "******"}) user.set_password(password) user.save() return user
def create(self, validated_data): account = Account(**validated_data) account.set_password(validated_data['password']) account.save() return account