示例#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)
示例#2
0
def profile(request, username=None):
    """ for displaying and editing a users profile """

    context = {}
    if not username and not request.user:
        return HttpResponseRedirect("/")

    # this is a person viewing their own profile page, make it editable
    if not username:
        user = request.user
        context["self"] = True
    elif re.match(r"^\d+", username):
        user = User.objects.filter(id=username)
    else:
        user = User.objects.filter(Q(email=username) | Q(username=username))

    user_profile = UserProfile.objects.filter(Q(user=user))
    if len(user_profile) == 0:
        return HttpResponseRedirect("/")

    user_profile = user_profile[0]  # replace above logic with None or single object?

    if request.method == "POST":

        avatar = request.FILES.get("file")
        if avatar:
            user_profile.media_url = base.send_to_remote_storage(
                avatar, user_profile.make_s3_media_url(avatar), "image/png"
            )
            user_profile.save()
        else:
            user_profile.user.first_name = request.POST.get("first_name")
            user_profile.user.last_name = request.POST.get("last_name")
            user_profile.user.email = request.POST.get("email")
            user_profile.bio = request.POST.get("bio")
            user_profile.occupation = request.POST.get("occupation")
            user_profile.location = request.POST.get("location")
            user_profile.skills = request.POST.get("skills")
            user_profile.user.save()
            user_profile.save()

            context["alert"] = {"type": "success", "message": "Your changes have been saved."}

    context["opportunities"] = Opportunity.objects.filter(engaged_by=user)
    context["my_projects"] = Project.objects.filter(Q(created_by=user) | Q(followed_by=user))

    context["user_profile"] = user_profile

    return render(request, "profile.html", context)
示例#3
0
def new_project(request):

    # Show the sign page and collect emails
    context = {}
    context["causes"] = CAUSES

    allow_sponsorship = True

    if request.GET.get("org"):
        org = Organization.objects.get(id=request.GET.get("org"))

    elif request.user and request.user.get_profile().organization:
        org = request.user.get_profile().organization

    else:
        context["alert"] = {"type": "danger", "message": "Could not find a valid organization"}
        return render(request, "new_project.html", context)

    if request.method == "POST":

        context["organization"] = org

        project_form = ProjectForm(request.POST)
        project = project_form.save(commit=False)

        if project_form.is_valid():

            project.organization = org
            project.created_by = request.user

            project.save()

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

            if allow_sponsorship:

                project.create_sponsorship()

            # 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:
                project.media_url = base.send_to_remote_storage(
                    media_file, project.make_s3_media_url(media_file), "image/png"
                )
                project.save()

            # send admin email with link adminpanel to change project status
            created_by = project.created_by.first_name + " " + project.created_by.last_name
            subj = "[%s] A new project submission" % request.get_host()
            body = """
                A new project has been submitted by %s:<br /><br />
                <b>%s</b><br /><br />
                %s<br /><br />
                <a href='%s'>View this project</a><br />
                """ % (
                created_by,
                project.name,
                project.short_desc,
                project.get_url(request),
            )

            base.send_admin_email(subj, body, html_content=body)

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

        else:

            context["alert"] = {"type": "danger", "message": "The form is invalid"}
            return render(request, "new_project.html", context)

    else:

        return render(request, "new_project.html", context)
示例#4
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)
示例#5
0
def add_update(request, *args):

    if request.REQUEST.get('id'):

        update = Update.objects.get(id=request.REQUEST['id'])
        orgid = update.organization.id if update.organization else None
        pid = update.project.id if update.project else None
        oid = update.opportunity.id if update.opportunity else None

    else:

        update = None
        orgid = request.REQUEST.get('orgid')
        pid = request.REQUEST.get('pid')
        oid = request.REQUEST.get('oid')

    text = request.REQUEST.get('text')
    url = None
    response = {"success": "true"}

    project = Project.objects.get(id=pid)

    # handle file upload
    if request.body:

        mime_type = request.META.get('HTTP_X_MIME_TYPE')
    
        # TODO: check mimetype for proper file extensions
        # TODO: create create_s3_media_url() on update object, then move this code below object creation
        ext = 'png'
        hash = hashlib.sha1()
        hash.update(str(time.time()))
        filename = hash.hexdigest()[:10]
        
        url = base.send_to_remote_storage(request.body, 'project/%s/%s.%s' % (pid, filename, ext), mime_type)

    # edit previous post
    if update:

        if update.created_by == request.user or project.created_by == request.user:

            if text:
                update.text = text
            if url:
                update.media_url = url

            update.save()

        else:

            response.update({'success':False, 'message': "You don't have permission to edit this post."})

    # create new post
    elif (oid and OpportunityEngagement.objects.get(opportunity_id=oid, user=request.user)) or project.created_by == request.user:

        Update.objects.create(organization_id=orgid, opportunity_id=oid, project_id=pid, text=text, created_by=request.user, media_url=url)
        
    else:

        response.update({'success': False, 'message': 'Not a valid project id'})


    return HttpResponse(json.dumps(response), mimetype="application/json")