def test_apply_volunteer_to_project_view_after_canceled(self): """ Apply again after canceled. """ u = User(email="*****@*****.**", name="test", slug="test") u.is_email_verified = True u.save() volunteer = Volunteer(user=u) volunteer.save() project = self.create_project() factory = APIRequestFactory() request = factory.post("/apply_volunteer_to_project/", {"project": project.id}) force_authenticate(request, user=volunteer.user) response = views.apply_volunteer_to_project(request) self.assertEqual(response.data, {'Applied'}) self.assertEqual(response.status_code, status.HTTP_200_OK) request = factory.post("/apply_volunteer_to_project/", {"project": project.id}) force_authenticate(request, user=volunteer.user) response = views.apply_volunteer_to_project(request) self.assertEqual(response.data, {'Canceled'}) self.assertEqual(response.status_code, status.HTTP_200_OK) request = factory.post("/apply_volunteer_to_project/", {"project": project.id}) force_authenticate(request, user=volunteer.user) response = views.apply_volunteer_to_project(request) self.assertEqual(response.data, {'Applied'}) self.assertEqual(response.status_code, status.HTTP_200_OK)
def test_apply_volunteer_to_project_view(self): """ Ensure we can apply a volunteer to a project. """ a = ApplyStatus(name="Candidato", id=2) a.save() u = User(email="*****@*****.**", name="test", slug="test") u.is_email_verified = True u.save() volunteer = Volunteer(user=u) volunteer.save() project = self.create_project() factory = APIRequestFactory() request = factory.post("/apply_volunteer_to_project/", {"project": project.id, 'name': 'new name', 'phone': '3444-3434'}) force_authenticate(request, user=volunteer.user) response = views.apply_volunteer_to_project(request) self.assertEqual(response.data, {'Applied'}) self.assertEqual(response.status_code, status.HTTP_200_OK) u = User.objects.get(slug='test') self.assertEqual('new name', u.name) self.assertEqual('3444-3434', u.phone) project = Project.objects.get(pk=project.id) self.assertEqual(project.applied_count, 1)
def test_apply_volunteer_to_project_view(self): """ Ensure we can apply a volunteer to a project. """ a = ApplyStatus(name="Candidato", id=2) a.save() u = User(email="*****@*****.**", name="test", slug="test") u.is_email_verified = True u.save() volunteer = Volunteer(user=u) volunteer.save() project = self.create_project() factory = APIRequestFactory() request = factory.post("/apply_volunteer_to_project/", { "project": project.id, 'name': 'new name', 'phone': '3444-3434' }) force_authenticate(request, user=volunteer.user) response = views.apply_volunteer_to_project(request) self.assertEqual(response.data, {'Applied'}) self.assertEqual(response.status_code, status.HTTP_200_OK) u = User.objects.get(slug='test') self.assertEqual('new name', u.name) self.assertEqual('3444-3434', u.phone)
def test_apply_volunteer_to_project_view_already_apply(self): """ Unaply when apply already exists. """ u = User(email="*****@*****.**", name="test", slug="test") u.is_email_verified = True u.save() volunteer = Volunteer(user=u) volunteer.save() project = self.create_project() factory = APIRequestFactory() request = factory.post("/apply_volunteer_to_project/", {"project": project.id}) force_authenticate(request, user=volunteer.user) response = views.apply_volunteer_to_project(request) project = Project.objects.get(pk=project.id) nonprofit = Nonprofit.objects.get(user__slug="hahah") self.assertEqual(response.data, {'Applied'}) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(project.applied_count, 1) # We only test if volunteer count increases, we don't test if it lowers # when unapplying because Nonprofit.get_volunteers and N.get_volunteers_numbers # doesn't filter applies by cancelled self.assertEqual(nonprofit.volunteer_count, 1) request = factory.post("/apply_volunteer_to_project/", {"project": project.id}) force_authenticate(request, user=volunteer.user) response = views.apply_volunteer_to_project(request) project = Project.objects.get(pk=project.id) self.assertEqual(response.data, {'Canceled'}) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(project.applied_count, 0)
def test_apply_creation(self): """ Tests Apply. """ u = User(name="Voluntario", slug="voluntario") u.save() v = Volunteer(user=u) v.save() p = Project(name="Project") a = Apply(status=ApplyStatus(), volunteer=v, project=p) self.assertTrue(isinstance(a, Apply)) self.assertEqual(a.__unicode__(), "[False] Voluntario - Project")
def test_apply_volunteer_to_project_view_without_project(self): """ Apply volunteer to project without project id. """ u = User(email="*****@*****.**", name="test", slug="test") u.is_email_verified = True u.save() volunteer = Volunteer(user=u) volunteer.save() factory = APIRequestFactory() request = factory.post("/apply_volunteer_to_project/", {"project": ''}) force_authenticate(request, user=volunteer.user) response = views.apply_volunteer_to_project(request) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_apply_volunteer_to_project_view_without_auth(self): """ Apply no volunteer to a project. """ a = ApplyStatus(name="Candidato", id=2) a.save() u = User(email="*****@*****.**", name="test", slug="test") u.save() volunteer = Volunteer(user=u) volunteer.save() project = self.create_project() factory = APIRequestFactory() request = factory.post("/apply_volunteer_to_project/", {"project": project.id}) response = views.apply_volunteer_to_project(request) self.assertEqual(response.data, {"No user logged in."}) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def test_apply_volunteer_to_project_view_not_verified(self): """ Apply a volunteer not verified to a project. """ a = ApplyStatus(name="Candidato", id=2) a.save() u = User(email="*****@*****.**", name="test", slug="test") u.save() volunteer = Volunteer(user=u) volunteer.save() project = self.create_project() factory = APIRequestFactory() request = factory.post("/apply_volunteer_to_project/", {"project": project.id}) force_authenticate(request, user=volunteer.user) response = views.apply_volunteer_to_project(request) self.assertEqual(response.data, {"403": "Volunteer has not actived its account by email."}) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def test_volunteer_creation(self): """ Test Volunteer Model. """ u = User(email=self.email, slug=self.slug, name=self.name) v = Volunteer.create(u) self.assertTrue(isinstance(v, Volunteer)) self.assertEqual(v.__unicode__(), self.name) self.assertEqual(v.get_type(), "VOLUNTEER"); self.assertEqual(v.image_name("teste.jpg"), "volunteer/" + self.slug + "/" + self.slug + ".jpg")
def test_apply_volunteer_to_project_view_not_verified(self): """ Apply a volunteer not verified to a project. """ a = ApplyStatus(name="Candidato", id=2) a.save() u = User(email="*****@*****.**", name="test", slug="test") u.save() volunteer = Volunteer(user=u) volunteer.save() project = self.create_project() factory = APIRequestFactory() request = factory.post("/apply_volunteer_to_project/", {"project": project.id}) force_authenticate(request, user=volunteer.user) response = views.apply_volunteer_to_project(request) self.assertEqual( response.data, {"403": "Volunteer has not actived its account by email."}) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def test_volunteer_creation(self): """ Test Volunteer Model. """ u = User(email=self.email, slug=self.slug, name=self.name) v = Volunteer.create(u) self.assertTrue(isinstance(v, Volunteer)) self.assertEqual(v.__unicode__(), self.name) self.assertEqual(v.get_type(), "VOLUNTEER") self.assertEqual(v.image_name("teste.jpg"), "volunteer/" + self.slug + "/" + self.slug + ".jpg")
def create_volunteer(request, format=None): slug = request.DATA['slug'] email = request.DATA['email'] password = request.DATA['password'] try: user = User.objects.get(email=email) return Response({'detail': 'User already exists.'}, status.HTTP_404_NOT_FOUND) except User.DoesNotExist: user = User.objects.create_user(email, password, slug=slug, site=request.META.get('HTTP_ORIGIN', None)) # Sending welcome email on email signup plaintext = get_template('email/volunteerSignup.txt') htmly = get_template('email/volunteerSignup.html') d = Context({ 'name': user.name }) subject, from_email, to = 'Seja bem vindo ao Atados', '*****@*****.**', user.email text_content = plaintext.render(d) html_content = htmly.render(d) msg = EmailMultiAlternatives(subject, text_content, from_email, [to]) msg.attach_alternative(html_content, "text/html") msg.send() volunteer = Volunteer(user=user) volunteer.save() return Response({'detail': 'Volunteer succesfully created.'}, status.HTTP_201_CREATED)
def create_volunteer(request, format=None): slug = request.DATA["slug"] email = request.DATA["email"] password = request.DATA["password"] try: user = User.objects.get(email=email) return Response({"detail": "User already exists."}, status.HTTP_404_NOT_FOUND) except User.DoesNotExist: site = request.META.get("HTTP_ORIGIN", "https://www.atados.com.br") user = User.objects.create_user(email, password, slug=slug, site=site) # Sending welcome email on email signup plaintext = get_template("email/volunteerSignup.txt") htmly = get_template("email/volunteerSignup.html") subject = u"Seja bem vindo ao Atados" d = Context({"name": user.name}) from_email, to = "*****@*****.**", user.email text_content = plaintext.render(d) html_content = htmly.render(d) msg = EmailMultiAlternatives(subject, text_content, from_email, [to]) msg.attach_alternative(html_content, "text/html") msg.send() volunteer = Volunteer(user=user) volunteer.save() return Response({"detail": "Volunteer succesfully created."}, status.HTTP_201_CREATED)
def facebook_auth(request, format=None): accessToken = request.DATA["accessToken"] userID = request.DATA["userID"] expiresIn = request.DATA["expiresIn"] getPhoto = request.DATA.get("getPhoto", None) try: graph = facebook.GraphAPI(accessToken) me = graph.get("me") except facebook.FacepyError as e: print "ERROR - %d - %s" % (sys.exc_traceback.tb_lineno, e) return Response({"We don't have permissions to log in the user."}, status.HTTP_403_FORBIDDEN) volunteer = Volunteer.objects.filter(facebook_uid=userID) if volunteer: volunteer = volunteer[0] if getPhoto: faceImage = graph.get("me/picture?redirect=0&height=200&type=normal&width=200") imgurl = iri_to_uri(faceImage["data"]["url"]) image = NamedTemporaryFile(delete=True) image.write(urllib2.urlopen(imgurl).read()) image.flush() imgname = "volunteer/%s/%s.jpg" % (volunteer.user.slug, volunteer.user.slug) volunteer.image.save(imgname, File(image)) volunteer.save() user = volunteer.user user.last_login = timezone.now() user.save() else: try: user = User.objects.get(email=me["email"]) volunteer = Volunteer.objects.get(user=user) except: email = me.get("email", None) name = me.get("name", None) if name: slug = slugify(name) elif email: slug = slugify(email) else: return Response({"Could not creata slug for account."}, status.HTTP_400_BAD_REQUEST) user = User.objects.create_user(slug=slug, email=email, password="******") volunteer = Volunteer(user=user) try: # Sending welcome email on facebook signup plaintext = get_template("email/volunteerFacebookSignup.txt") htmly = get_template("email/volunteerFacebookSignup.html") d = Context({"name": name}) subject, from_email, to = "Seja bem vindo ao Atados", "*****@*****.**", email text_content = plaintext.render(d) html_content = htmly.render(d) msg = EmailMultiAlternatives(subject, text_content, from_email, [to]) msg.attach_alternative(html_content, "text/html") msg.send() except: pass user.last_login = timezone.now() user.name = me.get("name", None) user.save() faceImage = graph.get("me/picture?redirect=0&height=200&type=normal&width=200") imgurl = iri_to_uri(faceImage["data"]["url"]) image = NamedTemporaryFile(delete=True) image.write(urllib2.urlopen(imgurl).read()) image.flush() imgname = "volunteer/%s/%s.jpg" % (volunteer.user.slug, volunteer.user.slug) volunteer.image.save(imgname, File(image)) volunteer.facebook_uid = userID volunteer.facebook_access_token = accessToken volunteer.facebook_access_token_expires = expiresIn volunteer.save() if not volunteer: return Response({"Could not get user through facebook login."}, status.HTTP_404_NOT_FOUND) client = Client.objects.get(id=1) token = AccessToken.objects.create(user=user, client=client) data = {"access_token": token.token, "user": VolunteerSerializer(volunteer).data} return Response(data, status.HTTP_200_OK)