Esempio n. 1
0
 def test_create_project_view_with_only_required_fields_job(self):
   """
   Project job with only required fields and no availabilites.
   """
   factory = APIRequestFactory()
   project = {
     'name': "Name",
     'details': 'This needs to be big',
     'description': 'This needs to be big',
     'responsible': 'Marjor',
     'phone': '123123',
     'email': '*****@*****.**',
     'skills': [1],
     'causes': [2],
     'job': {
       'start_date': 20140416,
       'end_date': 20140417
     }
   }
   request = factory.post("/create/project/", {'project': project})
   u = User()
   u.save()
   n = Nonprofit(user=u)
   n.save()
   force_authenticate(request, user=u)
   response = views.create_project(request)
   self.assertEqual(response.data['detail'], 'Project succesfully created.')
   self.assertEqual(response.status_code, status.HTTP_201_CREATED)
Esempio n. 2
0
 def test_create_two_project_same_slug(self):
   """
   Creating two projects with same name to see if different slugs are created..
   """
   factory = APIRequestFactory()
   project = {
     'name': "Name",
     'details': 'This needs to be big',
     'description': 'This needs to be big',
     'responsible': 'Marjor',
     'phone': '123123',
     'email': '*****@*****.**',
     'skills': [1],
     'causes': [2],
     'job': {
       'start_date': 20140416,
       'end_date': 20140417
     }
   }
   u = User()
   u.save()
   n = Nonprofit(user=u)
   n.save()
   request = factory.post("/create/project/", {'project': project})
   force_authenticate(request, user=u)
   response = views.create_project(request)
   self.assertEqual(response.status_code, status.HTTP_201_CREATED)
   request = factory.post("/create/project/", {'project': project})
   force_authenticate(request, user=u)
   response = views.create_project(request)
   self.assertEqual(response.data['detail'], 'Project succesfully created.')
   self.assertEqual(response.status_code, status.HTTP_201_CREATED)
Esempio n. 3
0
 def test_create_project_view_with_only_required_fields_job(self):
     """
 Project job with only required fields and no availabilites.
 """
     factory = APIRequestFactory()
     project = {
         'name': "Name",
         'details': 'This needs to be big',
         'description': 'This needs to be big',
         'responsible': 'Marjor',
         'phone': '123123',
         'email': '*****@*****.**',
         'skills': [1],
         'causes': [2],
         'job': {
             'start_date': 20140416,
             'end_date': 20140417
         }
     }
     request = factory.post("/create/project/", {'project': project})
     u = User()
     u.save()
     n = Nonprofit(user=u)
     n.save()
     force_authenticate(request, user=u)
     response = views.create_project(request)
     self.assertEqual(response.data['detail'],
                      'Project succesfully created.')
     self.assertEqual(response.status_code, status.HTTP_201_CREATED)
Esempio n. 4
0
 def test_create_project_view_with_only_required_fields_but_no_work_or_job(
         self):
     """
 Project with only required fields but no work or job.
 """
     factory = APIRequestFactory()
     project = {
         'name': "Name",
         'details': 'This needs to be big',
         'description': 'This needs to be big',
         'responsible': 'Marjor',
         'phone': '123123',
         'email': '*****@*****.**',
         'skills': [1],
         'causes': [2]
     }
     request = factory.post("/create/project/", {'project': project})
     u = User()
     u.save()
     n = Nonprofit(user=u)
     n.save()
     force_authenticate(request, user=u)
     response = views.create_project(request)
     self.assertEqual(response.data,
                      {'detail': 'Needs to have project or work.'})
     self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
