bunlongheng
8/31/2016 - 4:58 PM

accounts.py

# Last updated : BH | 5/17/2016

import requests
import json
import os
import random
import re
import emoji
import sys
import math


def print_menu():

    print ("______________\n")
    print "1.Create    "+emoji.emojize(':heavy_plus_sign:', use_aliases=True)
    print "2.Delete    "+emoji.emojize(':x:', use_aliases=True)
    print "3.Exit      "+emoji.emojize(':door:', use_aliases=True)
    print ("______________\n")

    option = raw_input("Select your option : ")
    print '\n'

    if(int(option) == 1):
        create_account()
    if(int(option) == 2):
        delete_account()
    if(int(option) == 3):
        os.system("clear")
        sys.exit()

def special_match(strg, search=re.compile(r'[^a-z0-9@.]').search):
    return not bool(search(strg))

def get_highest_id(url):
    accounts = requests.get(url).json()
    object_with_max_account_id = max(accounts['data'], key=lambda x: x['account_id'])
    max_account_id = object_with_max_account_id['account_id']
    return max_account_id


def create_account():

    print 'CREATE'
    accounts_num = raw_input("How many Account(s) you want to create ? : ")
    print '\n'
    c = 0

    max_account_id = get_highest_id('http://172.19.242.32:1234/vse/accounts')
    total_accounts = int(max_account_id) + int(accounts_num)
    account_id = max_account_id


    while ( account_id < total_accounts ):

        url = "https://randomuser.me/api/"
        data = requests.get(url).json()
        firstname  = data['results'][0]['name']['first']
        lastname   = data['results'][0]['name']['last']
        email      = data['results'][0]['email']
        address    = data['results'][0]['location']['street'].replace(' ','')
        city       = data['results'][0]['location']['city'].replace(' ','')
        state      = data['results'][0]['location']['state'].replace(' ','')
        postcode   = data['results'][0]['location']['postcode']
        phone      = data['results'][0]['cell']



        if ( special_match(firstname) and special_match(lastname) and special_match(email) and
            special_match(address) and special_match(city) and special_match(state) ):

            account_id = account_id + 1


            radius = 10000                         #Choose your own radius
            radiusInDegrees=radius/1005
            r = radiusInDegrees
            x0 = 42.534755
            y0 = -71.282449

            u = float(random.uniform(0.0,1.0))
            v = float(random.uniform(0.0,1.0))

            w = r * math.sqrt(u)
            t = 2 * math.pi * v

            x = w * math.cos(t)
            y = w * math.sin(t)

            lat  = x + x0
            lng = y + y0


            data = {}

            data['account_id']                = int(account_id)
            data['email_address']             = email
            data['password']                  = 'benu123'
            data['account_type']              = 'customer'
            data['name_prefix']               = ''
            data['first_name']                = firstname
            data['middle_names']              = ''
            data['last_name']                 = lastname
            data['name_suffix']               = ''
            data['non_person_name']           = bool('false')
            data['DBA']                       = ''
            data['display_name']              = firstname + lastname
            data['address1']                  = address
            data['address2']                  = ''
            data['address3']                  = ''
            data['city']                      = city
            data['state']                     = state
            data['postal_code']               = str(postcode)
            data['nation_code']               = 'USA'
            data['phone1']                    = str(phone)
            data['phone2']                    = str(phone)
            data['phone3']                    = str(phone)
            data['longitude']                 = lat
            data['latitude']                  = lng
            data['altitude']                  = 0
            data['time_zone_offset_from_utc'] = -5
            data['customer_type']             = 1

            # print json.dumps(data)
            c = c+1
            url = 'http://172.19.242.32:1234/vse/account'
            headers = {'content-type': 'application/json'}
            r = requests.post(url, data=json.dumps(data), headers=headers)
            if(int(r.status_code) == 200):
                icon = ':white_check_mark:'
            else:
                icon = ':x:'

            print str(c) + ' - ' + str(account_id)  + emoji.emojize(icon, use_aliases=True)


        else:
            continue

    print '\n'

    print_menu()



def delete_account():
    print 'DELETE'
    accounts_num = raw_input("How many Account(s) you want to delete ? : ")
    print '\n'
    max_account_id = get_highest_id('http://172.19.242.32:1234/vse/accounts')
    account_id = (max_account_id + 1) - int(accounts_num)
    c = 1

    while ( account_id <= max_account_id ):
        url = "http://172.19.242.32:1234/vse/account/" + str(account_id)
        r = requests.delete(url)
        if(int(r.status_code) == 200):
            icon = ':white_check_mark:'
        else:
            icon = ':x:'
        print  str(c) + ' - ' + str(account_id) + emoji.emojize(icon, use_aliases=True)

        c = c+1
        account_id = account_id + 1

    print_menu()



#Main
print 'ACCOUNT'
print_menu()