def register(request): if request.method == 'POST': form = RegisterForm(request.POST) if form.is_valid() and not request.user.is_authenticated(): data = form.cleaned_data new_user = User.objects.create_user(username=data['nickname'], password=data['password']) new = CustomUser(user=new_user, sex=data.get('sex'), phone=data.get('phone')) new.save() login( request, authenticate(username=new_user.username, password=data.get('password2'))) _return = {'message': '感谢您的注册'} _return.update(get_header(request)) return render(request, 'redirect.html', _return) else: return render(request, 'register.html', {'form': form}) elif request.method == 'GET': if request.user.is_authenticated(): return render(request, 'redirect.html', {'message': '您已登陆'}) form = RegisterForm() _return = {'form': form} _return.update(get_header(request)) return render(request, 'register.html', _return)
class TestManageNotification(TestCase): def setUp(self): self.user = CustomUser(username="******", password="******") self.user.save() def test_create_notification(self): ManageNotification().create_notification(self.user, "testuser", 1) notif_is_exist = Notification.objects.all().count() self.assertEquals(notif_is_exist, 1)
class TestCommentsEndpoint(APITestCase): def setUp(self): self.user = CustomUser(username="******", password="******") self.user.save() self.category = Categories(name_category="TestCategory") self.category.save() self.article = Article( title="TestTitle", content_article="Test Content", id_category=self.category, id_user=self.user, ).save() self.get_all_url = reverse("getAll", args=[Article.objects.all()[0].id, 1]) self.create_like_comment_url = reverse("create_like_comment", args=[0, 'testuser']) self.create_comment_url = reverse("Create_comment", args=[0, 'testuser']) self.data_like_comment = {"recation_comment": 1} self.data_comment = {"content_comment": "test comment"} def test_get_all_comment(self): response = self.client.get(self.get_all_url) self.assertEquals(response.status_code, status.HTTP_200_OK) def test_post_like_comment_unauthenticated(self): response = self.client.post(self.create_like_comment_url, self.data_like_comment, format='json') self.assertEquals(response.status_code, status.HTTP_403_FORBIDDEN) def test_post_like_comment_authenticated(self): self.client.force_login( CustomUser.objects.get_or_create(username='******')[0]) response = self.client.post(self.create_like_comment_url, self.data_like_comment, format='json') self.assertEquals(response.status_code, status.HTTP_400_BAD_REQUEST) def test_post_create_comment_article_unauthenticated(self): response = self.client.post(self.create_comment_url, self.data_comment, format='json') self.assertEquals(response.status_code, status.HTTP_403_FORBIDDEN) def test_post_create_comment_article_authenticated(self): self.client.force_login( CustomUser.objects.get_or_create(username='******')[0]) response = self.client.post(self.create_comment_url, self.data_comment, format='json') self.assertEquals(response.status_code, status.HTTP_400_BAD_REQUEST)
def post(self, request): serializer = self.serializer_class(data=request.data) if serializer.is_valid(): logger.info("Valid incoming data") username = serializer.data['username'] password1 = serializer.data['password1'] password2 = serializer.data['password2'] email = serializer.data['email'] user = CustomUser(username=username, password=password1, email=email) user.save() return Response(status=201) else: return Response(status=400)
class TestCreateArticle(APITestCase): def setUp(self): self.user = CustomUser(username="******", password="******") self.user.save() self.create_article_url = reverse("create_article") self.categories = Categories.objects.get_or_create(id=64) self.data_article = { "title": "testTile", "content_article": "test content", "id_category": 64 } def test_post_create_article_unauthenticate(self): response = self.client.post(self.create_article_url, self.data_article) self.assertEquals(response.status_code, status.HTTP_403_FORBIDDEN) def test_post_create_article_authenticated(self): self.client.force_login(CustomUser.objects.get_or_create(username='******')[0]) response = self.client.post(self.create_article_url, self.data_article, format='json') self.assertEquals(response.status_code, 200)
class TestPostLikeEndpoint(APITestCase): def setUp(self): self.user = CustomUser(username="******", password="******") self.user.save() self.create_like_article_url = reverse("create_like_article", args=[1, 'testuser']) self.data_like_article = { "reaction": 1 } def test_post_create_like_article_unaunthenticated(self): response = self.client.post( self.create_like_article_url, self.data_like_article ) self.assertEquals(response.status_code, status.HTTP_403_FORBIDDEN) def test_post_create_like_article_authenticated(self): self.client.force_login(CustomUser.objects.get_or_create(username='******')[0]) response = self.client.post(self.create_like_article_url, self.data_like_article, format='json') self.assertEquals(response.status_code, 400)
class TestCommand(TestCase): def setUp(self): self.list_categories = ['boisson', 'tartine', 'sandwich', 'viande'] self.list_products = ['coca', 'tartine nutella', 'club', 'entrecote'] self.command = Command() self.login = CustomUser(username='******', email='*****@*****.**', password='******') self.login.save() def test_get_email_user_subscribe(self): UserSubscribeEmail(id_user=self.login).save() self.command.get_email_user_subscribe() self.assertEquals(self.command.list_email, ['*****@*****.**']) def test_get_random_products(self): for i in range(3): category = Categories(name_category=self.list_categories[i]) category.save() Products(id_category=category, product_name=self.list_products[i]).save() self.command.get_random_products() self.command.text_content() self.assertEquals(len(self.command.txt.split('\n')), 4) self.assertEquals(len(self.command.list_products), 3) self.assertEquals(len(self.command.list_categories), 3) def test_send_all_email(self): self.command.list_email = ['*****@*****.**'] msg = ('Toujours pleins de substituants chez PureButterWeb', "ahahahah", '*****@*****.**', self.command.list_email) mail.send_mass_mail((msg, ), fail_silently=False) self.assertEquals(len(mail.outbox), 1)
def post(self, request, format=None): requested_data = json.loads(request.body) """ insert into base user """ username = requested_data["org_id"] email = requested_data["org_id"] password = requested_data["org_password"] # CustomUser.objects.filter(username=username,email=email, is_normaluser=False).update_or_create(password=password) try: base_user = CustomUser(username=username, email=email, is_normaluser=False) base_user.set_password(password) base_user.save() except: return Response({"msg": "failed", "error": "username " + username + " already exists"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) get_org = CustomUser.objects.get(username=username) print("get organization", get_org.id) try: """ getting request data """ org_type = requested_data['type'] org_category = requested_data["org_category"] org_name = requested_data["name"] org_address = requested_data["address"] org_image = utils.save_to_file(requested_data["image"], utils.replace_str_with_us(org_name)) org_description = requested_data["description"] website = requested_data["website"] main_coordinator_name = requested_data["main_coordinator_name"] main_coordinator_phone = requested_data["main_coordinator_phone"] main_coordinator_email = requested_data["main_coordinator_email"] sub_coordinator_name = requested_data["sub_coordinator_name"] sub_coordinator_phone = requested_data["sub_coordinator_phone"] sub_coordinator_email = requested_data["sub_coordinator_email"] # event_team = requested_data["team"] # event_manager_name = requested_data["manager_name"] # event_manager_phone = requested_data["manager_phone"] """ update & saving into organization table """ Organization.objects.filter(user=get_org.id).update( type = org_type, org_category = org_category, name = org_name, address = org_address, image = org_image, description = org_description, website = website, main_coordinator_name = main_coordinator_name, main_coordinator_phone = main_coordinator_phone, main_coordinator_email = main_coordinator_email, sub_coordinator_name = sub_coordinator_name, sub_coordinator_phone = sub_coordinator_phone, sub_coordinator_email = sub_coordinator_email, # team = event_team, # manager_name = event_manager_name, # manager_phone = event_manager_phone, ) return Response({"msg": "adding organisation data successfully"}, status=status.HTTP_201_CREATED) except Exception as e: print(e) return Response({"msg": "failed"}, status=status.HTTP_404_NOT_FOUND)