Пример #1
0
def guest_login(trackid,guesttrack,wifisite,guestdevice,phoneconfig,loginauth):
    ''' Function to called if the site is configured with phone login    
    
    '''    

    #show the configured landing page
    phone_form = generate_phoneform(phoneconfig)
    if phone_form.validate_on_submit():
        #assign a guest based on form
        newguest = assign_guest_entry(wifisite,guesttrack,form=phone_form)
        #create new auth
        loginauth.populate_auth_details(phoneconfig)
        loginauth.reset()     
        loginauth.state = LOGINAUTH_FIRST
        loginauth.save()
        #update guesttrack   
        guesttrack.state        = GUESTTRACK_POSTLOGIN
        guesttrack.loginauthid  = loginauth.id 
        guesttrack.updatestat('auth_phone',1)
        #update guestdevice
        guestdevice.guestid     = newguest.id
        guestdevice.save()
        #update guest
        newguest.demo           = guesttrack.demo
        newguest.devices.append(guestdevice)
        newguest.save()
        guestlog_debug('phone_login  new guest track ID:%s'%newguest.id,wifisite,guesttrack)
        return redirect_guest(wifisite,guesttrack)

    landingpage = Landingpage.query.filter_by(siteid=wifisite.id).first()
    return render_template('guest/%s/phone_landing.html'%wifisite.template,\
            wifisite=wifisite,landingpage=landingpage,phone_form=phone_form,
            trackid=trackid)   
Пример #2
0
def guest_login(trackid,
                guesttrack,
                wifisite,
                guestdevice,
                voucherconfig,
                voucherauth,
                voucherid=None):
    ''' Function to called if the site is configured with payment login    
    
    '''
    #show the configured landing page
    voucher_form = generate_voucherform(voucherconfig)
    if voucher_form.validate_on_submit():
        voucher = Voucher.query.filter(
            and_(Voucher.siteid == wifisite.id,
                 Voucher.voucher == voucher_form.voucher.data)).first()
        if voucher:
            #check and update validity of paymentaccount
            (status, msg) = voucher.check_and_update_validity(voucherauth)
            if status:
                #assign a guest based on form
                newguest = assign_guest_entry(wifisite,
                                              guesttrack,
                                              form=voucher_form)
                #update guesttrack
                guesttrack.state = GUESTTRACK_POSTLOGIN
                guesttrack.loginauthid = voucherauth.id
                guesttrack.updatestat('auth_voucher', 1)
                guesttrack.save()
                #update guestdevice
                guestdevice.guestid = newguest.id
                guestdevice.save()
                #update guest
                newguest.demo = guesttrack.demo
                newguest.devices.append(guestdevice)
                newguest.save()
                guestlog_debug('voucher_login  new guest track ID:%s'%\
                                newguest.id,wifisite,guesttrack)
                return redirect_guest(wifisite, guesttrack)
            else:
                flash(msg, 'danger')
                guestlog_warn('in voucher.guest_login limit expired', wifisite,
                              guesttrack)
        else:
            #transaction failed! display msg
            flash(_l('Wrong voucher entry'), 'danger')
            guestlog_warn('in voucher.guest_login wrong voucher id', wifisite,
                          guesttrack)

    landingpage = Landingpage.query.filter_by(siteid=wifisite.id).first()
    return render_template('guest/%s/voucher_landing.html'%wifisite.template,\
            wifisite=wifisite,landingpage=landingpage,voucher_form=voucher_form,
            trackid=trackid)