Esempio n. 5
0
 def test_create_two_project_same_slug(self):
     """
 Creating two projects with same name to see if different slugs are created..
 """
     factory = APIRequestFactory()
     project = {
         'name': "Name",
         'details': 'This needs to be big',
         'description': 'This needs to be big',
         'responsible': 'Marjor',
         'phone': '123123',
         'email': '*****@*****.**',
         'skills': [1],
         'causes': [2],
         'job': {
             'start_date': 20140416,
             'end_date': 20140417
         }
     }
     u = User()
     u.save()
     n = Nonprofit(user=u)
     n.save()
     request = factory.post("/create/project/", {'project': project})
     force_authenticate(request, user=u)
     response = views.create_project(request)
     self.assertEqual(response.status_code, status.HTTP_201_CREATED)
     request = factory.post("/create/project/", {'project': project})
     force_authenticate(request, user=u)
     response = views.create_project(request)
     self.assertEqual(response.data['detail'],
                      'Project succesfully created.')
     self.assertEqual(response.status_code, status.HTTP_201_CREATED)
Esempio n. 6
0
def create_nonprofit():
    address = create_address()
    u = User(googleaddress=address, is_active=True)
    u.save()
    n = Nonprofit(user=u, published=True)
    n.save()
    return n
Esempio n. 7
0
 def test_create_project_view_with_only_required_fields_work(self):
   """
   Project work with only required fields and no availabilites.
   """
   factory = APIRequestFactory()
   project = {
     'name': "Name",
     'details': 'This needs to be big',
     'description': 'This needs to be big',
     'responsible': 'Marjor',
     'phone': '11987456321',
     'email': '*****@*****.**',
     'skills': [{'id': 1}],
     'causes': [{'id': 1}],
     'work': {
       'weekly_hours': 1,
       'can_be_done_remotely': True
     }
   }
   request = factory.post("/create/project/", {'project': json.dumps(project)})
   u = User()
   u.save()
   n = Nonprofit(user=u)
   n.save()
   force_authenticate(request, user=u)
   response = views.create_project(request)
   self.assertEqual(response.data['detail'], 'Project succesfully created.')
   self.assertEqual(response.status_code, status.HTTP_201_CREATED)
Esempio n. 8
0
 def test_create_project_view_bad_request(self):
     """
 Project with bad data being sent.
 """
     factory = APIRequestFactory()
     request = factory.post("/create/project/", {"project": ""})
     u = User()
     u.save()
     n = Nonprofit(user=u)
     n.save()
     force_authenticate(request, user=u)
     response = views.create_project(request)
     self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
Esempio n. 9
0
 def test_create_project_view_bad_request(self):
   """
   Project with bad data being sent.
   """
   factory = APIRequestFactory()
   request = factory.post("/create/project/", {"project": ""})
   u = User()
   u.save()
   n = Nonprofit(user=u)
   n.save()
   force_authenticate(request, user=u)
   response = views.create_project(request)
   self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
Esempio n. 10
0
 def create_project(self):
     u = User(email="*****@*****.**", name="what", slug="hahah")
     u.save()
     n = Nonprofit(user=u, name="hahah")
     n.save()
     from random import randint
     random = randint(1,100)
     project = Project(nonprofit=n, name='name' + str(random))
     project.nonprofit = n
     project.save()
     project.job = Job(start_date=datetime.now().replace(tzinfo=pytz.timezone("America/Sao_Paulo")), end_date=datetime.now().replace(tzinfo=pytz.timezone("America/Sao_Paulo")))
     project.job.save()
     return project
Esempio n. 11
0
  def test_create_project_view_to_test_address_creation(self):
    """
    Project to test address creation.
    """

    skill = Skill(name="Programming")
    skill.save()
    cause = Cause(name="Atados")
    cause.save()

    factory = APIRequestFactory()
    project = {
      'name': "Name",
      'details': 'This needs to be big',
      'description': 'This needs to be big',
      'responsible': 'Marjor',
      'phone': '11987456321',
      'email': '*****@*****.**',
      'skills': [{'id': 1}],
      'causes': [{'id': 1}],
      'work': {
        'weekly_hours': 1,
        'can_be_done_remotely': True
      },
      'address': {
        'addr': {
          'formatted_address': 'R. Capote Valente, 701, São Paulo'
        },
        'typed_address2': 'Complemento'
      }
    }


    s = State(name="sao paulo")
    s.save()
    c = City(id=9422, name="sao paulo", state=s)
    c.save()
    request = factory.post("/create/project/", {'project': json.dumps(project)})
    u = User()
    u.save()
    n = Nonprofit(user=u)
    n.save()
    force_authenticate(request, user=u)
    response = views.create_project(request)
    self.assertEqual(response.status_code, status.HTTP_201_CREATED)
    p = Project.objects.all()[0]
    self.assertEqual(p.phone, "11987456321")
    self.assertEqual(p.name, "Name")
    self.assertTrue(p.googleaddress)
    self.assertEqual(p.googleaddress.typed_address, u"R. Capote Valente, 701, São Paulo")
    self.assertEqual(p.googleaddress.typed_address2, u"Complemento")
