コード例 #1
0
def profile(request):
    # We always have the user, but not always the profile. And we need a bit
    # of a hack around the normal forms code since we have two different
    # models on a single form.
    (profile, created) = UserProfile.objects.get_or_create(pk=request.user.pk)

    # Don't allow users whose accounts were created via oauth to change
    # their email, since that would kill the connection between the
    # accounts.
    can_change_email = (request.user.password != OAUTH_PASSWORD_STORE)

    # We may have a contributor record - and we only show that part of the
    # form if we have it for this user.
    try:
        contrib = Contributor.objects.get(user=request.user.pk)
    except Contributor.DoesNotExist:
        contrib = None

    contribform = None

    if request.method == 'POST':
        # Process this form
        userform = UserForm(data=request.POST, instance=request.user)
        profileform = UserProfileForm(data=request.POST, instance=profile)
        if contrib:
            contribform = ContributorForm(data=request.POST, instance=contrib)

        if userform.is_valid() and profileform.is_valid() and (
                not contrib or contribform.is_valid()):
            userform.save()
            profileform.save()
            if contrib:
                contribform.save()
            return HttpResponseRedirect("/account/")
    else:
        # Generate form
        userform = UserForm(instance=request.user)
        profileform = UserProfileForm(instance=profile)
        if contrib:
            contribform = ContributorForm(instance=contrib)

    return render_pgweb(
        request, 'account', 'account/userprofileform.html', {
            'userform': userform,
            'profileform': profileform,
            'contribform': contribform,
            'can_change_email': can_change_email,
        })
コード例 #2
0
ファイル: views.py プロジェクト: 350dotorg/localpower
def card(request, contrib_id=None, form_name=None):
    # Get the contributor object if specified
    if not contrib_id:
        contributor = Contributor()
    else:
        contributor = get_object_or_404(Contributor, pk=contrib_id)

        # Make sure the logged in user has access to view the card. User must have entered a survey for this 
        # contributor, or else have the edit_any_contributor permission
        if ContributorSurvey.objects.filter(entered_by=request.user, contributor=contributor).exists() == False \
            and request.user.has_perm("contributor.edit_any_contributor") == False:
            return forbidden(request, "You don't have permission to edit this contributor.")

    contrib_form = ContributorForm((request.POST or None), instance=contributor)

    # If a survey_form was specified, use that, otherwise use a default
    form_name = request.GET.get("form_name", str(form_name))
    try:
        survey_form = getattr(survey_forms, form_name)(contributor, request.user, (request.POST or None))
    except AttributeError:
        survey_form = survey_forms.PledgeCard(contributor, request.user, (request.POST or None))

    if request.method == 'POST' and contrib_form.is_valid() and survey_form.is_valid():

        # If the contrib form finds that the email already exists, it'll return the matched contributor
        contributor = contrib_form.save()

        # Make sure the survey form has the right contributor in case one was matched on email
        survey_form.contributor = contributor
        survey_form.save()

        messages.success(request, "Commitment card for %s saved" % contributor.name)

    if request.is_ajax():
        if request.method == 'POST':
            message_html = loader.render_to_string('_messages.html', {}, RequestContext(request))
            return HttpResponse(message_html)

        template = 'commitments/_card.html'
    else:
        if request.method == 'POST':
            if request.POST.get("submit") == "save_and_add_another":
                return redirect("commitments_card_create")
            else:
                return redirect("commitments_show")
        template = 'commitments/card.html'

    # Get the Surveys for the survey select widget
    survey_types = Survey.objects.filter(is_active=True)

    return render_to_response(template, {
        "survey_form": survey_form,
        "contrib_form": contrib_form,
        "survey_types": survey_types,
        "current_form_name": survey_form.__class__.__name__
    }, context_instance=RequestContext(request))
コード例 #3
0
def take_pledge(request):
    form = ContributorForm(data=request.POST)
    valid = form.is_valid()
    if valid:
        contributor = form.save()
        survey_forms.PledgeCard(contributor, None, data=request.POST).save()
        messages.success(request, "Thanks for taking the pledge!")
    pledge_card = loader.render_to_string("commitments/_pledge_card_form.html", {"pledge_card_form": form}, RequestContext(request))
    message = loader.render_to_string("_messages.html", {}, RequestContext(request))
    return HttpResponse(json.dumps({"errors": not valid, "msg": message, "payload": pledge_card}), mimetype="text/json")
コード例 #4
0
ファイル: views.py プロジェクト: yh453926638/pgweb
def profile(request):
	# We always have the user, but not always the profile. And we need a bit
	# of a hack around the normal forms code since we have two different
	# models on a single form.
	(profile, created) = UserProfile.objects.get_or_create(pk=request.user.pk)

	# We may have a contributor record - and we only show that part of the
	# form if we have it for this user.
	try:
		contrib = Contributor.objects.get(user=request.user.pk)
	except Contributor.DoesNotExist:
		contrib = None

	contribform = None

	if request.method == 'POST':
		# Process this form
		userform = UserForm(data=request.POST, instance=request.user)
		profileform = UserProfileForm(data=request.POST, instance=profile)
		if contrib:
			contribform = ContributorForm(data=request.POST, instance=contrib)

		if userform.is_valid() and profileform.is_valid() and (not contrib or contribform.is_valid()):
			userform.save()
			profileform.save()
			if contrib:
				contribform.save()
			return HttpResponseRedirect("/account/")
	else:
		# Generate form
		userform = UserForm(instance=request.user)
		profileform = UserProfileForm(instance=profile)
		if contrib:
			contribform = ContributorForm(instance=contrib)

	return render_to_response('account/userprofileform.html', {
			'userform': userform,
			'profileform': profileform,
			'contribform': contribform,
			}, NavContext(request, "account"))
