Esempio n. 1
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")
Esempio n. 2
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")
Esempio n. 3
0
  def handle_noargs(self, **options):

    cursor = connections['legacy'].cursor()
    cursor.execute(
      '''
        SELECT DISTINCT                                                                
           flag_content.uid AS 'uid',      
           flag_content.content_id AS 'nid',
           flag_content.timestamp AS 'date'                                             
         FROM                                                                           
           flag_content                                                                 
         LEFT JOIN users ON flag_content.uid = users.uid                                
         WHERE flag_content.fid = 4
      ;''')
                                                                                      
    desc = cursor.description
    print "Now processing....%d" % cursor.rowcount
    print
    i = 0
    new = 0
    for row in cursor.fetchall():
      i = i + 1
      row = dict(zip([col[0] for col in desc], row))
      try:
        volunteer = User.objects.get(legacy_uid=row['uid']).volunteer
        project = Project.objects.get(legacy_nid=row['nid'])
        try:
          apply = Apply.objects.get(volunteer=volunteer, project=project)
        except:
          apply = Apply()                                                                                        
          apply.volunteer = volunteer                                                                            
          apply.project = project                                                                                
          apply.date = datetime.utcfromtimestamp(row['date']).replace(tzinfo=pytz.timezone("America/Sao_Paulo"))
          apply.status = ApplyStatus.objects.get(name="Candidato")                                              
          print "%s %s" % (apply.volunteer, apply.project)
          new = new + 1
          apply.save()                                                                                           
      except Exception as e: 
        print "ERROR - %d - %s" % (sys.exc_traceback.tb_lineno, e)
    print new
Esempio n. 4
0
File: views.py Progetto: Vedolin/api
def apply_volunteer_to_project(request, format=None):
    if request.user.is_authenticated():
        if not request.user.is_email_verified:
            return Response({"403": "Volunteer has not actived its account by email."}, status.HTTP_403_FORBIDDEN)

        volunteer = Volunteer.objects.get(user=request.user)

        try:
            project = Project.objects.get(id=request.DATA["project"])
        except Exception as e:
            error = "ERROR - %d - %s" % (sys.exc_traceback.tb_lineno, e)
            return Response({"No project id. " + error}, status.HTTP_400_BAD_REQUEST)

        message = request.DATA.get("message", "")
        phone = request.DATA.get("phone", "")
        name = request.DATA.get("name", volunteer.user.name)

        if name:
            if volunteer.user.name != name:
                volunteer.user.name = name
                volunteer.user.save()

        if phone:
            if volunteer.user.phone != phone:
                volunteer.user.phone = phone
                volunteer.user.save()

        try:
            # If apply exists, then cancel it
            apply = Apply.objects.get(project=project, volunteer=volunteer)
            if not apply.canceled:
                apply.canceled = True
                try:
                    apply.status = ApplyStatus.objects.get(id=3)  # Desistente
                except:
                    apply.status = ApplyStatus(name="Desistente", id=3)  # Desistente
                apply.save()
                # TODO remove 4 week email from message queue if passed 30 days
                return Response({"Canceled"}, status.HTTP_200_OK)

            else:
                apply.canceled = False
                try:
                    apply.status = ApplyStatus.objects.get(id=2)  # Candidato
                except:
                    apply.status = ApplyStatus(name="Candidato", id=2)  # Candidato
                apply.save()

                try:
                    # Schedule email to be sent 30 days after this Apply
                    eta = datetime.now() + timedelta(days=30)
                    send_email_to_volunteer_after_4_weeks_of_apply.apply_async(
                        eta=eta, kwargs={"project_id": project.id, "volunteer_email": volunteer.user.email}
                    )
                except:
                    pass

                # if pontual, schedule email to be sent 3 days before
                try:
                    if project.job.start_date:
                        eta = project.job.start_date - timedelta(days=3)
                        send_email_to_volunteer_3_days_before_pontual.apply_async(
                            eta=eta, kwargs={"project_id": project.id, "volunteer_email": volunteer.user.email}
                        )
                except:
                    pass

                return Response({"Applied"}, status.HTTP_200_OK)

        except:  # new apply
            apply = Apply()
            apply.project = project
            apply.volunteer = volunteer
            try:
                apply.status = ApplyStatus.objects.get(id=2)  # Candidato
            except:
                apply.status = ApplyStatus(name="Candidato", id=2)  # Candidato
            apply.save()

            try:
                # Sending email to volunteer after user applied to project
                plaintext = get_template("email/volunteerAppliesToProject.txt")
                htmly = get_template("email/volunteerAppliesToProject.html")
                d = Context(
                    {"project_name": project.name, "project_email": project.email, "project_phone": project.phone}
                )
                subject, from_email, to = (
                    u"Confirmação do ato. Parabéns.",
                    "*****@*****.**",
                    volunteer.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()
            except:
                pass

            try:
                # Sending email to nonprofit after user applied to project
                plaintext = get_template("email/nonprofitGetsNotifiedAboutApply.txt")
                htmly = get_template("email/nonprofitGetsNotifiedAboutApply.html")
                d = Context(
                    {
                        "volunteer_name": volunteer.user.name,
                        "volunteer_email": volunteer.user.email,
                        "volunteer_phone": volunteer.user.phone,
                        "volunteer_message": message,
                        "project_name": project.name,
                    }
                )
                email = project.email if project.email else project.nonprofit.user.email
                subject, from_email, to = u"Um voluntário se candidatou a seu ato!", "*****@*****.**", 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

            try:
                # Schedule email to be sent 30 days after this Apply
                eta = datetime.now() + timedelta(days=30)
                send_email_to_volunteer_after_4_weeks_of_apply.apply_async(
                    eta=eta, kwargs={"project_id": project.id, "volunteer_email": volunteer.user.email}
                )
            except:
                pass

            # if pontual, schedule email to be sent 3 days before
            try:
                if project.job.start_date:
                    eta = project.job.start_date - timedelta(days=3)
                    send_email_to_volunteer_3_days_before_pontual.apply_async(
                        eta=eta, kwargs={"project_id": project.id, "volunteer_email": volunteer.user.email}
                    )
            except:
                pass

            return Response({"Applied"}, status.HTTP_200_OK)

    return Response({"No user logged in."}, status.HTTP_403_FORBIDDEN)