Example #1
0
def handle_optin(messaging):
    fb_page = FacebookPage.objects.get(fbid=messaging['recipient']['id'])
    ref_decoded = base64.urlsafe_b64decode(
        str(messaging['optin']['ref']))
    fb_user_id = messaging['sender']['id']
    signup_query, unique = utils.signup_send_to_messenger(
        fb_page,
        messaging['sender']['id'],
        ref_decoded)

    if signup_query and not unique:
        for signup in signup_query:
            text, button = utils.another_signup_button(signup)
            post_button_message(
                fb_page, fb_user_id, text, button)
    elif signup_query:
        for signup in signup_query:
            utils.mark_registered(signup.event, fb_user_id)
        send_thank_you_message(fb_page, fb_user_id, signup_query)
Example #2
0
def signup_website(
        request,
        form_cls=SignupForm,
        network='website',
        mobile=False,
        template_name_website='signup_mobile_tdc_hkjf17/signup.html',
        template_name_wechat='signup_mobile_tdc_hkjf17/signup_wechat.html',
        template_name_mobile='signup_mobile_tdc_hkjf17/signup_mobile.html'):
    if network == 'website':
        template_name = (template_name_mobile
                         if mobile else template_name_website)
        next_view = 'snm_tdc_hkjf17_thanks_website'
    elif network == 'wechat':
        template_name = template_name_wechat
        form_cls = WechatSignupForm
        next_view = 'snm_tdc_hkjf17_thanks_wechat'
        request.language = SIMPLIFIED_CHINESE

    aid = request.GET.get(settings.CS_TRACKING_PARAMETER, None)

    if request.method == 'GET':
        form = form_cls(language=request.language)
        return render(
            request, template_name, {
                'CS_ACCOUNT_ID': settings.SNM_TDC_HKJF17_CS_ACCOUNT_ID,
                'CS_TRACKING_PATH': settings.SNM_TDC_HKJF17_URL_ROOT,
                'aid': aid,
                'is_cn': True if network == 'wechat' else False,
                'form': form,
                'network': network,
                'trackA': True,
            })

    elif request.method == 'POST':
        logger.debug(str(request.POST), extra=logging_extra(request))
        form = form_cls(data=request.POST, language=request.language)

        if not aid:
            aid_cookie = [
                value for key, value in request.COOKIES.items()
                if '.'.join(('_pk_id',
                             settings.SNM_TDC_HKFFW17_CS_ACCOUNT_ID)) in key
            ]
            aid = (aid_cookie[0].split('.')[-1]
                   if len(aid_cookie) != 0 else None)

        if form.is_valid():
            contact = form.save(commit=False)

            with transaction.atomic():
                # NOTE: assume we have enough unused codes
                if contact.attend_jfs:
                    code_jfs = SignupCode.objects.filter(used=False,
                                                         is_jfs=True)[0]
                    code_jfs.used = True
                    code_jfs.save()
                    contact.signup_code_jfs = code_jfs.value
                if contact.attend_idg:
                    code_idg = SignupCode.objects.filter(used=False,
                                                         is_idg=True)[0]
                    code_idg.used = True
                    code_idg.save()
                    contact.signup_code_idg = code_idg.value

                contact_mgr = Contact.objects.filter(used_fb_mgr=True).count()

                if contact_mgr < settings.SNM_TDC_HKJF17_ALLOWED_FB_MGR_USER \
                    and ((request.language == 'chi' and contact.country == 'hk'
                          ) or request.language == 'eng'):
                    contact.is_show_mgr_btn = True

                fb_psid = form.cleaned_data['fb_psid']

                contact.language = request.language
                contact.aid = aid
                contact.is_mobile = mobile

                for i in xrange(5):
                    auth_str = binascii.hexlify(os.urandom(16))
                    if not Contact.objects.filter(auth_str=auth_str).exists():
                        contact.auth_str = auth_str
                        contact.save()
                        break

                if network == 'website':
                    contact.language = request.language
                elif network == 'twitter':
                    contact.language = ENGLISH
                    contact.is_twitter = True
                elif network == 'wechat':
                    contact.language = SIMPLIFIED_CHINESE
                    contact.is_wechat = True
                contact.save()

                if fb_psid != '':
                    event_query = Event.objects.filter(
                        app_model_path='signup_mobile_tdc_hkjf17.Contact')

                    for event in event_query:
                        mark_registered(event, fb_psid)

                    data_ref = ('signup_mobile_tdc_hkjf17.Contact|%s' %
                                contact.auth_str)
                    create_mgr_signups(settings.SNM_TDC_HKJF17_FB_PAGE_ID,
                                       fb_psid, data_ref)

            logger.debug('Contact received.', extra=logging_extra(request))
            return _redirect(
                next_view, {
                    't': contact.auth_str,
                    'cid': contact.email,
                    'mgr': contact.is_show_mgr_btn,
                })
        else:
            logger.debug('Invalid form data.', extra=logging_extra(request))
            return render(request, template_name, {
                'form': form,
                'network': network,
            })
