def edit_finding(request, username):
    person = get_object_or_404(DjangoPerson, user__username = username)
    if request.method == 'POST':
        form = FindingForm(request.POST, person=person)
        if form.is_valid():
            user = person.user
            user.email = form.cleaned_data['email']
            user.save()
            
            person.machinetags.filter(namespace = 'profile').delete()
            if form.cleaned_data['blog']:
                person.add_machinetag(
                    'profile', 'blog', form.cleaned_data['blog']
                )
            if form.cleaned_data['looking_for_work']:
                person.add_machinetag(
                    'profile', 'looking_for_work',
                    form.cleaned_data['looking_for_work']
                )
            
            for fieldname, (namespace, predicate) in \
                MACHINETAGS_FROM_FIELDS.items():
                person.machinetags.filter(
                    namespace = namespace, predicate = predicate
                ).delete()
                if form.cleaned_data.has_key(fieldname) and \
                    form.cleaned_data[fieldname].strip():
                    value = form.cleaned_data[fieldname].strip()
                    person.add_machinetag(namespace, predicate, value)
            
            return redirect(reverse('user_profile', args=[username]))
    else:
        mtags = tagdict(person.machinetags.all())
        initial = {
            'email': person.user.email,
            'blog': mtags['profile']['blog'],
            'looking_for_work': mtags['profile']['looking_for_work'],
        }
        
        # Fill in other initial fields from machinetags
        for fieldname, (namespace, predicate) in \
                MACHINETAGS_FROM_FIELDS.items():
            initial[fieldname] = mtags[namespace][predicate]
        
        form = FindingForm(initial=initial, person=person)
    return render(request, 'edit_finding.html', {
        'form': form,
        'person': person,
    })
Exemple #2
0
def edit_finding(request, username):
    person = get_object_or_404(DjangoPerson, user__username=username)
    if request.method == "POST":
        form = FindingForm(request.POST, person=person)
        if form.is_valid():
            user = person.user
            user.email = form.cleaned_data["email"]
            user.save()

            person.machinetags.filter(namespace="profile").delete()
            if form.cleaned_data["blog"]:
                person.add_machinetag("profile", "blog", form.cleaned_data["blog"])
            if form.cleaned_data["looking_for_work"]:
                person.add_machinetag("profile", "looking_for_work", form.cleaned_data["looking_for_work"])

            for fieldname, (namespace, predicate) in MACHINETAGS_FROM_FIELDS.items():
                person.machinetags.filter(namespace=namespace, predicate=predicate).delete()
                if form.cleaned_data.has_key(fieldname) and form.cleaned_data[fieldname].strip():
                    value = form.cleaned_data[fieldname].strip()
                    person.add_machinetag(namespace, predicate, value)

            return HttpResponseRedirect("/%s/" % username)
    else:
        mtags = tagdict(person.machinetags.all())
        initial = {
            "email": person.user.email,
            "blog": mtags["profile"]["blog"],
            "looking_for_work": mtags["profile"]["looking_for_work"],
        }

        # Fill in other initial fields from machinetags
        for fieldname, (namespace, predicate) in MACHINETAGS_FROM_FIELDS.items():
            initial[fieldname] = mtags[namespace][predicate]

        form = FindingForm(initial=initial, person=person)
    return render(request, "edit_finding.html", {"form": form, "person": person})
