Ejemplo n.º 1
0
def signup(request):
    if request.method == 'POST':
        user_form = UserForm(request.POST)
        profile_form = ProfileForm(request.POST)
        print('40 = ', profile_form.errors.as_data())
        print('41 = ', user_form.errors.as_data())
        if user_form.is_valid() and profile_form.is_valid():
            user_form.save()
            profile_form.save()
            print('test')
            return render(request, 'home/home.html')
    else:
        user_form = UserForm()
        profile_form = ProfileForm
    context = {'user_form': user_form, 'profile_form': profile_form}
    template = 'home/signup.html'
    return render(request, template, context)
Ejemplo n.º 2
0
def saveInfo(request):
    if request.user.is_authenticated:
        profile = get_object_or_404(Profile, user=request.user)
        if request.method == "POST":
            try:
                form = ProfileForm(request.POST)
                print(request.POST)
                if form.is_valid():
                    profile.delete()
                    newprofile = form.save(commit=False)
                    newprofile.user = request.user
                    if request.POST.getlist(
                            'dost') or request.POST.getlist('osob') != []:
                        newprofile.dopinfo = True
                    newprofile.save()
                    if request.POST.getlist(
                            'dost') or request.POST.getlist('osob') != []:
                        for d in request.POST.getlist('dost'):
                            newdost = DostizhDocument()
                            newdost.type = d
                            newdost.profile = newprofile
                            newdost.save()

                        for d in request.POST.getlist('osob'):
                            newdost = OsobDocument()
                            newdost.type = d
                            newdost.profile = newprofile
                            newdost.save()

                else:
                    print(form.errors)
            except:
                return render(
                    request, 'error.html',
                    {'content': "Произошла ошибка при сохранении данных"})

        return redirect('index')

    else:
        return redirect('log')
Ejemplo n.º 3
0
def index(request):
    if request.user.is_authenticated:
        profile = get_object_or_404(Profile, user=request.user)
        if profile.role == 2:
            return redirect('adminpanel', slug='all')
        status = 0
        if profile.checkAnket():
            status = 1
        # if profile.dopinfo==True:
        #     status=2
        if profile.checkZayavl():
            status = 3
        print(profile.checkAnket())
        print(profile.dopinfo)
        dopinfochek = True
        if profile.dopinfo:
            osob = OsobDocument.objects.filter(profile=profile)
            for o in osob:
                if not o.doc:
                    dopinfochek = False
                    break
            dost = DostizhDocument.objects.filter(profile=profile)
            for o in dost:
                if not o.doc:
                    dopinfochek = False
                    break

        print(profile.checkZayavl())
        form = ProfileForm(instance=profile)
        zayavlForm = ZayavlForm(instance=profile)
        OsobFormSet = modelformset_factory(OsobDocument,
                                           form=OsobDocumentForm,
                                           extra=0)
        DostFormSet = modelformset_factory(DostizhDocument,
                                           form=DostizhDocumentForm,
                                           extra=0)
        formset1 = OsobFormSet(queryset=OsobDocument.objects.filter(
            profile=profile))
        formset0 = DostFormSet(queryset=DostizhDocument.objects.filter(
            profile=profile))
        return render(
            request, 'home.html', {
                'form': form,
                'formset0': formset0,
                'formset1': formset1,
                'zayavlForm': zayavlForm,
                'status': status,
                'profile': profile,
                'dopinfochek': dopinfochek
            })
    else:
        return redirect('log')
Ejemplo n.º 4
0
def checkout_success(request, order_id):
    save_info = request.session.get('save_info')
    order_id = get_object_or_404(Order, order_number=order_id)
    order_items = OrderItem.objects.filter(order=order_id)
    extra_title = "- Order placed!"

    if request.user.is_authenticated:
        profile = UserProfile.objects.get(user_id=request.user)
        # Attach the user's profile to the order
        order_id.user_id = profile
        order_id.save()
        del request.session['bag']
        # Save the user's info, if the checkbox is ticked in checkout
        if save_info:
            profile_data = {
                'default_phone_number': order_id.phone_number,
                'default_country': order_id.country,
                'default_postcode': order_id.postcode,
                'default_town_or_city': order_id.town_or_city,
                'default_street_address1': order_id.street_address1,
                'default_street_address2': order_id.street_address2,
                'default_county': order_id.county,
            }
            user_profile_form = ProfileForm(profile_data, instance=profile)
            if user_profile_form.is_valid():
                user_profile_form.save()

    try:
        del request.session['bag']
    except KeyError:
        pass

    context = {
        "order": order_id,
        "order_items": order_items,
        'extra_title': extra_title
    }

    return render(request, 'checkout_success.html', context)
