Exemplo n.º 1
0
def register(request):
    if request.method == 'POST':
        registration_form = NewUserForm(request.POST)
        profile_form = EditProfileForm(request.POST, request.FILES)
        try:
            if request.POST.get('password-create') == request.POST.get(
                    'password-confirm'):
                if registration_form.is_valid():
                    user = registration_form.save()

                    registration = get_object_or_404(UserProfile, user=user)
                    registration.address1 = request.POST.get('address1', None)
                    registration.phone = request.POST.get('phone', None)
                    registration.phone2 = request.POST.get('phone2', None)
                    registration.address2 = request.POST.get('address2', None)
                    registration.city = request.POST.get('city', None)
                    registration.state = request.POST.get('state', None)
                    registration.zipcode = request.POST.get('zipcode', None)
                    registration.bio = request.POST.get('bio', None)
                    registration.gender = request.POST.get('gender', None)
                    # TODO needs to be an instance of these three?
                    # registration.howHeard = request.POST.get('howHeard', None)
                    # registration.industry = request.POST.get('industry', None)
                    # registration.neighborhood = request.POST.get('neighborhood', None)
                    registration.has_kids = request.POST.get('has_kids', None)
                    registration.self_employed = request.POST.get(
                        'self_employed', None)
                    registration.public_profile = request.POST.get(
                        'public_profile', False)
                    registration.photo = request.FILES.get('photo', None)

                    registration.save()

                    pwd = request.POST.get('password-create')
                    u = User.objects.get(username=user.username)
                    u.set_password(pwd)
                    u.save()

                    return HttpResponseRedirect(
                        reverse('member:profile:view',
                                kwargs={'username': user.username}))
            else:
                messages.error(
                    request,
                    'The entered passwords do not match. Please try again.')
        except Exception as e:
            logger.error(str(e))
    else:
        registration_form = NewUserForm()
        profile_form = EditProfileForm()

    context = {
        'registration_form': registration_form,
        'ALLOW_PHOTO_UPLOAD': settings.ALLOW_PHOTO_UPLOAD,
        'settings': settings,
        'profile_form': profile_form
    }
    return render(request, 'member/core/register.html', context)
Exemplo n.º 2
0
def edit_profile(request, username):
    page_message = None
    user = get_object_or_404(User, username=username)
    if not user == request.user:
        if not request.user.is_staff:
            return HttpResponseRedirect(reverse('member_profile', kwargs={'username': request.user.username}))

    if request.method == 'POST':
        profile_form = EditProfileForm(request.POST)
        profile_form.public_profile = request.POST['public_profile']

        if profile_form.is_valid():
            if request.POST.get('password-create') == request.POST.get('password-confirm'):
                pwd = request.POST.get('password-create')

                if len(pwd.strip()) > 0:
                    if pwd.strip() == pwd and len(pwd) > 7:

                        profile_form.save()
                        user.set_password(pwd)
                        user.save()

                        return HttpResponseRedirect(reverse('member_profile', kwargs={'username': user.username}))
                    else:
                        page_message = 'Your password must be at least 8 characters long.'
                else:
                    profile_form.save()

                    return HttpResponseRedirect(reverse('member_profile', kwargs={'username': user.username}))
            else:
                page_message = 'The entered passwords do not match. Please try again.'
    else:
        profile = user.profile
        emergency_contact = user.get_emergency_contact()
        profile_form = EditProfileForm(initial={'username': user.username, 'first_name': user.first_name, 'last_name': user.last_name, 'email': user.email,
                                                'phone': profile.phone, 'phone2': profile.phone2,
                                                'address1': profile.address1, 'address2': profile.address2, 'city': profile.city, 'state': profile.state, 'zipcode': profile.zipcode,
                                                'url_personal': profile.url_personal, 'url_professional': profile.url_professional,
                                                'url_facebook': profile.url_facebook, 'url_twitter': profile.url_twitter,
                                                'url_linkedin': profile.url_linkedin, 'url_github': profile.url_github,
                                                'bio': profile.bio, 'photo': profile.photo,
                                                'public_profile': profile.public_profile,
                                                'gender': profile.gender, 'howHeard': profile.howHeard, 'industry': profile.industry, 'neighborhood': profile.neighborhood,
                                                'has_kids': profile.has_kids, 'self_employed': profile.self_employed,
                                                'emergency_name': emergency_contact.name, 'emergency_relationship': emergency_contact.relationship,
                                                'emergency_phone': emergency_contact.phone, 'emergency_email': emergency_contact.email,
                                            })

    context = {'user': user, 'profile_form': profile_form, 'page_message': page_message}
    return render(request, 'members/profile_edit.html', context)