def signup(request):
    if not request.user.is_anonymous():
        return redirect(reverse('index'))
    if request.method == 'POST':
        if request.openid:
            form = SignupForm(
                request.POST, request.FILES, openid=request.openid
            )
        else:
            form = SignupForm(request.POST, request.FILES)
        if form.is_valid():
            # First create the user
            creation_args = {
                'username': form.cleaned_data['username'],
                'email': form.cleaned_data['email'],
            }
            if form.cleaned_data.get('password1'):
                creation_args['password'] = form.cleaned_data['password1']

            user = User.objects.create_user(**creation_args)
            user.first_name = form.cleaned_data['first_name']
            user.last_name = form.cleaned_data['last_name']
            user.save()

            if request.openid:
                associate_openid(user, str(request.openid))

            region = None
            if form.cleaned_data['region']:
                region = Region.objects.get(
                    country__iso_code = form.cleaned_data['country'],
                    code = form.cleaned_data['region']
                )

            # Now create the DjangoPerson
            person = DjangoPerson.objects.create(
                user = user,
                bio = form.cleaned_data['bio'],
                country = Country.objects.get(
                    iso_code = form.cleaned_data['country']
                ),
                region = region,
                latitude = form.cleaned_data['latitude'],
                longitude = form.cleaned_data['longitude'],
                location_description = form.cleaned_data['location_description']
            )

            # Set up the various machine tags
            for fieldname, (namespace, predicate) in \
                    MACHINETAGS_FROM_FIELDS.items():
                if form.cleaned_data.has_key(fieldname) and \
                    form.cleaned_data[fieldname].strip():
                    value = form.cleaned_data[fieldname].strip()
                    person.add_machinetag(namespace, predicate, value)

            # Stash their blog and looking_for_work
            if form.cleaned_data['blog']:
                person.add_machinetag(
                    'profile', 'blog', form.cleaned_data['blog']
                )
            if form.cleaned_data['looking_for_work']:
                person.add_machinetag(
                    'profile', 'looking_for_work',
                    form.cleaned_data['looking_for_work']
                )

            # Finally, set their skill tags
            person.skilltags = form.cleaned_data['skilltags']

            # Log them in and redirect to their profile page
            # HACK! http://groups.google.com/group/django-users/
            #    browse_thread/thread/39488db1864c595f
            user.backend='django.contrib.auth.backends.ModelBackend'
            auth.login(request, user)
            return redirect(person.get_absolute_url())
    else:
        if request.openid and request.openid.sreg:
            sreg = request.openid.sreg
            first_name = ''
            last_name = ''
            username = ''
            if sreg.get('fullname'):
                bits = sreg['fullname'].split()
                first_name = bits[0]
                if len(bits) > 1:
                    last_name = ' '.join(bits[1:])
            # Find a not-taken username
            if sreg.get('nickname'):
                username = derive_username(sreg['nickname'])
            form = SignupForm(initial = {
                'first_name': first_name,
                'last_name': last_name,
                'email': sreg.get('email', ''),
                'username': username,
            }, openid = request.openid)
        elif request.openid:
            form = SignupForm(openid = request.openid)
        else:
            form = SignupForm()
    
    return render(request, 'signup.html', {
        'form': form,
        'openid': request.openid,
    })
Exemple #4
0
def signup(request):
    if not request.user.is_anonymous():
        return HttpResponseRedirect("/")
    if request.method == "POST":
        if request.openid:
            form = SignupForm(request.POST, request.FILES, openid=request.openid)
        else:
            form = SignupForm(request.POST, request.FILES)
        if form.is_valid():
            # First create the user
            creation_args = {"username": form.cleaned_data["username"], "email": form.cleaned_data["email"]}
            if form.cleaned_data.get("password1"):
                creation_args["password"] = form.cleaned_data["password1"]

            user = User.objects.create_user(**creation_args)
            user.first_name = form.cleaned_data["first_name"]
            user.last_name = form.cleaned_data["last_name"]
            user.save()

            if request.openid:
                associate_openid(user, str(request.openid))

            region = None
            if form.cleaned_data["region"]:
                region = Region.objects.get(
                    country__iso_code=form.cleaned_data["country"], code=form.cleaned_data["region"]
                )

            # Now create the DjangoPerson
            person = DjangoPerson.objects.create(
                user=user,
                bio=form.cleaned_data["bio"],
                country=Country.objects.get(iso_code=form.cleaned_data["country"]),
                region=region,
                latitude=form.cleaned_data["latitude"],
                longitude=form.cleaned_data["longitude"],
                location_description=form.cleaned_data["location_description"],
            )

            # Set up the various machine tags
            for fieldname, (namespace, predicate) in MACHINETAGS_FROM_FIELDS.items():
                if form.cleaned_data.has_key(fieldname) and form.cleaned_data[fieldname].strip():
                    value = form.cleaned_data[fieldname].strip()
                    person.add_machinetag(namespace, predicate, value)

            # Stash their blog and looking_for_work
            if form.cleaned_data["blog"]:
                person.add_machinetag("profile", "blog", form.cleaned_data["blog"])
            if form.cleaned_data["looking_for_work"]:
                person.add_machinetag("profile", "looking_for_work", form.cleaned_data["looking_for_work"])

            # Finally, set their skill tags
            person.skilltags = form.cleaned_data["skilltags"]

            # Log them in and redirect to their profile page
            # HACK! http://groups.google.com/group/django-users/
            #    browse_thread/thread/39488db1864c595f
            user.backend = "django.contrib.auth.backends.ModelBackend"
            auth.login(request, user)
            return HttpResponseRedirect(person.get_absolute_url())
    else:
        if request.openid and request.openid.sreg:
            sreg = request.openid.sreg
            first_name = ""
            last_name = ""
            username = ""
            if sreg.get("fullname"):
                bits = sreg["fullname"].split()
                first_name = bits[0]
                if len(bits) > 1:
                    last_name = " ".join(bits[1:])
            # Find a not-taken username
            if sreg.get("nickname"):
                username = derive_username(sreg["nickname"])
            form = SignupForm(
                initial={
                    "first_name": first_name,
                    "last_name": last_name,
                    "email": sreg.get("email", ""),
                    "username": username,
                },
                openid=request.openid,
            )
        elif request.openid:
            form = SignupForm(openid=request.openid)
        else:
            form = SignupForm()

    return render(
        request, "signup.html", {"form": form, "api_key": settings.GOOGLE_MAPS_API_KEY, "openid": request.openid}
    )