Exemplo n.º 1
0
def add_opportunity(request, pid=None, sponsorship=False):

    context = {}
    project = get_object_or_404(Project, pk=pid)

    context["project"] = project
    context["opportunities"] = Opportunity.objects.filter(project=pid)

    if request.method == "POST":

        if request.POST.get("name") == "" and request.POST.get("short_desc") == "" and not request.POST.get("add"):
            return HttpResponseRedirect("/project/%s/manage" % project.id)

        opp_form = OpportunityForm(request.POST)
        opp = opp_form.save(commit=False)

        if opp_form.is_valid():

            opp.project = project
            opp.organization = project.organization
            opp.created_by = request.user

            opp.save()

            opp.sites = [settings.SITE_ID]
            opp.save()

            # this has to occur after initial save b/c we use pk id as part of the s3 filepath
            media_file = request.FILES.get("file")
            if media_file:
                opp.media_url = base.send_to_remote_storage(media_file, opp.make_s3_media_url(media_file), "image/png")
                opp.save()

            if request.POST.get("add"):

                return HttpResponseRedirect("/project/%s/opportunity/add" % project.id)

            else:

                return HttpResponseRedirect("/project/%s/manage" % project.id)

        else:

            return HttpResponse("error")

    elif sponsorship:

        project.create_sponsorship()

        return HttpResponseRedirect(request.META.get("HTTP_REFERER", "/"))

    else:

        return render(request, "add_opportunity.html", context)
Exemplo n.º 2
0
def manage_project(request, pid=1):

    project = get_object_or_404(Project, pk=pid)

    if (not request.user.is_authenticated() or project.created_by != request.user) and not request.user.is_staff:
        return HttpResponseRedirect("/")

    opps = Opportunity.objects.filter(project=project)

    context = {}
    context["no_sponsorship"] = False if opps.filter(sponsorship=True) else True
    context["COUNTRIES"] = COUNTRIES

    engagements = OpportunityEngagement.objects.filter(project_id=pid)
    context.update(
        {
            "project": project,
            "opportunities": opps,
            "engagements": engagements,
            "updates": Update.objects.filter(project=project).order_by("-date_created"),
        }
    )

    for u in context["updates"]:

        (u.video, u.text) = base.embed_video(u.text)

    if request.user.is_authenticated():
        context["is_following"] = request.user in project.followed_by.all()

    if request.method == "POST":

        if request.POST.get("type", "") == "opportunity":

            opportunity = get_object_or_404(Opportunity, pk=request.POST["id"])
            opportunity_form = OpportunityForm(request.POST or None, instance=opportunity)
            opportunity = opportunity_form.save(commit=False)

            media_file = request.FILES.get("file")
            if media_file:
                opportunity.media_url = base.send_to_remote_storage(
                    media_file, opportunity.make_s3_media_url(media_file), "image/png"
                )

            if opportunity_form.is_valid():
                opportunity.save()
            else:
                context["error"] = "failed to update opportunity"

        else:

            project_form = ProjectForm(request.POST or None, instance=project)
            original_media_url = project.media_url
            project = project_form.save(commit=False)

            media_file = request.FILES.get("file")

            if media_file:
                project.media_url = base.send_to_remote_storage(
                    media_file, project.make_s3_media_url(media_file), "image/png"
                )
            else:
                project.media_url = original_media_url

            if project_form.is_valid():

                project.save()

            else:

                context["error"] = "failed to update project"

    return render(request, "manage_project.html", context)