Esempio n. 12
0
 def test_project_date_creation(self):
   """
   Tests Project date creation.
   """
   u = User()
   u.save()
   n = Nonprofit(user=u, name="Nonprofit 1")
   n.save()
   p = self.create_project(n, "Project 1")
   now = datetime.utcnow().replace(tzinfo=pytz.timezone("America/Sao_Paulo"))
   self.assertTrue(isinstance(p, Project))
   self.assertEqual(p.__unicode__(), "Project 1 - Nonprofit 1")
   self.assertTrue(datesAreEqual(p.created_date, now))
   self.assertTrue(datesAreEqual(p.modified_date, now))
Esempio n. 13
0
 def test_project_date_creation(self):
     """
 Tests Project date creation.
 """
     u = User()
     u.save()
     n = Nonprofit(user=u, name="Nonprofit 1")
     n.save()
     p = self.create_project(n, "Project 1")
     now = datetime.utcnow().replace(
         tzinfo=pytz.timezone("America/Sao_Paulo"))
     self.assertTrue(isinstance(p, Project))
     self.assertEqual(p.__unicode__(), "Project 1 - Nonprofit 1")
     self.assertTrue(datesAreEqual(p.created_date, now))
     self.assertTrue(datesAreEqual(p.modified_date, now))
Esempio n. 14
0
 def create_project(self):
     u = User(email="*****@*****.**", name="what", slug="hahah")
     u.save()
     n = Nonprofit(user=u, name="hahah")
     n.save()
     from random import randint
     random = randint(1, 100)
     project = Project(nonprofit=n, name='name' + str(random))
     project.nonprofit = n
     project.save()
     project.job = Job(start_date=datetime.now().replace(
         tzinfo=pytz.timezone("America/Sao_Paulo")),
                       end_date=datetime.now().replace(
                           tzinfo=pytz.timezone("America/Sao_Paulo")))
     project.job.save()
     return project
Esempio n. 15
0
 def test_close_project(self):
   """
   Tests closing project that is open.
   """
   u = User()
   u.save()
   n = Nonprofit(user=u, name="Nonprofit 1")
   n.save()
   p = self.create_project(n, "Project 1")
   p.closed = False;
   factory = APIRequestFactory()
   request = factory.put("/close/project/", {'project': p.id})
   force_authenticate(request, user=u)
   response = views.close_project(request)
   self.assertEqual(response.data, True)
   self.assertEqual(response.status_code, status.HTTP_200_OK)
Esempio n. 16
0
 def test_close_project(self):
     """
 Tests closing project that is open.
 """
     u = User()
     u.save()
     n = Nonprofit(user=u, name="Nonprofit 1")
     n.save()
     p = self.create_project(n, "Project 1")
     p.closed = False
     factory = APIRequestFactory()
     request = factory.put("/close/project/", {'project': p.id})
     force_authenticate(request, user=u)
     response = views.close_project(request)
     self.assertEqual(response.data, True)
     self.assertEqual(response.status_code, status.HTTP_200_OK)
Esempio n. 17
0
 def test_comment_deletion(self):
     """
 Tests Comment deletion.
 """
     u1 = User(email="test", slug="user1")
     u1.save()
     n = Nonprofit(user=u1)
     n.save()
     p = Project(name="Project 1", nonprofit=n)
     p.save()
     u = User(email="*****@*****.**", slug="email")
     u.save()
     c = self.create_comment(p, u, "Comment nro 1")
     c.save()
     c.delete()
     self.assertTrue(c.deleted)
     self.assertTrue(c.deleted_date)