コード例 #5
0
def profile(request):
	# We always have the user, but not always the profile. And we need a bit
	# of a hack around the normal forms code since we have two different
	# models on a single form.
	(profile, created) = UserProfile.objects.get_or_create(pk=request.user.pk)

	# We may have a contributor record - and we only show that part of the
	# form if we have it for this user.
	try:
		contrib = Contributor.objects.get(user=request.user.pk)
	except Contributor.DoesNotExist:
		contrib = None

	contribform = None

	if request.method == 'POST':
		# Process this form
		userform = UserForm(data=request.POST, instance=request.user)
		profileform = UserProfileForm(data=request.POST, instance=profile)
		if contrib:
			contribform = ContributorForm(data=request.POST, instance=contrib)

		if userform.is_valid() and profileform.is_valid() and (not contrib or contribform.is_valid()):
			userform.save()
			profileform.save()
			if contrib:
				contribform.save()
			return HttpResponseRedirect("/account/")
	else:
		# Generate form
		userform = UserForm(instance=request.user)
		profileform = UserProfileForm(instance=profile)
		if contrib:
			contribform = ContributorForm(instance=contrib)

	return render_to_response('account/userprofileform.html', {
			'userform': userform,
			'profileform': profileform,
			'contribform': contribform,
			}, NavContext(request, "account"))
コード例 #6
0
def take_pledge(request):
    form = ContributorForm(data=request.POST)
    valid = form.is_valid()
    if valid:
        contributor = form.save()
        survey_forms.PledgeCard(contributor, None, data=request.POST).save()
        messages.success(request, "Thanks for taking the pledge!")
    pledge_card = loader.render_to_string("commitments/_pledge_card_form.html",
                                          {"pledge_card_form": form},
                                          RequestContext(request))
    message = loader.render_to_string("_messages.html", {},
                                      RequestContext(request))
    return HttpResponse(json.dumps({
        "errors": not valid,
        "msg": message,
        "payload": pledge_card
    }),
                        mimetype="text/json")
コード例 #7
0
def card(request, contrib_id=None, form_name=None):
    # Get the contributor object if specified
    if not contrib_id:
        contributor = Contributor()
    else:
        contributor = get_object_or_404(Contributor, pk=contrib_id)

        # Make sure the logged in user has access to view the card. User must have entered a survey for this
        # contributor, or else have the edit_any_contributor permission
        if ContributorSurvey.objects.filter(entered_by=request.user, contributor=contributor).exists() == False \
            and request.user.has_perm("contributor.edit_any_contributor") == False:
            return forbidden(
                request, "You don't have permission to edit this contributor.")

    # If the contributor has a location, get the zipcode
    # TODO: move this to the form's init
    contrib_loc = contributor.location.zipcode if contributor.location else ""

    # Setup a contrib form
    contrib_form = ContributorForm((request.POST or None),
                                   instance=contributor,
                                   initial={"zipcode": contrib_loc})

    # If a survey_form was specified, use that, otherwise use a default
    form_name = request.GET.get("form_name", str(form_name))
    try:
        survey_form = getattr(survey_forms,
                              form_name)(contributor, request.user,
                                         (request.POST or None))
    except AttributeError:
        survey_form = survey_forms.PledgeCard(contributor, request.user,
                                              (request.POST or None))

    if request.method == 'POST' and contrib_form.is_valid(
    ) and survey_form.is_valid():

        # If the contrib form finds that the email already exists, it'll return the matched contributor
        contributor = contrib_form.save()

        # Make sure the survey form has the right contributor in case one was matched on email
        survey_form.contributor = contributor
        survey_form.save()

        messages.success(
            request, "Commitment card for %s %s saved" % (
                contributor.first_name,
                contributor.last_name,
            ))

    if request.is_ajax():
        if request.method == 'POST':
            message_html = loader.render_to_string('_messages.html', {},
                                                   RequestContext(request))
            return HttpResponse(message_html)

        template = 'commitments/_card.html'
    else:
        if request.method == 'POST':
            if request.POST.get("submit") == "save_and_add_another":
                return redirect("commitments_card_create")
            else:
                return redirect("commitments_show")
        template = 'commitments/card.html'

    # Get the Surveys for the survey select widget
    survey_types = Survey.objects.filter(is_active=True)

    return render_to_response(
        template, {
            "survey_form": survey_form,
            "contrib_form": contrib_form,
            "survey_types": survey_types,
            "current_form_name": survey_form.__class__.__name__
        },
        context_instance=RequestContext(request))