Example #3
0
def signup(
        request,
        form_cls=SignupForm,
        mobile=False,
        template_name_desktop='signup_mobile_tdc_hkelfae17/signup.html',
        template_name_mobile='signup_mobile_tdc_hkelfae17/signup_mobile.html'):
    template_name = template_name_mobile if mobile else template_name_desktop
    next_view = ('snm_tdc_hkelfae17_thanks_mobile'
                 if mobile else 'snm_tdc_hkelfae17_thanks')

    aid = request.GET.get(settings.CS_TRACKING_PARAMETER, None)

    if request.method == 'GET':
        form = form_cls(language=request.language)
        return render(
            request, template_name, {
                'CS_ACCOUNT_ID': settings.SNM_TDC_HKELFAE17_CS_ACCOUNT_ID,
                'CS_TRACKING_PATH': settings.SNM_TDC_HKELFAE17_URL_ROOT,
                'aid': aid,
                'form': form,
                'trackA': True,
            })

    elif request.method == 'POST':
        logger.debug(str(request.POST), extra=logging_extra(request))
        form = form_cls(data=request.POST, language=request.language)

        if not aid:
            aid_cookie = [
                value for key, value in request.COOKIES.items()
                if '.'.join(('_pk_id',
                             settings.SNM_TDC_HKELFAE17_CS_ACCOUNT_ID)) in key
            ]
            aid = (aid_cookie[0].split('.')[-1]
                   if len(aid_cookie) != 0 else None)

        if form.is_valid():
            contact = form.save(commit=False)

            with transaction.atomic():
                # NOTE: assume we have enough unused codes
                if contact.attend_elec:
                    code_elec = SignupCode.objects.filter(used=False,
                                                          is_elec=True)[0]
                    code_elec.used = True
                    code_elec.save()
                    contact.signup_code_elec = code_elec.value
                if contact.attend_light:
                    code_light = SignupCode.objects.filter(used=False,
                                                           is_light=True)[0]
                    code_light.used = True
                    code_light.save()
                    contact.signup_code_light = code_light.value

                # if request.language == 'eng':
                #     contact.is_show_mgr_btn = True
                fb_psid = form.cleaned_data['fb_psid']

                contact.language = request.language
                contact.aid = aid
                contact.is_mobile = mobile

                for i in xrange(5):
                    auth_str = binascii.hexlify(os.urandom(16))
                    if not Contact.objects.filter(auth_str=auth_str).exists():
                        contact.auth_str = auth_str
                        contact.save()
                        break
                contact.save()

                if fb_psid != '':
                    event_query = Event.objects.filter(
                        app_model_path='signup_mobile_tdc_hkelfae17.Contact')

                    for event in event_query:
                        mark_registered(event, fb_psid)

                    data_ref = ('signup_mobile_tdc_hkelfae17.Contact|%s' %
                                contact.auth_str)
                    create_mgr_signups(settings.SNM_TDC_HKELFAE17_FB_PAGE_ID,
                                       fb_psid, data_ref)

            logger.debug('Contact received.', extra=logging_extra(request))
            return _redirect(
                next_view, {
                    't': contact.auth_str,
                    'cid': contact.email,
                    'mgr': contact.is_show_mgr_btn,
                })
        else:
            logger.debug('Invalid form data.', extra=logging_extra(request))
            return render(request, template_name, {
                'form': form,
            })