Esempio n. 18
0
 def test_comment_deletion(self):
   """
   Tests Comment deletion.
   """
   u1 = User(email="test", slug="user1") 
   u1.save()
   n = Nonprofit(user=u1)
   n.save()
   p = Project(name="Project 1", nonprofit=n)
   p.save()
   u = User(email="*****@*****.**", slug="email")
   u.save()
   c = self.create_comment(p, u, "Comment nro 1")
   c.save()
   c.delete()
   self.assertTrue(c.deleted)
   self.assertTrue(c.deleted_date)
Esempio n. 19
0
 def test_comment_deletion(self):
   """
   Tests Comment deletion.
   """
   u1 = User(email='test', slug='user1')
   u1.save()
   n = Nonprofit(user=u1)
   n.save()
   p = Project(name='Project 1', nonprofit=n)
   p.save()
   u = User(email='*****@*****.**', slug='email')
   u.save()
   c = self.create_comment(p, u, 'Comment nro 1')
   c.save()
   c.delete()
   self.assertTrue(c.deleted)
   self.assertTrue(c.deleted_date)
Esempio n. 20
0
 def test_comment_creation(self):
   """
   Tests Comment creation.
   """
   u1 = User(email="test", slug="user1") 
   u1.save()
   n = Nonprofit(user=u1)
   n.save()
   p = Project(name="Project 1", nonprofit=n)
   p.save()
   u = User(email="*****@*****.**", slug="email")
   u.save()
   c = self.create_comment(p, u, "Comment nro 1")
   c.save()
   now = datetime.utcnow().replace(tzinfo=pytz.timezone("America/Sao_Paulo"))
   self.assertTrue(isinstance(c, Comment))
   self.assertEqual(c.__unicode__(), "(Project 1) [email protected]: Comment nro 1")
   self.assertTrue(datesAreEqual(c.created_date, now))
Esempio n. 21
0
 def test_comment_creation(self):
   """
   Tests Comment creation.
   """
   u1 = User(email='test', slug='user1')
   u1.save()
   n = Nonprofit(user=u1)
   n.save()
   p = Project(name='Project 1', nonprofit=n)
   p.save()
   u = User(email='*****@*****.**', slug='email')
   u.save()
   c = self.create_comment(p, u, 'Comment nro 1')
   c.save()
   now = datetime.utcnow().replace(tzinfo=pytz.timezone('America/Sao_Paulo'))
   self.assertTrue(isinstance(c, Comment))
   self.assertEqual(c.__unicode__(), '(Project 1) [email protected]: Comment nro 1')
   self.assertTrue(datesAreEqual(c.created_date, now))
Esempio n. 22
0
 def test_create_project_view_to_test_address_creation(self):
     """
 Project to test address creation.
 """
     factory = APIRequestFactory()
     project = {
         'name': "Name",
         'details': 'This needs to be big',
         'description': 'This needs to be big',
         'responsible': 'Marjor',
         'phone': '123123',
         'email': '*****@*****.**',
         'skills': [1],
         'causes': [2],
         'work': {
             'weekly_hours': 1,
             'can_be_done_remotely': True
         },
         'address': {
             'addressline': 'Rua Test',
             'addressnumber': 234,
             'neighborhood': 'Bairro',
             'zipcode': '13230-455',
             'city': {
                 'id': 9422
             }
         }
     }
     s = State(name="sdfsdf")
     s.save()
     c = City(id=9422, name="sao paulo", state=s)
     c.save()
     request = factory.post("/create/project/", {'project': project})
     u = User()
     u.save()
     n = Nonprofit(user=u)
     n.save()
     force_authenticate(request, user=u)
     response = views.create_project(request)
     self.assertEqual(response.status_code, status.HTTP_201_CREATED)
     p = Project.objects.all()[0]
     self.assertEqual(p.phone, "123123")
     self.assertEqual(p.name, "Name")
     self.assertTrue(p.address)
