def post(self, request, **kwargs): form = ProfileForm(request.POST or None,instance=self.us) form1 = AddressForm(data=request.POST,prefix="a",instance=self.add) form2=ReligionForm(data=request.POST,prefix="b",instance=self.rel) a_valid = form.is_valid() b_valid = form1.is_valid() c_valid = form2.is_valid() if a_valid and b_valid and c_valid: return self.form_valid(request,form,form1, form2) else: return self.get(request)
def _get_addresses_forms(self, customer): # # temporary crutches to not rewrite all # existing code based on one address. # base_address = { 'name': 'Base address', 'recipient_name': customer.get_full_name(), 'id': -1, 'is_primary': True, 'country': customer.country, 'line1': customer.line1, 'line2': customer.line2, 'postcode': customer.postcode, } additional_addresses = customer.addresses.all() if [True for address in additional_addresses if address.is_primary]: base_address['is_primary'] = False addresses_forms = [AddressForm(initial=base_address)] for addr in additional_addresses: addresses_forms.append(AddressForm(instance=addr)) return addresses_forms
def checkout(request, template_name="shop/checkout.html"): cart = get_shopping_cart(request) if cart.items_count() == 0: return redirect('shop_cart') customer = get_customer(request) c_form = CustomerForm(prefix='customer', instance=customer) s_form = AddressForm(prefix='shipping', instance=getattr(customer, 'shipping_address', None)) b_form = AddressForm(prefix='billing', instance=getattr(customer, 'billing_address', None)) if request.method == 'POST': c_form = CustomerForm(request.POST, prefix='customer', instance=customer) commit = True if c_form.is_valid(): customer = c_form.save(commit=False) s_form = AddressForm(request.POST, prefix='shipping', instance=customer.shipping_address) if s_form.is_valid(): customer.shipping_address = s_form.save(commit=False) if customer.same_address: customer.billing_address = customer.shipping_address else: b_form = AddressForm(request.POST, prefix='billing', instance=customer.billing_address) if b_form.is_valid(): customer.billing_address = b_form.save(commit=False) else: commit = False else: commit = False if commit: customer.save() if not cart.customer: cart.customer = customer cart.status = STATUS_CONFIRMED cart.save() return redirect('shop_confirmation') return render_to_response(template_name, { 'cart': cart, 'customerform': c_form, 'shippingform': s_form, 'billingform': b_form, }, context_instance=RequestContext(request))
def do_registration(request, is_nespresso): now = CTZ.normalize(timezone.now()) result = context = {} isBottled = request.session.get('isBottled') if request.method == "POST": email = request.session.get('email') if 'one-password1' in request.POST: rf = CustomRegistrationForm(request.POST, prefix='one') if rf.is_valid(): result['success'] = True details = json.loads(request.POST.get('array')) logger.debug('Registration: first step: %s; details: %s', email, details) request.session['email'] = rf.cleaned_data['email'] request.session['password'] = generate_securerandompass(64) if 'accesstoken' in request.session \ else request.POST['one-password1'] request.session['brew'] = details['brew'] request.session['package'] = details['package'] request.session['interval'] = details['interval'] request.session['different'] = details['different'] mailchimp_subscribe.delay(email=request.session.get('email')) else: return HttpResponse(json.dumps(rf.errors)) if 'two-first_name' in request.POST: cf = GS_CustomerForm(request.POST, prefix='two') pf = GS_PreferencesForm(request.POST, prefix='tri') if cf.is_valid(): result['success'] = True request.session['first_name'] = request.POST['two-first_name'] request.session['last_name'] = request.POST['two-last_name'] request.session['country'] = request.POST.get( 'two-country', 'SG') request.session['line1'] = request.POST['two-line1'] request.session['line2'] = request.POST['two-line2'] request.session['postcode'] = request.POST['two-postcode'] request.session['phone'] = request.POST['two-phone'] mailchimp_subscribe.delay( email=request.session.get('email'), merge_vars={ 'FNAME': request.session.get('first_name'), 'LNAME': request.session.get('last_name'), }, ) else: return HttpResponse(json.dumps(cf.errors)) if 'voucher' in request.POST: try: user = MyUser(email=request.session['email']) user.set_password(request.session['password']) user.save() logger.debug('User created: %s', user) except Exception: logger.error('User not created: %s', email, exc_info=True) try: customer = Customer.objects.create( user=user, first_name=request.session['first_name'], last_name=request.session['last_name'], amount=request.session.get('credits', 0), country=request.session['country'], line1=request.session['line1'], line2=request.session['line2'], postcode=request.session['postcode'], phone=request.session['phone'], stripe_id=request.session['stripe_id'], card_details='{}{}{}'.format( request.session.get('last4'), '{:0>2}'.format(request.session.get('exp_month')), request.session.get('exp_year'))) logger.debug('Customer created: %s', customer) # Add newly created customauth as a Foreign Key reference to FacebookCustomer try: facebook_customer = FacebookCustomer.objects.filter( email=email).update(customer=user) if facebook_customer: add_tag.delay(customer_id=customer.id, tag='Facebook') except Exception: logger.error('Cannot update FacebookCustomer: %s', email, exc_info=True) except Exception: logger.error('Customer not created: %s', email, exc_info=True) try: if request.session.get('from_preregister'): response = None preferences = Preferences(customer=customer) preferences.coffee = coffee = CoffeeType.objects.get( id=request.session.get('coffee')) else: response = GetStartedResponse.objects.filter( email=user.email).latest('created') flavor_ids = [] for ch in response.form_details['flavour']: if ch.isdigit(): flavor_ids.append(int(ch)) flavors = [] for flav in flavor_ids: flavors.append(Flavor.objects.get(id=flav)) preferences = Preferences(customer=customer) preferences.coffee = coffee = CoffeeType.objects.get( id=request.session.get('coffee', response.ct)) preferences.save() preferences.intense = response.form_details['intensity'] preferences.flavor = flavors except Exception: preferences = Preferences(customer=customer) preferences.coffee = coffee = CoffeeType.objects.get( id=request.session.get('coffee')) if request.session.get('alacarte'): price = coffee.amount_one_off else: price = coffee.amount preferences.interval = int(request.session['interval']) preferences.interval_pods = int(request.session['interval']) preferences.different = True if request.session[ 'different'] else False preferences.different_pods = True if request.session[ 'different'] else False if is_nespresso: preferences.brew = BrewMethod.objects.get(name_en='None') preferences.package = Preferences.DRIP_BAGS else: try: if response: brew = BrewMethod.objects.get( id=response.form_details['method']) else: brew_id = request.session.get('brew') if brew_id: brew = BrewMethod.objects.get(id=brew_id) else: brew_title = request.session.get( 'brew_title') or _('None') brew = BrewMethod.objects.get(name=brew_title) preferences.brew = brew except Exception: brew_title = request.session.get('brew_title') or _('None') brew = BrewMethod.objects.get(name=brew_title) preferences.brew = brew preferences.package = request.session['package'] preferences.save() # common voucher try: voucher = Voucher.objects.get( name=request.session.get('voucher')) price -= price * voucher.discount / 100 + voucher.discount2 # Mark voucher as used for current customer customer.vouchers.add(voucher) customer.save() voucher.count += 1 voucher.save() except: voucher = None try: # referral voucher referral_voucher = ReferralVoucher.objects.get( code=request.session.get('referral_voucher')) price -= price * referral_voucher.discount_percent / 100 + referral_voucher.discount_sgd referral_voucher.used = True referral_voucher.save() except Exception: referral_voucher = None if request.session.get('alacarte') or \ request.session.get('gift'): is_recurrent = False else: is_recurrent = True if START_DATE <= now <= END_DATE: shipping_date = NEW_DATE else: shipping_date = get_shipping_date() if isBottled: brew_method = BrewMethod.objects.get(name_en='Cold Brew') preferences.brew = brew_method preferences.package = Preferences.BOTTLED preferences.different = False preferences.save() elif is_nespresso: brew_method = BrewMethod.objects.get(name_en='Nespresso') else: try: brew_method = BrewMethod.objects.get( id=request.session['brew']) preferences.brew = brew_method preferences.save() except Exception: brew_method = preferences.brew try: order = createOrder(p_customer=customer, p_coffee=coffee, p_shipping_date=shipping_date, p_price=price, p_preferences=preferences, p_is_recurrent=is_recurrent, p_brew_method=brew_method, p_voucher=voucher, p_package=preferences.package, is_nespresso=is_nespresso) order.save() if voucher: if voucher.name in settings.GIFT_VOUCHERS: order.details[voucher.name] = True order.save() if referral_voucher: order.details['referred_by'] = '{} {}'.format( \ referral_voucher.sender.first_name, referral_voucher.sender.last_name) order.save() add_event.delay(customer_id=customer.id, event='signed-up', data={'credits': round(customer.amount, 2)}) add_event.delay(customer_id=customer.id, event='created-subscription' if is_recurrent else 'created-one-off', order_id=order.id) except Exception: order = None if not request.session.get('active_tab') and is_nespresso: request.session['active_tab'] = 'active_subscriptions_pods' # Create referral code for new user Referral.objects.create( user=user, code=Referral.get_random_code(customer=customer)) # Use referral link ref = request.session.get('ref') if ref or referral_voucher: referrer = user if ref: referred = Referral.objects.get(code=ref).user else: referred = referral_voucher.sender.user Reference.objects.create(referrer=referrer, referred=referred) logger.debug('Reference created: %s <-- %s', referred, referrer) current_customer = referred.customer add_event.delay(customer_id=current_customer.id, event='reference-created', data={ 'referred': referred.email, 'referrer': referrer.email }) referrer_customer = Customer.objects.get(user_id=referrer.id) add_event.delay(customer_id=referrer_customer.id, event='reference-created', data={ 'referred': referred.email, 'referrer': referrer.email }) # add %50 discount to customer's available discounts referrals = json.loads(referred.customer.discounts['referral']) new_discount = (50, timezone.now().strftime('%Y-%m-%dT%H:%M:%S%z')) referrals.append(new_discount) referred.customer.discounts['referral'] = referrals referred.customer.save() add_event.delay(customer_id=current_customer.id, event='give-$50-to-referrer', data={ 'discount': new_discount[0], 'date': new_discount[1] }) cnt = Reference.objects.filter(referred=referred).count() if cnt == 2: try: current_order = Order.objects \ .filter(customer=current_customer) \ .filter(status__in=['AC', 'PA']) \ .latest('date') current_order.details['KeepCup'] = True current_order.save() except Order.DoesNotExist: pass Reminder.objects.filter(email=user.email).update(completed=True) for reminder in Reminder.objects.filter(email__iexact=user.email): reminder.disable_voucher() new_user = authenticate(email=user.email, password=request.session['password']) login(request, new_user) context = { 'coffee': coffee.name, 'image': coffee.img.url, 'order_price': order.amount if order else coffee.amount, } Reminder.objects.create( username="******".format(customer.first_name.title(), customer.last_name.title()), email=customer.user.email, order=order, from_email='Ernest from Hook Coffee <*****@*****.**>', subject="Ahoy, {}!".format(customer.first_name.title()), template_name='EJ1', # scheduled=now + timedelta(hours=1), scheduled=now + timedelta(minutes=1), # TODO: remove voucher=voucher) ReminderSMS.objects.create( customer=customer, # WARNING: actual message generated in tasks module message=ReminderSMS.MESSAGE % customer.first_name.title(), # will be replaced with sms schedule date on save scheduled=shipping_date, ) # update campaign status to subscribed if from a campaign mailing_list.update_mailchimp_tracking( request, order, MailchimpCampaignStats.PURCHASED) request.session['voucher'] = "" new_purchase_amount = float(price) request.session['new_purchase_amount'] = new_purchase_amount return render(request, 'get_started/thankyou.html', context) return result else: username = email = coffee = default_pack = brew_title = voucher_name = '' price = 0 data = {} render_data = {} logger.debug('Registration:init:%s:%s', get_client_ip(request), get_session_data(request)) if not request.session.get('from_shopping_cart'): try: data['username'] = request.session.get('name') data['email'] = request.session.get('email') default_pack = request.session.get('default_pack') \ or Preferences.WHOLEBEANS if request.session.get('V60STARTER'): default_pack = Preferences.GRINDED brew_title = request.session.get('brew_title') or _('Espresso') except Exception: logger.error('Registration:init:%s:%s', get_client_ip(request), get_session_data(request), exc_info=True) # Check if it's registration by ref link without ReferralVoucher # for example by FB or [copy ref link and share your own way] ref_code = request.session.get('ref') if ref_code: try: referral = Referral.objects.get(code=ref_code) except Referral.DoesNotExist: pass else: # If referral voucher doesn't exists - create and use it ref_voucher, created = ReferralVoucher.objects.get_or_create( sender=referral.user.customer, recipient_email=request.session.get('email', ''), defaults={ # 'discount_percent': 50, 'discount_sgd': 12, 'code': ReferralVoucher.get_random_code(size=8), }) # If referral voucher created right now it means that # customer didn't get points for inviting the friend # (one of the methods above). if created: point_mixin.grant_points( user=referral.user, points=POINTS_FOR_INVITED_FRIEND) request.session['chosen_voucher'] = ref_voucher.code try: coffee = CoffeeType.objects.get( id=request.session.get('coffee')) if request.session.get('alacarte'): price = coffee.amount_one_off else: price = coffee.amount except Exception: coffee = None try: voucher = Voucher.objects.get(name=request.session['voucher']) if voucher: price -= price * voucher.discount / 100 + voucher.discount2 except: pass try: referral_voucher = ReferralVoucher.objects.get( code=request.session.get('referral_voucher')) if referral_voucher: price -= price * referral_voucher.discount_percent / 100 + referral_voucher.discount_sgd except: pass brew_methods = BrewMethod.objects.sorted(~Q(name_en='Nespresso')) rf = CustomRegistrationForm(prefix='one', initial=data) cf = GS_CustomerForm(prefix='two') pf = GS_PreferencesForm(prefix='tri') af = AuthenticationForm(prefix='four') naf = AddressForm() show_login_form = request.session.get( 'from_preregister') and not request.user.is_authenticated show_active_subs = False show_subscription_form = False show_sub_details = False active_subscriptions = [] if request.user.is_authenticated: customer = request.user.customer try: coffee = CoffeeType.objects.get(id=request.session.get('coffee')) except CoffeeType.DoesNotExist: coffee = None nespresso = BrewMethod.objects.get(name='Nespresso') is_pods = False if coffee != None: if nespresso in coffee.brew_method.all(): is_pods = True all_active_subscriptions = Order.objects.filter(customer=customer, status=Order.ACTIVE, recurrent=True) active_subscriptions = [[x.id, x.coffee.name, x.coffee.img.url, x.address.name if x.address else 'Base address']\ for x in all_active_subscriptions if ((nespresso in x.coffee.brew_method.all()) == is_pods and x.is_editable)] if len(active_subscriptions): show_active_subs = True else: show_sub_details = not (show_login_form and show_subscription_form and show_active_subs) # get available addresses addresses = Address.objects.filter(customer=customer) addresses = [{ 'id': x.id, 'name': x.name, 'country': str(x.country), 'line1': x.line1, 'line2': x.line2, 'postcode': x.postcode, 'customer_name': '{} {}'.format(customer.first_name, customer.last_name), } for x in addresses] render_data['base_address'] = json.dumps({ 'id': -1, 'country': str(customer.country), 'line1': customer.line1, 'line2': customer.line2, 'postcode': customer.postcode, 'customer_name': '{} {}'.format(customer.first_name, customer.last_name), }) render_data['addresses'] = json.dumps(addresses) else: show_subscription_form = not show_login_form render_data['addresses'] = [] render_data['base_address'] = {} active_subscriptions = json.dumps(active_subscriptions) render_data['reg_form'] = rf render_data['cus_form'] = cf render_data['pre_form'] = pf render_data['auth_form'] = af render_data['new_address_form'] = naf render_data['coffee'] = coffee render_data['isNespresso'] = is_nespresso render_data['price'] = "{0:.0f}".format(price) render_data['brew_title'] = brew_title render_data['brew_methods'] = brew_methods render_data['alacarte'] = request.session.get('alacarte') render_data['isBottled'] = isBottled render_data['default_pack'] = default_pack render_data['shipping_date'] = get_shipping_date() render_data['stripe_key'] = settings.PUBLISHABLE_KEY render_data['show_login_form'] = show_login_form render_data['show_subscription_form'] = show_subscription_form render_data['show_active_subs'] = show_active_subs render_data['show_sub_details'] = show_sub_details render_data['active_subscriptions'] = active_subscriptions render_data['estimated_date'] = get_estimated_date return render(request, 'get_started/registration.html', render_data)
def get_context_data(self, **kwargs): context = super(ProfileView, self).get_context_data(**kwargs) now = CTZ.normalize(timezone.now()) day = now.isoweekday() user = self.request.user customer, _ = Customer.objects.get_or_create( user=user, defaults={}, ) preferences, _ = Preferences.objects.get_or_create( customer=customer, defaults={}, ) if day in [1, 2, 3, 4, 7]: # isoweekdays context['shipping_tmr'] = timezone.now() + timedelta(days=1) else: context['shipping_tmr'] = False context['customer_details_form'] = CustomerDetailsForm( instance=customer, label_suffix='') context['referral_form'] = ReferralForm() context['addresses_forms'] = self._get_addresses_forms(customer) context['order_forms'] = self._get_order_forms(customer) context['new_address_form'] = AddressForm( initial={'recipient_name': customer.get_full_name()}) context['preferences_form'] = PreferencesForm(no_bottled=True) # brew_methods = BrewMethod.objects.sorted() brew_methods = BrewMethod.objects.all().order_by('id') brew_none = [brew for brew in brew_methods if brew.name_en == 'None'][0] brew_nespresso = [ brew for brew in brew_methods if brew.name_en == 'Nespresso' ][0] brew_non_nespresso = [ brew for brew in brew_methods if brew.name_en != 'Nespresso' ] brew_bottled = [ brew for brew in brew_methods if brew.name_en == 'Cold Brew' ][0] context['brew_methods'] = brew_non_nespresso context['free'] = preferences.present_next context['coffees'] = [ x for x in CoffeeType.objects.bags() if not x.is_bottled() ] context['coffees_pods'] = CoffeeType.objects.nespresso() context['coffees_bottled'] = CoffeeType.objects.bottled() context['coffee_rating'] = CoffeeType.objects.avg_rating() context['new_order_form_bags'] = CreateOrderForm( initial={ 'coffee': context['coffees'][0], 'brew': brew_none, 'different': False, }) context['new_order_form_pods'] = CreateOrderForm( initial={ 'coffee': context['coffees_pods'][0], 'brew': brew_nespresso, 'package': Preferences.DRIP_BAGS, 'different': False, }) context['new_order_form_bottled'] = CreateOrderForm( initial={ 'coffee': context['coffees_bottled'][0], 'brew': brew_bottled, 'package': Preferences.BOTTLED, 'different': False, }) context['orders'] = (Order.objects.select_related( 'address', 'brew').filter(customer=customer, recurrent=True, status__in=[ Order.ACTIVE, Order.PAUSED, Order.DECLINED ]).exclude(brew=brew_nespresso).exclude( package=Preferences.BOTTLED)) context['orders_pods'] = (Order.objects.select_related( 'address', 'brew').filter( customer=customer, recurrent=True, status__in=[Order.ACTIVE, Order.PAUSED, Order.DECLINED], brew=brew_nespresso)) context['orders_bottled'] = (Order.objects.select_related( 'address', 'brew').filter( customer=customer, recurrent=True, status__in=[Order.ACTIVE, Order.PAUSED, Order.DECLINED], brew=brew_bottled, package=Preferences.BOTTLED)) context['gear_orders'] = ( GearOrder.objects.select_related('gear').filter( customer=customer, status__in=[Order.ACTIVE, Order.DECLINED, Order.ERROR])) context['workshop_orders'] = ( WorkShopOrder.objects.select_related('workshop').filter( customer=customer, status__in=[Order.ACTIVE, Order.DECLINED, Order.ERROR])) context['history'] = (Order.objects.select_related( 'coffee', 'brew').prefetch_related('reviews').filter(customer=customer, status=Order.SHIPPED)) # previous_month = now.replace(day=1) - timedelta(days=1) # context['best_sellers'] = { # 'month': dt_format(previous_month, 'M'), # 'coffee': ( # CoffeeType.objects # .bags() # .best_seller(order__date__month=previous_month.month)), # 'coffee_pods': ( # CoffeeType.objects # .nespresso() # .best_seller(order__date__month=previous_month.month)), # } context['gears_recommend'] = ( CoffeeGear.objects.prefetch_related('images').filter( recommend=True)) context['redemption_history'] = (RedemItem.objects.filter(user=user)) context['gear_history'] = ( GearOrder.objects.select_related('gear').filter( customer=customer, status=GearOrder.SHIPPED).prefetch_related('gear__images')) context['alacartes'] = (Order.objects.filter( customer=customer, recurrent=False, status__in=[Order.ACTIVE, Order.DECLINED, Order.ERROR])) context['redeem_items'] = (Item.objects.filter(in_stock=True).exclude( name='Free bag of coffee').exclude( name='Special surprise for invites')) point, _ = Point.objects.get_or_create( user=user, defaults={'points': 0}, ) context['points'] = point.points free_bag_item = Item.objects.filter(name='Free bag of coffee') if free_bag_item.exists(): context['free_bag_item'] = free_bag_item[0] context['redemed_points'] = point_mixin.redemed_points(user=user) context['accumulated_points'] = point_mixin.accumulated_points( user=user) context['customer'] = customer context['last4'] = customer.card_details[:4] if len(customer.card_details) > 9: context['exp_month'] = customer.card_details[4:6] context['exp_year'] = customer.card_details[6:] else: context['exp_month'] = '{:0>2}'.format(customer.card_details[4:5]) context['exp_year'] = customer.card_details[5:] ref, _ = Referral.objects.get_or_create( user=user, defaults={'code': Referral.get_random_code(customer=customer)}) context['ref_link'] = self.request.build_absolute_uri( ref.get_absolute_url()) context['stripe_key'] = settings.PUBLISHABLE_KEY context['the_code'] = ref.code context['active_tab'] = self.request.session.get( 'active_tab', 'active_subscriptions') new_purchase_amount = self.request.session.pop('new_purchase_amount', None) context['new_purchase_amount'] = new_purchase_amount context['stripe_notification'] = self.request.session.pop( 'stripe-notification', None) # show the customer survey only after 14 days after # the customer made the first order now = CTZ.normalize(timezone.now()) gte_14_days_after_reg = now > (customer.user.created_at + timedelta(days=14)) already_answered_exp_survey = customer.extra.get( 'answered_exp_survey', False) context['show_customer_survey'] = bool(not already_answered_exp_survey and gte_14_days_after_reg) context['pagename'] = 'profile' for message in get_messages(self.request): if message.extra_tags == 'skip': context['skip'] = message return context