def login1(request,form_class=RegistrationFormUniqueEmail,template_name='registration/registration_form.html',extra_context=None): lform = LoginForm() rform = RegistrationFormUniqueEmail() if request.method == 'POST': lform = LoginForm(data=request.POST, files=request.FILES) if lform.is_valid(): user = authenticate(username=lform.cleaned_data['login'], password=lform.cleaned_data['password']) if user is None: try: username=User.objects.get(email=lform.cleaned_data['login']).username user = authenticate(username=username, password=lform.cleaned_data['password']) except: lform.non_field_errors="Your username or password was incorrect." if user is not None: if user.is_active: login(request,user) return HttpResponseRedirect('/') if extra_context is None: extra_context = {} context = RequestContext(request) for key, value in extra_context.items(): context[key] = callable(value) and value() or value return render_to_response(template_name, {'rForm':rform,'lForm':lform,'next':next},context_instance=context )
def register(request, success_url=None, form_class=RegistrationFormUniqueEmail, profile_callback=None, template_name='registration/registration_form.html', extra_context=None): lform = LoginForm() rform = RegistrationFormUniqueEmail() context = RequestContext(request) if request.method == 'POST': rform = form_class(data=request.POST, files=request.FILES) if rform.is_valid(): new_user = rform.save(profile_callback=profile_callback) user = authenticate(username=rform.cleaned_data['username'], password=rform.cleaned_data['password1']) from django.utils import translation lang = translation.get_language_from_request(request) profile = user.get_profile() profile.lang = lang profile.save() sendEmail(user=user, type='registr', extra=[rform.cleaned_data['password1']]) login(request, user) return HttpResponseRedirect('/accounts/register/complete/') else: lform = LoginForm() return render(request, template_name=template_name, context={ 'rForm': rform, 'lForm': lform, 'next': next })
def login1(request, form_class=RegistrationFormUniqueEmail, template_name='registration/registration_form.html', extra_context=None): lform = LoginForm() rform = RegistrationFormUniqueEmail() if request.method == 'POST': lform = LoginForm(data=request.POST, files=request.FILES) if lform.is_valid(): user = authenticate(username=lform.cleaned_data['login'], password=lform.cleaned_data['password']) if user is None: try: username = User.objects.get( email=lform.cleaned_data['login']).username user = authenticate( username=username, password=lform.cleaned_data['password']) except: lform.non_field_errors = "Your username or password was incorrect." if user is not None: if user.is_active: login(request, user) return HttpResponseRedirect('/') if extra_context is None: extra_context = {} context = RequestContext(request) for key, value in extra_context.items(): context[key] = callable(value) and value() or value return render(request, template_name=template_name, context={ 'rForm': rform, 'lForm': lform, 'next': next })
def cart(request): carts=CartItem.objects.filter(cart_id=request.session['cart_id']) full_price = carts.full_price() carts=carts.order_by_shop() if request.user.is_authenticated(): lForm,rForm='','' else: lForm,rForm=LoginForm(),RegistrationFormUniqueEmail() addressForm = AddressForm() if request.method == 'POST': address= request.POST['address'] if 'address' in request.POST else 'new' addressForm = AddressForm(request.POST) lForm = LoginForm(request.POST) if not addressForm.is_valid() and address=='new': if not request.user.is_authenticated(): return render_to_response('public/cart.html',{'carts':carts,'full_price':full_price, 'rForm':rForm,'lForm':lForm,'addressForm':addressForm},context_instance=RequestContext(request)) else: return render_to_response('cart/cart.html',{'carts':carts,'full_price':full_price, 'rForm':rForm,'lForm':lForm,'addressForm':addressForm},context_instance=RequestContext(request)) # messages.info(request,'Enter address') # return HttpResponseRedirect(reverse('cart')) if lForm.is_valid(): user = authenticate(username=lForm.cleaned_data['login'], password=lForm.cleaned_data['password']) #if user try to login by email in username if user is None: username=User.objects.filter(email=lForm.cleaned_data['login']) if username: username=username[0].username user = authenticate(username=username, password=lForm.cleaned_data['password']) else: from django.forms.forms import NON_FIELD_ERRORS lForm._errors[NON_FIELD_ERRORS] = lForm.error_class(["Your username or password was incorrect."]) if user is not None and user.is_active: for shop in carts: for cart in shop.carts.all(): if cart.product.user==user: messages.error(request,_(u'Вы не можете купить товар у самого себя')) return render_to_response('public/cart.html',{'carts':carts,'full_price':full_price, 'rForm':rForm,'lForm':lForm,'addressForm':addressForm},context_instance=RequestContext(request)) login(request,user) rForm= RegistrationFormUniqueEmail(request.POST) if rForm.is_valid(): rForm.save() user = authenticate(username=rForm.cleaned_data['username'], password=rForm.cleaned_data['password1']) login(request,user) if rForm.is_valid() or lForm.is_valid() or request.user.is_authenticated(): if address=='new': address = addressForm.save() request.user.get_profile().addresses.add(address) else: address=address.replace('address_','') address=request.user.get_profile().addresses.get(id=address) shops=CartItem.objects.filter(cart_id=request.session['cart_id']).order_by_shop() for shop in shops: order=Order.objects.create(seller=shop.user,buyer = request.user, status = 'Not payed', country = address.country, region = address.region, phone = address.phone, city = address.city,street = address.street, zip_code = address.zip_code) for cart in shop.carts: cart.cart_id='' cart.save() order.carts.add(cart) request.user.get_profile().orders.add(order) UserLog.objects.create(user=order.seller, text_en=(u'Got <a href="/shop/orders/"> order #%s</a> from user %s'% (order.id,order.buyer)), text_ru=(u'Поступил <a href="/shop/orders/"> заказ #%s</a> от пользователя %s'% (order.id,order.buyer))) UserLog.objects.create(user=order.buyer, text_en=(u'Created <a href="/orders/">order #%s</a>' % order.id), text_ru=(u'Создан <a href="/orders/">заказ #%s</a>' % order.id)) SystemLog.objects.create(user=request.user,text='Create order %s' % order.id) sendEmail(user=order.seller,type='createOrder',extra={'order':order}) messages.success(request,u'Спасибо за заказ! Продавец свяжется с вами в кратчайшие сроки.') return HttpResponseRedirect(reverse('orders')) if not request.user.is_authenticated(): return render_to_response('public/cart.html',{'carts':carts,'full_price':full_price, 'rForm':rForm,'lForm':lForm,'addressForm':addressForm},context_instance=RequestContext(request)) return render_to_response('cart/cart.html',{'carts':carts,'full_price':full_price, 'rForm':rForm,'lForm':lForm,'addressForm':addressForm},context_instance=RequestContext(request))
def cart(request): carts = CartItem.objects.filter(cart_id=request.session['cart_id']) full_price = carts.full_price() carts = carts.order_by_shop() if request.user.is_authenticated(): lForm, rForm = '', '' else: lForm, rForm = LoginForm(), RegistrationFormUniqueEmail() addressForm = AddressForm() if request.method == 'POST': address = request.POST[ 'address'] if 'address' in request.POST else 'new' addressForm = AddressForm(request.POST) lForm = LoginForm(request.POST) if not addressForm.is_valid() and address == 'new': if not request.user.is_authenticated(): return render_to_response( 'public/cart.html', { 'carts': carts, 'full_price': full_price, 'rForm': rForm, 'lForm': lForm, 'addressForm': addressForm }, context_instance=RequestContext(request)) else: return render_to_response( 'cart/cart.html', { 'carts': carts, 'full_price': full_price, 'rForm': rForm, 'lForm': lForm, 'addressForm': addressForm }, context_instance=RequestContext(request)) # messages.info(request,'Enter address') # return HttpResponseRedirect(reverse('cart')) if lForm.is_valid(): user = authenticate(username=lForm.cleaned_data['login'], password=lForm.cleaned_data['password']) #if user try to login by email in username if user is None: username = User.objects.filter( email=lForm.cleaned_data['login']) if username: username = username[0].username user = authenticate( username=username, password=lForm.cleaned_data['password']) else: from django.forms.forms import NON_FIELD_ERRORS lForm._errors[NON_FIELD_ERRORS] = lForm.error_class( ["Your username or password was incorrect."]) if user is not None and user.is_active: for shop in carts: for cart in shop.carts.all(): if cart.product.user == user: messages.error( request, _(u'Вы не можете купить товар у самого себя')) return render_to_response( 'public/cart.html', { 'carts': carts, 'full_price': full_price, 'rForm': rForm, 'lForm': lForm, 'addressForm': addressForm }, context_instance=RequestContext(request)) login(request, user) rForm = RegistrationFormUniqueEmail(request.POST) if rForm.is_valid(): rForm.save() user = authenticate(username=rForm.cleaned_data['username'], password=rForm.cleaned_data['password1']) login(request, user) if rForm.is_valid() or lForm.is_valid( ) or request.user.is_authenticated(): if address == 'new': address = addressForm.save() request.user.get_profile().addresses.add(address) else: address = address.replace('address_', '') address = request.user.get_profile().addresses.get(id=address) shops = CartItem.objects.filter( cart_id=request.session['cart_id']).order_by_shop() for shop in shops: order = Order.objects.create(seller=shop.user, buyer=request.user, status='Not payed', country=address.country, region=address.region, phone=address.phone, city=address.city, street=address.street, zip_code=address.zip_code) for cart in shop.carts: cart.cart_id = '' cart.save() order.carts.add(cart) request.user.get_profile().orders.add(order) UserLog.objects.create( user=order.seller, text_en= (u'Got <a href="/shop/orders/"> order #%s</a> from user %s' % (order.id, order.buyer)), text_ru= (u'Поступил <a href="/shop/orders/"> заказ #%s</a> от пользователя %s' % (order.id, order.buyer))) UserLog.objects.create( user=order.buyer, text_en=(u'Created <a href="/orders/">order #%s</a>' % order.id), text_ru=(u'Создан <a href="/orders/">заказ #%s</a>' % order.id)) SystemLog.objects.create(user=request.user, text='Create order %s' % order.id) sendEmail(user=order.seller, type='createOrder', extra={'order': order}) messages.success( request, u'Спасибо за заказ! Продавец свяжется с вами в кратчайшие сроки.' ) return HttpResponseRedirect(reverse('orders')) if not request.user.is_authenticated(): return render_to_response('public/cart.html', { 'carts': carts, 'full_price': full_price, 'rForm': rForm, 'lForm': lForm, 'addressForm': addressForm }, context_instance=RequestContext(request)) return render_to_response('cart/cart.html', { 'carts': carts, 'full_price': full_price, 'rForm': rForm, 'lForm': lForm, 'addressForm': addressForm }, context_instance=RequestContext(request))