Esempio n. 23
0
 def test_create_project_view_to_test_address_creation(self):
   """
   Project to test address creation.
   """
   factory = APIRequestFactory()
   project = {
     'name': "Name",
     'details': 'This needs to be big',
     'description': 'This needs to be big',
     'responsible': 'Marjor',
     'phone': '123123',
     'email': '*****@*****.**',
     'skills': [1],
     'causes': [2],
     'work': {
       'weekly_hours': 1,
       'can_be_done_remotely': True
     },
     'address': {
         'addressline': 'Rua Test',
         'addressnumber': 234,
         'neighborhood': 'Bairro', 
         'zipcode': '13230-455',
         'city': {'id': 9422}
         }
   }
   s = State(name="sdfsdf")
   s.save()
   c = City(id=9422, name="sao paulo", state=s)
   c.save()
   request = factory.post("/create/project/", {'project': project})
   u = User()
   u.save()
   n = Nonprofit(user=u)
   n.save()
   force_authenticate(request, user=u)
   response = views.create_project(request)
   self.assertEqual(response.status_code, status.HTTP_201_CREATED)
   p = Project.objects.all()[0]
   self.assertEqual(p.phone, "123123")
   self.assertEqual(p.name, "Name")
   self.assertTrue(p.address)
Esempio n. 24
0
 def test_comment_creation(self):
     """
 Tests Comment creation.
 """
     u1 = User(email="test", slug="user1")
     u1.save()
     n = Nonprofit(user=u1)
     n.save()
     p = Project(name="Project 1", nonprofit=n)
     p.save()
     u = User(email="*****@*****.**", slug="email")
     u.save()
     c = self.create_comment(p, u, "Comment nro 1")
     c.save()
     now = datetime.utcnow().replace(
         tzinfo=pytz.timezone("America/Sao_Paulo"))
     self.assertTrue(isinstance(c, Comment))
     self.assertEqual(c.__unicode__(),
                      "(Project 1) [email protected]: Comment nro 1")
     self.assertTrue(datesAreEqual(c.created_date, now))
Esempio n. 25
0
 def test_create_project_view_with_only_required_fields_but_no_work_or_job(self):
   """
   Project with only required fields but no work or job.
   """
   factory = APIRequestFactory()
   project = {
     'name': "Name",
     'details': 'This needs to be big',
     'description': 'This needs to be big',
     'responsible': 'Marjor',
     'phone': '123123',
     'email': '*****@*****.**',
     'skills': [1],
     'causes': [2]
   }
   request = factory.post("/create/project/", {'project': project})
   u = User()
   u.save()
   n = Nonprofit(user=u)
   n.save()
   force_authenticate(request, user=u)
   response = views.create_project(request)
   self.assertEqual(response.data, {'detail': 'Needs to have project or work.'})
   self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
Esempio n. 26
0
 def create_nonprofit(self):
     u = User()
     u.save()
     n = Nonprofit(user=u)
     n.save()
     return n