Exemplo n.º 3
0
def edit_pic(request, username):
    user = get_object_or_404(User, username=username)
    if not user == request.user and not request.user.is_staff:
        return HttpResponseRedirect(
            reverse('member:profile:view',
                    kwargs={'username': request.user.username}))
    if request.method == 'POST':
        profile_form = EditProfileForm(request.POST, request.FILES)
        profile = get_object_or_404(UserProfile, user=user)
        profile.photo = request.FILES.get('photo', None)

        profile.save()

        return HttpResponseRedirect(
            reverse('member:profile:view', kwargs={'username': user.username}))
    else:
        profile_form = EditProfileForm()

    ALLOW_PHOTO_UPLOAD = settings.ALLOW_PHOTO_UPLOAD
    if request.user.is_staff:
        ALLOW_PHOTO_UPLOAD = True

    context = {'ALLOW_PHOTO_UPLOAD': ALLOW_PHOTO_UPLOAD, 'user': user}
    return render(request, 'members/profile/edit_pic.html', context)
Exemplo n.º 4
0
def edit_profile(request, username):
    user = get_object_or_404(User, username=username)
    if not user == request.user:
        if not request.user.is_staff:
            return HttpResponseRedirect(reverse('member:profile:view', kwargs={'username': request.user.username}))

    LinkFormSet = formset_factory(LinkForm, formset=BaseLinkFormSet)

    user_links = user.profile.websites.all()
    link_data = [{'url_type': l.url_type, 'url': l.url, 'username': user.username} for l in user_links]

    if request.method == 'POST':
        profile_form = EditProfileForm(request.POST)
        link_formset = LinkFormSet(request.POST)
        profile_form.public_profile = request.POST['public_profile']

        if profile_form.is_valid():
            if link_formset.is_valid():
                if request.POST.get('password-create') == request.POST.get('password-confirm'):
                    pwd = request.POST.get('password-create')

                    if len(pwd.strip()) > 0:
                        if pwd.strip() == pwd and len(pwd) > 7:

                            profile_form.save()
                            user.set_password(pwd)
                            user.save()

                            return HttpResponseRedirect(reverse('member:profile:view', kwargs={'username': user.username}))
                        else:
                            messages.error(request, _('Your password must be at least 8 characters long.'))
                    else:
                        for link in link_data:
                            del_url = link.get('url')
                            user.profile.websites.filter(url=del_url).delete()

                        for link_form in link_formset:
                            if not link_form.cleaned_data.get('username'):
                                link_form.cleaned_data['username'] = user.username
                            try:
                                if link_form.is_valid():
                                    url_type = link_form.cleaned_data.get('url_type')
                                    url = link_form.cleaned_data.get('url')
                                    if url_type and url:
                                        link_form.save()
                            except Exception as e:
                                messages.add_message(request, messages.ERROR, _("Could not save: %s") % str(e))
                        profile_form.save()

                        return HttpResponseRedirect(reverse('member:profile:view', kwargs={'username': user.username}))
                else:
                    messages.error(request, _('The entered passwords do not match. Please try again.'))
            else:
                messages.error(request, _('There was an error saving your websites. Please make sure they have a valid URL and URL type.'))
    else:
        link_formset = LinkFormSet(initial=link_data)
        profile = user.profile
        emergency_contact = user.get_emergency_contact()
        profile_form = EditProfileForm(initial={'username': user.username,
                                                'first_name': user.first_name,
                                                'last_name': user.last_name,
                                                'email': user.email,
                                                'phone': profile.phone, 'phone2': profile.phone2,
                                                'address1': profile.address1, 'address2': profile.address2, 'city': profile.city, 'state': profile.state, 'zipcode': profile.zipcode,
                                                'url_personal': profile.url_personal, 'url_professional': profile.url_professional,
                                                'url_facebook': profile.url_facebook, 'url_twitter': profile.url_twitter,
                                                'url_linkedin': profile.url_linkedin, 'url_github': profile.url_github,
                                                'bio': profile.bio, 'photo': profile.photo,
                                                'public_profile': profile.public_profile,
                                                'gender': profile.gender, 'pronouns': profile.pronouns,
                                                'howHeard': profile.howHeard, 'industry': profile.industry, 'neighborhood': profile.neighborhood,
                                                'has_kids': profile.has_kids, 'self_employed': profile.self_employed,
                                                'emergency_name': emergency_contact.name, 'emergency_relationship': emergency_contact.relationship,
                                                'emergency_phone': emergency_contact.phone, 'emergency_email': emergency_contact.email,
                                                })

    context = {'user': user, 'profile_form': profile_form, 'link_formset': link_formset}
    return render(request, 'member/profile/profile_edit.html', context)