Пример #3
0
def test_assign_guest_entry(client, session):

    #create dummy email and phone forms
    class DummyForm1(Form):
        email = TextField('Email')
        firstname = TextField('Firstname')
        extra1 = TextField('Extra1')
        extra2 = TextField('Extra2')

    class DummyForm2(Form):
        phonenumber = TextField('Email')
        firstname = TextField('Firstname')
        extra1 = TextField('Extra1')

    class DummyFBProfile():
        first_name = None
        last_name = None
        email = None
        gender = None
        birthday = None
        age_range = None

    eform = DummyForm1()
    eform.email.data = '*****@*****.**'
    eform.firstname.data = 'firstname'
    eform.extra1.data = 'extra1'
    eform.extra2.data = 'extra2'

    pform = DummyForm2()
    pform.phonenumber.data = '+1234567890'
    pform.firstname.data = 'firstname'
    pform.extra1.data = 'extra1'

    profile = {
        'first_name': 'first_name',
        'last_name': 'last_name',
        'email': '*****@*****.**',
        'age_range': {
            'min': 21,
            'max': 28
        }
    }

    site1 = Wifisite.query.get(1)
    #test creating a new track

    ##-----test email form
    track1 = init_track(site1, guestmac=randomMAC(), apmac=randomMAC())
    track2 = init_track(site1, guestmac=randomMAC(), apmac=randomMAC())
    guest1 = assign_guest_entry(site1, track1, form=eform)
    guest1 = assign_guest_entry(site1, track2, form=eform)
    cnt = Guest.query.count()
    assert 1 == cnt, 'number of guest created is not 1 but :%s ' % cnt
    newguest = Guest.query.get(1)
    assert newguest.details == {
        'Extra1': 'extra1',
        'Extra2': 'extra2'
    }, 'Guest details is :%s insteads \
                                of expected :%s' % (newguest.details, {
        'Extra1': 'extra1',
        'Extra2': 'extra2'
    })

    assert newguest.siteid == site1.id, "Guest siteid is not correctly populated"
    assert 1 == Guesttrack.query.get(1).loginstat.get('newguest'),\
            'newguest is not set to 1 after new guest added'
    assert None == Guesttrack.query.get(2).loginstat.get('newguest'),\
            'newguest is not set to None after existing guest found'

    ##-----test phone form
    track3 = init_track(site1, guestmac=randomMAC(), apmac=randomMAC())
    track4 = init_track(site1, guestmac=randomMAC(), apmac=randomMAC())
    guest2 = assign_guest_entry(site1, track3, form=pform)
    guest2 = assign_guest_entry(site1, track4, form=pform)
    cnt = Guest.query.count()
    assert 2 == cnt, 'number of guest created is not 2 but :%s ' % cnt
    assert 1 == Guesttrack.query.get(3).loginstat.get('newguest'),\
            'newguest is not set to 1 after new guest added'
    assert None == Guesttrack.query.get(4).loginstat.get('newguest'),\
            'newguest is not set to None after existing guest found'

    ##-----test FB profile
    track5 = init_track(site1, guestmac=randomMAC(), apmac=randomMAC())
    track6 = init_track(site1, guestmac=randomMAC(), apmac=randomMAC())
    guest1 = assign_guest_entry(site1, track5, fbprofile=profile)
    guest1 = assign_guest_entry(site1, track6, fbprofile=profile)
    cnt = Guest.query.count()
    assert 3 == cnt, 'number of guest created is not 3 but :%s ' % cnt
    newguest = Guest.query.get(3)
    assert '*****@*****.**' == newguest.email, 'Wrong email '
    assert '21-28' == newguest.agerange, 'Wrong age range'
    assert 1 == Guesttrack.query.get(5).loginstat.get('newguest'),\
            'newguest is not set to 1 after new guest added'
    assert None == Guesttrack.query.get(6).loginstat.get('newguest'),\
            'newguest is not set to None after existing guest found'