Esempio n. 27
0
def create_nonprofit(request, format=None):
  obj = json.loads(request.DATA['nonprofit'])
  email = obj['user']['email']

  obja = obj['address']
  address = Address()
  address.zipcode = obja['zipcode'][0:9]
  address.addressline = obja['addressline']
  address.addressline2 = obja.get('addressline2')
  address.addressnumber = obja['addressnumber'][0:9]
  address.neighborhood = obja['neighborhood']
  address.city = City.objects.get(id=obja['city']['id'])
  address.save()

  try:
   user = User.objects.get(email=email)
  except User.DoesNotExist:
   password = obj['user']['password']
   user = User.objects.create_user(email, password, slug=obj['user']['slug'])
   user.name = obj['user']['name']
   user.hidden_address = obj['hidden_address']
   user.phone = obj['phone']
   user.address = address
   user.save()

  if Nonprofit.objects.filter(user=user):
   return Response({'detail': 'Nonprofit already exists.'}, status.HTTP_404_NOT_FOUND)


  FACEBOOK_KEY = 'facebook_page'
  GOOGLE_KEY = 'google_page'
  TWITTER_KEY = 'twitter_handle'

  nonprofit = Nonprofit(user=user)
  nonprofit.name = obj['name']
  nonprofit.details = obj['details']
  nonprofit.description = obj['description']
  nonprofit.save()

  causes = obj['causes']
  for c in causes:
    nonprofit.causes.add(Cause.objects.get(name=c['name']))

  if FACEBOOK_KEY in obj:
   nonprofit.facebook_page = obj[FACEBOOK_KEY]
  if GOOGLE_KEY in obj:
   nonprofit.google_page = obj[GOOGLE_KEY]
  if TWITTER_KEY in obj:
   nonprofit.twitter_handle = obj[TWITTER_KEY]

  nonprofit.image = request.FILES.get('image')
  nonprofit.cover = request.FILES.get('cover')
  nonprofit.save()

  # Sending welcome email on nonprofit signup
  plaintext = get_template('email/nonprofitSignup.txt')
  htmly     = get_template('email/nonprofitSignup.html')
  d = Context()
  subject, from_email, to = 'Cadastro no Atados enviado com sucesso!', '*****@*****.**', nonprofit.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()

  return Response({'detail': 'Nonprofit succesfully created.'}, status.HTTP_200_OK)
Esempio n. 28
0
 def create_nonprofit(self):
   u = User()
   u.save()
   n = Nonprofit(user=u)
   n.save()
   return n
Esempio n. 29
0
File: views.py Progetto: Vedolin/api
def create_nonprofit(request, format=None):
    obj = json.loads(request.DATA["nonprofit"])
    email = obj["user"]["email"]

    obja = obj["address"]
    address = Address()
    address.zipcode = obja["zipcode"][0:9]
    address.addressline = obja["addressline"]
    address.addressline2 = obja.get("addressline2")
    address.addressnumber = obja["addressnumber"][0:9]
    address.neighborhood = obja["neighborhood"]
    address.city = City.objects.get(id=obja["city"]["id"])
    address.save()

    try:
        user = User.objects.get(email=email)
    except User.DoesNotExist:
        password = obj["user"]["password"]
        user = User.objects.create_user(email, password, slug=obj["user"]["slug"])
        user.name = obj["user"]["name"]
        user.hidden_address = obj["hidden_address"]
        user.phone = obj["phone"]
        user.address = address
        user.save()

    if Nonprofit.objects.filter(user=user):
        return Response({"detail": "Nonprofit already exists."}, status.HTTP_404_NOT_FOUND)

    FACEBOOK_KEY = "facebook_page"
    GOOGLE_KEY = "google_page"
    TWITTER_KEY = "twitter_handle"

    nonprofit = Nonprofit(user=user)
    nonprofit.name = obj["name"]
    nonprofit.details = obj["details"]
    nonprofit.description = obj["description"]
    nonprofit.save()

    causes = obj["causes"]
    for c in causes:
        nonprofit.causes.add(Cause.objects.get(name=c["name"]))

    if FACEBOOK_KEY in obj:
        nonprofit.facebook_page = obj[FACEBOOK_KEY]
    if GOOGLE_KEY in obj:
        nonprofit.google_page = obj[GOOGLE_KEY]
    if TWITTER_KEY in obj:
        nonprofit.twitter_handle = obj[TWITTER_KEY]

    nonprofit.image = request.FILES.get("image")
    nonprofit.image_small = request.FILES.get("image")
    nonprofit.image_medium = request.FILES.get("image")
    nonprofit.image_large = request.FILES.get("image")
    nonprofit.cover = request.FILES.get("cover")
    nonprofit.save()

    # Sending welcome email on nonprofit signup
    plaintext = get_template("email/nonprofitSignup.txt")
    htmly = get_template("email/nonprofitSignup.html")
    d = Context()
    subject, from_email, to = "Cadastro no Atados enviado com sucesso!", "*****@*****.**", nonprofit.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()

    return Response({"detail": "Nonprofit succesfully created."}, status.HTTP_200_OK)