Ejemplo n.º 1
0
def locator(request):
    """
    Renders and processes user location chooser form.
    Can be used to set either current session location, or default home
    location.
    When changing home location, existing location is updated in DB,
    so any posts using that location get their location modified as well.
    """
    profile = request.profile
    hide_set_home = profile is None
    get_browser_location = True
    # Work on a copy of profile location, since it is modified by
    # form validation.
    instance = profile and profile.location and profile.location.clone()
    # Don't overwrite old location -- old posts, etc., still need it.
    if instance:
        instance.id = None
    if request.method == 'POST':
        form = LocationForm(request.POST,
                            instance=instance,
                            hide_set_home=hide_set_home)
        if form.is_valid():
            if 'clear' in request.POST:
                Location.clear_session(request)
                return redirect_after_locator(request)
            save = (profile and (profile.location is None
                                 or form.cleaned_data.get('set_home', False)))
            location = form.save(commit=save)
            if save:
                profile.location = location
                profile.set_updated()
                profile.save()
                Location.clear_session(request)
            else:
                location.to_session(request)
            messages.info(request, MESSAGES['location_set'])
            return redirect_after_locator(request)
        else:
            get_browser_location = False

            # TODO: Set initial_lat, initial_lng for map redisplay.
            # Don't reset text fields in form though...

    else:
        form = LocationForm(instance=instance, hide_set_home=hide_set_home)
        if request.location:

            # TODO: Don't overwrite initial form data with geocoding javascript.

            initial_lng, initial_lat = request.location.point.tuple
        else:
            # Do an IP geocode as a rough first guess.
            initial_lat, initial_lng = get_geoip_coords(request)
            if initial_lat == '' or initial_lng == '':
                initial_lat, initial_lng = settings.DEFAULT_LOCATION
    return locals()
Ejemplo n.º 2
0
def locator(request):
    """
    Renders and processes user location chooser form.
    Can be used to set either current session location, or default home
    location.
    When changing home location, existing location is updated in DB,
    so any posts using that location get their location modified as well.
    """
    profile = request.profile
    hide_set_home = profile is None
    get_browser_location = True
    # Work on a copy of profile location, since it is modified by
    # form validation.
    instance = profile and profile.location and profile.location.clone()
    # Don't overwrite old location -- old posts, etc., still need it.
    if instance:
        instance.id = None
    if request.method == 'POST':
        form = LocationForm(
            request.POST, instance=instance, hide_set_home=hide_set_home)
        if form.is_valid():
            if 'clear' in request.POST:
                Location.clear_session(request)
                return redirect_after_locator(request)
            save = (profile and (profile.location is None or
                                 form.cleaned_data.get('set_home', False)))
            location = form.save(commit=save)
            if save:
                profile.location = location
                profile.set_updated()
                profile.save()
                Location.clear_session(request)
            else:
                location.to_session(request)
            messages.info(request, MESSAGES['location_set'])
            return redirect_after_locator(request)
        else:
            get_browser_location = False

            # TODO: Set initial_lat, initial_lng for map redisplay.
            # Don't reset text fields in form though...

    else:
        form = LocationForm(instance=instance, hide_set_home=hide_set_home)
        if request.location:

            # TODO: Don't overwrite initial form data with geocoding javascript.
            
            initial_lng, initial_lat = request.location.point.tuple
        else:
            # Do an IP geocode as a rough first guess.
            initial_lat, initial_lng = get_geoip_coords(request)
            if initial_lat == '' or initial_lng == '':
                initial_lat, initial_lng = settings.DEFAULT_LOCATION
    return locals()
Ejemplo n.º 3
0
 def process_request(self, request):
     """
     Set request.location to the current session location if it is
     present, to the profile location if it is not.
     """
     location = Location.from_session(request)
     if not location and request.profile:
         location = request.profile.location
     request.location = location
Ejemplo n.º 4
0
 def process_request(self, request):
     """
     Set request.location to the current session location if it is
     present, to the profile location if it is not.
     """
     location = Location.from_session(request)
     if not location and request.profile:
         location = request.profile.location
     request.location = location
Ejemplo n.º 5
0
def register(request):
    """
    Registration form.
    """
    invitation = get_invitation(request)
    if settings.INVITATION_ONLY and not invitation:
        raise PermissionDenied
    if request.method == 'POST':
        form = RegistrationForm(request.POST)
        if form.is_valid():
            profile = form.save(request.location, request.LANGUAGE_CODE)
            if invitation:
                # Turn invitation into endorsement.
                Endorsement.objects.create(
                    endorser=invitation.from_profile,
                    recipient=profile,
                    weight=invitation.endorsement_weight,
                    text=invitation.endorsement_text)
                send_invitation_accepted_email(invitation, profile)
                invitation.delete()
            else:
                # Let sharer know someone registered through their link.
                send_shared_link_registration_email(request, profile)
            # Auto login.
            user = authenticate(username=form.username, password=form.password)
            django_login(request, user)
            Location.clear_session(request)  # Location is in profile now.
            # Notifications.
            send_registration_email(profile)
            messages.info(request, MESSAGES['registration_done'])
            return redirect(edit_profile)
    else:
        initial = {}
        if invitation:
            initial['email'] = invitation.to_email
        form = RegistrationForm(initial=initial)
    return locals()
Ejemplo n.º 6
0
def register(request):
    """
    Registration form.
    """
    invitation = get_invitation(request)
    if settings.INVITATION_ONLY and not invitation:
        raise PermissionDenied    
    if request.method == 'POST':
        form = RegistrationForm(request.POST)
        if form.is_valid():
            profile = form.save(request.location)
            if invitation:
                # Turn invitation into endorsement.
                Endorsement.objects.create(
                    endorser = invitation.from_profile,
                    recipient = profile,
                    weight = invitation.endorsement_weight,
                    text = invitation.endorsement_text)
                send_invitation_accepted_email(invitation, profile)
                invitation.delete()
            else:
                # Let sharer know someone registered through their link.
                send_shared_link_registration_email(request, profile)
            # Auto login.
            user = authenticate(username=form.username, password=form.password)
            django_login(request, user)
            Location.clear_session(request)  # Location is in profile now.
            # Notifications.
            send_registration_email(profile)
            messages.info(request, MESSAGES['registration_done'])            
            return redirect(edit_profile)
    else:
        initial = {}
        if invitation:
            initial['email'] = invitation.to_email
        form = RegistrationForm(initial=initial)
    return locals()