Exemplo n.º 5
0
def edit_profile(request, username):
    user = get_object_or_404(User, username=username)
    if not user == request.user:
        if not request.user.is_staff:
            return HttpResponseRedirect(reverse('member:profile:view', kwargs={'username': request.user.username}))

    LinkFormSet = formset_factory(LinkForm, formset=BaseLinkFormSet)

    user_links = user.profile.websites.all()
    link_data = [{'url_type': l.url_type, 'url': l.url, 'username': user.username} for l in user_links]

    if request.method == 'POST':
        profile_form = EditProfileForm(request.POST)
        link_formset = LinkFormSet(request.POST)
        profile_form.public_profile = request.POST['public_profile']

        if profile_form.is_valid():
            if link_formset.is_valid():
                if request.POST.get('password-create') == request.POST.get('password-confirm'):
                    pwd = request.POST.get('password-create')

                    if len(pwd.strip()) > 0:
                        if pwd.strip() == pwd and len(pwd) > 7:

                            profile_form.save()
                            user.set_password(pwd)
                            user.save()

                            return HttpResponseRedirect(reverse('member:profile:view', kwargs={'username': user.username}))
                        else:
                            messages.error(request, 'Your password must be at least 8 characters long.')
                    else:
                        for link in link_data:
                            del_url = link.get('url')
                            user.profile.websites.filter(url=del_url).delete()

                        for link_form in link_formset:
                            if not link_form.cleaned_data.get('username'):
                                link_form.cleaned_data['username'] = user.username
                            try:
                                if link_form.is_valid():
                                    url_type = link_form.cleaned_data.get('url_type')
                                    url = link_form.cleaned_data.get('url')
                                    if url_type and url:
                                        link_form.save()
                            except Exception as e:
                                messages.add_message(request, messages.ERROR, "Could not save: %s" % str(e))
                        profile_form.save()

                        return HttpResponseRedirect(reverse('member:profile:view', kwargs={'username': user.username}))
                else:
                    messages.error(request,'The entered passwords do not match. Please try again.')
            else:
                messages.error(request, 'There was an error saving your websites. Please make sure they have a valid URL and URL type.')
    else:
        link_formset = LinkFormSet(initial=link_data)
        profile = user.profile
        emergency_contact = user.get_emergency_contact()
        profile_form = EditProfileForm(initial={'username': user.username,
                                                'first_name': user.first_name,
                                                'last_name': user.last_name,
                                                'email': user.email,
                                                'phone': profile.phone, 'phone2': profile.phone2,
                                                'address1': profile.address1, 'address2': profile.address2, 'city': profile.city, 'state': profile.state, 'zipcode': profile.zipcode,
                                                'url_personal': profile.url_personal, 'url_professional': profile.url_professional,
                                                'url_facebook': profile.url_facebook, 'url_twitter': profile.url_twitter,
                                                'url_linkedin': profile.url_linkedin, 'url_github': profile.url_github,
                                                'bio': profile.bio, 'photo': profile.photo,
                                                'public_profile': profile.public_profile,
                                                'gender': profile.gender, 'howHeard': profile.howHeard, 'industry': profile.industry, 'neighborhood': profile.neighborhood,
                                                'has_kids': profile.has_kids, 'self_employed': profile.self_employed,
                                                'emergency_name': emergency_contact.name, 'emergency_relationship': emergency_contact.relationship,
                                                'emergency_phone': emergency_contact.phone, 'emergency_email': emergency_contact.email,
                                                })

    context = {'user': user, 'profile_form': profile_form, 'link_formset': link_formset}
    return render(request, 'member/profile/profile_edit.html', context)