Пример #4
0
def fb_login_check(trackid, guesttrack, wifisite, guestdevice, fbconfig,
                   loginauth):
    '''End point for validating guest's FB login

    '''
    code = request.args.get('code')
    access_token = None
    fb_appid = fbconfig.fb_appid
    fb_app_secret = fbconfig.fb_app_secret
    if code:
        #URL called after OAuth
        redirect_uri = url_for('unifispot.modules.facebook.fb_login_check',
                               trackid=trackid,
                               _external=True)
        try:
            at = GraphAPI().get_access_token_from_code(code, redirect_uri,
                                                       fb_appid, fb_app_secret)
            access_token = at['access_token']
            graph = GraphAPI(access_token)
            profile = graph.get_object(
                "me",
                fields=
                'name,email,first_name,last_name,gender,birthday,age_range')
            if not profile:
                #
                #User is not logged into DB app, redirect to social login page
                guestlog_warn(
                    'guestdevice MAC:%s facebook_login  empty profile, \
                    should be redirected to login ' % guestdevice.devicemac,
                    wifisite, guesttrack)
                return redirect_guest(wifisite, guesttrack)
        except:
            guestlog_exception(
                'guestdevice MAC:%s facebook_login  exception , \
                    should be redirected to login ' % guestdevice.devicemac,
                wifisite, guesttrack)
            return redirect_guest(wifisite, guesttrack)
    else:
        #URL could be called by JS, check for cookies
        #
        try:
            check_user_auth = get_user_from_cookie(cookies=request.cookies,
                                                   app_id=fb_appid,
                                                   app_secret=fb_app_secret)
            access_token = check_user_auth['access_token']
            graph = GraphAPI(access_token)
            profile = graph.get_object(
                "me",
                fields=
                'name,email,first_name,last_name,gender,birthday,age_range')
            if not check_user_auth or not check_user_auth['uid'] or not profile:
                #
                #User is not logged into DB app, redirect to social login page
                guestlog_warn(
                    'guestdevice MAC:%s facebook_login  empty profile, \
                    should be redirected to login ' % guestdevice.devicemac,
                    wifisite, guesttrack)
                return redirect_guest(wifisite, guesttrack)
        except:
            guestlog_exception(
                'guestdevice MAC:%s facebook_login  exception , \
                    should be redirected to login ' % guestdevice.devicemac,
                wifisite, guesttrack)
            return redirect_guest(wifisite, guesttrack)
    #create FB AUTH
    loginauth.fbprofileid = profile['id']
    loginauth.fbtoken = access_token
    loginauth.save()

    #add/update guest
    newguest = assign_guest_entry(wifisite, guesttrack, fbprofile=profile)

    #update guesttrack

    #update guestdevice
    guestdevice.guestid = newguest.id
    guestdevice.save()
    #update guest
    newguest.demo = guesttrack.demo
    newguest.devices.append(guestdevice)
    newguest.save()

    #check if either checkin/like configured
    if fbconfig.auth_fb_post == 1:
        return redirect(
            url_for('unifispot.modules.facebook.fb_checkin', trackid=trackid))
    elif fbconfig.auth_fb_like == 1 and newguest.fbliked != 1:
        return redirect(
            url_for('unifispot.modules.facebook.fb_like', trackid=trackid))
    else:
        loginauth.populate_auth_details(fbconfig)
        loginauth.reset()
        loginauth.reset_lastlogin()
        loginauth.state = LOGINAUTH_FIRST
        loginauth.save()
        #neither configured, authorize guest
        guesttrack.state = GUESTTRACK_POSTLOGIN
        guesttrack.loginauthid = loginauth.id
        guesttrack.updatestat('auth_facebook', 1)
        return redirect_guest(wifisite, guesttrack)
Пример #5
0
def init_demo():
    with app.app_context():
        from sqlalchemy.exc import OperationalError
        from flask_security.utils import encrypt_password
        from unifispot.core.models import User, Account, Admin, Client, Wifisite, Landingpage
        from unifispot.core.guestutils import init_track, assign_guest_entry
        from unifispot.modules.analytics.methods import update_daily_stat
        from tests.helpers import randomMAC, randomGuestEmailForm
        from random import randint

        account = Account.query.filter_by(id=1).first()
        enc_pass = encrypt_password('password')

        client1 = Client.query.filter_by(email='*****@*****.**').first()
        if not client1:
            #add a client
            client1 = Client(email='*****@*****.**',
                             password=enc_pass,
                             displayname="Client User",
                             active=1)
            client1.account_id = account.id
            client1.save()

        site1 = Wifisite.query.get(1)
        if not site1:
            site1 = Wifisite(name='Site1', sitekey='site1')
            site1.client_id = client1.id
            site1.account_id = account.id
            site1.save()
            landing1 = Landingpage()
            landing1.siteid = site1.id
            landing1.save()

        now = arrow.now()
        month_start = now.replace(days=-30)
        days = (now - month_start).days
        for d in range(days):
            day_start = month_start.replace(days=d).floor('day')
            logger.warn('-------Generating data for :%s' % day_start)
            for i in range(randint(5, 10)):
                track = init_track(site1,
                                   guestmac=randomMAC(),
                                   apmac=randomMAC())
                track.timestamp = day_start.replace(minutes=+i * 1).naive
                track.loginstat = {'num_visits': 1, 'auth_email': 1}
                track.save()
                if i % 2:
                    f = randomGuestEmailForm()
                    g = assign_guest_entry(site1, track, f)
                    g.createdat = day_start.replace(minutes=+i * 1).naive
                    g.save()
                track.save()
                logger.warn('track added for email ID;%s for :%s' %
                            (track.id, day_start))

            #create random
            #half of them new user
            for i in range(randint(5, 10)):
                track = init_track(site1,
                                   guestmac=randomMAC(),
                                   apmac=randomMAC())
                track.timestamp = day_start.replace(minutes=+i * 1).naive
                track.loginstat = {
                    'num_visits': 1,
                    'auth_facebook': 1,
                    'fbcheckedin': 1
                }
                track.save()
                if i % 2:
                    g = assign_guest_entry(site1, track, f)
                    g.createdat = day_start.replace(minutes=+i * 1).naive
                    g.save()
                    track.updatestat('fbliked', 1)
                    track.save()
                track.save()
                logger.warn('track added for FB ID;%s for :%s' %
                            (track.id, day_start))
            update_daily_stat(site1, day_start)