Ejemplo n.º 5
0
def update_profile(request):
    if request.method == 'POST':
        profile_form = ProfileForm(request.POST,
                                   instance=request.user.userprofile)
        if profile_form.is_valid():
            profile_form.save()
            messages.success(request,
                             ('Your profile was successfully updated!'))
            return redirect('home:profile')
        else:
            messages.error(request, ('Please correct the error below.'))
    else:
        profile_form = ProfileForm(instance=request.user.userprofile)
    return render(request, 'home/profile.html', {'profile_form': profile_form})
Ejemplo n.º 6
0
def update_profile(request):
    if request.method == "POST":
        user_form = UserForm(request.POST, instance=request.user)
        profile_form = ProfileForm(request.POST, instance=request.user.profile)
        if user_form.is_valid() and profile_form.is_valid():
            user_form.save()
            profile_form.save()
            return HttpResponseRedirect('/')
        else:
            messages.error(request, _('Please correct the error below.'))
    else:
        user_form = UserForm(instance=request.user)
        profile_form = ProfileForm(instance=request.user.profile)
    return render(request, 'home/profile.html', {
        'user_form': user_form,
        'profile_form': profile_form
    })
Ejemplo n.º 7
0
def update_profile(request):
    if request.method == 'POST':
        user_form = UserForm(request.POST, instance=request.user)
        profile_form = ProfileForm(request.POST, instance=request.user.profile)
        if user_form.is_valid() and profile_form.is_valid():
            user_form.save()
            profile_form.save()
            messages.success(request, 'Your profile was succesfully updated!')
            return redirect('/profile/')
        else:
            messages.error(request, 'Please correct the error bellow')
    else:
        user_form = UserForm(instance=request.user)
        profile_form = ProfileForm(instance=request.user.profile)
    context = {
        'user_form': user_form,
        'profile_form': profile_form
    }
    return render(request, 'home/update_profile.html', context)

    
Ejemplo n.º 8
0
def profile(request):
    """
    Страница профиля

    :param request: объект c деталями запроса
    :type request: :class:`django.http.HttpRequest`
    :return: перенаправление на страницу профиля
    :return: перенаправление на страницу 404 при ошибке
    """
    context = get_base_context()
    if request.method == "POST":
        profile_form = ProfileForm(request.POST)
        if profile_form.is_valid():
            users_email = User.objects.filter(email=profile_form.data["email"])
            if users_email and profile_form.data['email'] != users_email[
                    0].email:
                messages.error(request,
                               "Пользователь с таким email уже существует.")
                context["form"] = ProfileForm(
                    data={
                        'username': profile_form.data['username'],
                        'last_name': profile_form.data['last_name'],
                        'first_name': profile_form.data['first_name'],
                        'email': profile_form.data['email']
                    })
                return render(request, "register.html", context)
            else:
                user = User.objects.get(username=request.user)
                user.username = username = str(profile_form.data["username"])
                user.last_name = str(profile_form.data["last_name"])
                user.first_name = str(profile_form.data["first_name"])
                user.email = str(profile_form.data["email"])
                print(profile_form.data['password'])
                if profile_form.data['password'] != "":
                    user.set_password(profile_form.cleaned_data["password"])
                user.save()
                if user is not None:
                    login(request,
                          user,
                          backend='django.contrib.auth.backends.ModelBackend')
                return redirect("/profile/")
        else:
            users = User.objects.filter(username=profile_form.data["username"])
            if users and users[0].username != profile_form.data['username']:
                messages.error(request,
                               "Пользователь с таким именем уже существует.")
            elif profile_form.data["password"] != profile_form.data[
                    "password2"]:
                messages.error(request, "Пароли не совпадают.")
            else:
                messages.error(request, "Неверный email.")
            context["form"] = ProfileForm(
                data={
                    'username': profile_form.data['username'],
                    'last_name': profile_form.data['last_name'],
                    'first_name': profile_form.data['first_name'],
                    'email': profile_form.data['email']
                })
        return redirect('/profile/')
    else:
        try:
            user = User.objects.get(username=request.user)
            data = {
                'username': user.username,
                'last_name': user.last_name,
                'first_name': user.first_name,
                'email': user.email
            }
            context['form'] = ProfileForm(data)
            return render(request, "profile.html", context)
        except User.DoesNotExist:
            return handler404(request)