def get_organization(organization_domain=SITE_DOMAIN): """ Function to get project domain """ try : organization = DomainOrganizationModel.objects.get(domain__name=organization_domain,is_active=True) except Exception, e : LOGGER.error('Failed to get organization for domain %s.' % organization_domain + str(e)) organization = None
def get_entity_by_name(name=''): """ Function to get entity by name """ try : entity_list = EntityModel.objects.filter(entitytranslationmodel__name__icontains=name, is_active=True,is_delete=False) except Exception, e : LOGGER.error('Failed to get entity for name %s. ' % name + str(e)) entity_list = []
def get_language(code=LANGUAGE_CODE) : """ Fucntion to get project language """ try : language = LanguageModel.objects.get(code=code) except Exception, e : LOGGER.error('Failed to get language for language code %s.' % code + str(e)) language = None sys.exit(1)
def get_token_by_exhibitor(organization=None,language=None,exhibitor=None) : """ Function to get exhibitor tokens by exhibitor """ status = True try : exhibitor_token = ExhibitorTokenModel.objects.get(exhibitor=exhibitor) except Exception, e : LOGGER.error('Failed to get exhibitor tokens.' + str(e)) status = False exhibitor_token = None
def check_option(option): """ Function to check if give option is as per python standards """ status = True try: request_option = eval(option) except Exception, e: LOGGER.error("Failed to covert to Python standards.") status = False request_option = []
def get_event_by_id(organization=None,language=None,event_id=EVENT_ID) : """ Function to get event with id """ try : event = EventModel.objects.get(id=event_id,is_active=True, event_name__organization=organization) except Exception, e : LOGGER.error('Failed to get event with id %s. ' % event_id + str(e)) event = None sys.exit(1)
def get_exhibitor_by_event_n_entity(organization=None,language=None,event=None,entity=None) : """ Function to get exhibitor by event and entity """ status = True try : exhibitor = ExhibitorModel.objects.get(event=event,entity=entity, is_active=True,is_delete=False) except Exception, e : LOGGER.error('Failed to get Exhibitor. ' + str(e)) status = False
def args_three(args_list): """ Function to return element three of sys args """ status = True try: three = args_list[3] except Exception, e: LOGGER.error("Failed to get element 3 for sys args") three = "" status = False
def args_two(args_list): """ Function to return element two of sys args """ status = True try: two = args_list[2] except Exception, e: LOGGER.error("Failed to get element 2 for sys args") two = "" status = False
def open_file(file,mode='r'): """ Function to open a file """ status = True try : file_object = open(file,mode) except : LOGGER.error('Failed to open File.') file_object = None status = False return status, file_object
def args_one(args_list): """ Function to validate element one of args """ function_counter = 0 request_function = None # get element one try: one = args_list[1] except Exception, e: LOGGER.info("Please provide a function name.") sys.exit(1)
def check_number(option): """ Function to check ranges """ status = True try: for tup in option: if not len(tup) == 2: status = False if tup[0] > tup[1]: status = False except Exception, e: LOGGER.error("Token Ranges not proper.") status = False
def connect(path=PROJECT_PATH,settings=PROJECT_SETTINGS): """ Function to set system path and django settings module """ # check for empty settings check_path(path) check_settings(settings) try : sys.path.append(path) sys.path.append(os.path.abspath(os.path.join(os.path.abspath(path),os.path.pardir))) except Exception, e : LOGGER.error("Failed to set project path in OS sys path. " + str(e)) sys.exit(1)
def set_country_code() : """ Function to set country code """ # get args ( element two ) status, country_code_file = args_two(sys.argv) if not status : LOGGER.info("Please provide Country Code file.") LOGGER.info("Usage : python execute set_country_code <CountryCodeFile>.") sys.exit(1) # open the given file status, file = open_file(country_code_file) if not status : LOGGER.info('Please check file location.') sys.exit(1) # read content country_list = file.read() # load json format country_list = json.loads(country_list) # get country code list country_code_list = [ k for k in country_list ] # add country code to CountryModel add_country_code(code_list=country_code_list) LOGGER.info('Congratulations! Country Code added successfully.')
def check_format(option): """ Function to check format """ status = True try: if not isinstance(option, ListType): status = False for tup in option: if not isinstance(tup, TupleType): status = False if not isinstance(tup[0], IntType) or not isinstance(tup[1], IntType): status = False except Exception, e: LOGGER.error("Formatting error.") status = False request_option = []
def add_token_to_exhibitor(organization=None,language=None,exhibitor_token=None,token_range=[]): """ Function to add exhibitor_token """ range_dict = {} status = True try : current_tokens = json.loads(exhibitor_token.tokens) for i in token_range : for x in range(i[0],i[1]+1) : range_dict.update({ x: 0}) current_tokens = dict( current_tokens.items() + range_dict.items() ) exhibitor_token.tokens = json.dumps(current_tokens) exhibitor_token.save() except Exception,e : LOGGER.error('Failed to add exhibitor token.' + str(e)) status = False
def set_country(): """ Function used to set country """ # get args ( element two ) status, language_code = args_two(sys.argv) if not status : LOGGER.info("Please provide Language Code.") LOGGER.info("Usage : python execute set_country <LanguageCode> <CountryCodeFile>.") sys.exit(1) # get args ( element three ) status, country_file = args_three(sys.argv) if not status : LOGGER.info("Please provide Country file.") LOGGER.info("Usage : python execute set_country <LanguageCode> <CountryCodeFile>.") sys.exit(1) # get language language = get_language(code=language_code) # open the given file status, file = open_file(country_file) if not status : LOGGER.info('Please check file location.') sys.exit(1) # read content country_list = file.read() # load json format country_list = json.loads(country_list) # add country to CountryTranslationModel add_country(language=language,country_dict=country_list) LOGGER.info('Congratulations! Countries added successfully.')
def set_token(): """ Function to set tokens for an exhibitor """ # get organization organization = get_organization() # get language language = get_language() # get args ( element two ) status, entity_name = args_two(sys.argv) if not status : LOGGER.info("Please provide Entity/Exhibitor Name.") sys.exit(1) # get args ( element three ) status, token_range = args_three(sys.argv) if not status : LOGGER.info("Please provide token range.") sys.exit(1) # check for option standards option_status, token_range = check_option(token_range) if not option_status : LOGGER.info("Please provide option as per Python standards.") sys.exit(1) # check for option format format_status = check_format(token_range) if not format_status : LOGGER.info("Please Provide Proper Format ex:'[(100,200),(500,800)]'.") sys.exit(1) # check for option format number_status = check_number(token_range) if not number_status : LOGGER.info("Please Provide proper ranges ex:'[(100,200),(500,800)]'.") sys.exit(1) # get entity list entity_list = get_entity_by_name(name=entity_name) # check for empty list if not entity_list : LOGGER.info("Entity/Exhibitor with name %s does not exist" %entity_name) sys.exit(1) # check for multiple exhibitors if len(entity_list) > 1 : counter = 0 # get entity values entity_values_list = entity_list.values('id','entitytranslationmodel__name') print("Please select appropriate entity") for v in entity_values_list : print(str(counter) + ".ID: " + str(v['id']) + " " + "Name :" + v['entitytranslationmodel__name']) counter = counter + 1 print("Type 'exit' or 'e' to quit") while True : request_entity = raw_input("Please select an option:") if str(request_entity).lower() == 'exit' or str(request_entity).lower() == 'e' : sys.exit(1) try : if int(request_entity) < len(entity_list) and int(request_entity) >= 0 : break else : LOGGER.info("Wrong option selected") except Exception, e : LOGGER.info("Wrong option selected") entity = entity_list[int(request_entity)]
LOGGER.info("Wrong option selected") entity = entity_list[int(request_entity)] else : entity = entity_list[0] # get event event = get_event_by_id(organization=organization,language=language) # get exhibitor exhibitor_status, exhibitor = get_exhibitor_by_event_n_entity(organization=organization,language=language, event=event,entity=entity) # check exhibitor status if not exhibitor_status : LOGGER.info('Exhibitor for Event %s and Entity %s does not exist.' % (event,entity)) sys.exit(1) # get exhibitor tokens exhibitor_token_status, exhibitor_token = get_token_by_exhibitor(organization=organization,language=language, exhibitor=exhibitor) # check exhibitor token status if not exhibitor_token_status : LOGGER.info('Exhibitor Token for Event %s and Entity %s does not exist.' % (event,entity)) sys.exit(1) # add token to exhibitor token_status = add_token_to_exhibitor(organization=organization,language=language, exhibitor_token=exhibitor_token,token_range=token_range)
# Connecting / Settings up system path and django settings module # python imports import os, sys # project imports from standalone.config import PROJECT_PATH, PROJECT_SETTINGS, LOGGER from standalone.validation import check_path, check_settings def connect(path=PROJECT_PATH,settings=PROJECT_SETTINGS): """ Function to set system path and django settings module """ # check for empty settings check_path(path) check_settings(settings) try : sys.path.append(path) sys.path.append(os.path.abspath(os.path.join(os.path.abspath(path),os.path.pardir))) except Exception, e : LOGGER.error("Failed to set project path in OS sys path. " + str(e)) sys.exit(1) try : os.environ['DJANGO_SETTINGS_MODULE'] = settings except Exception, e : LOGGER.error("Failed to set project settings in OS environment. " + str(e)) sys.exit(1)
def check_settings(settings): """ Function to check if project settings are sepecified """ if not settings : LOGGER.info("Please define project settings in config.py .") sys.exit(1)
def check_path(path): """ Function to check it project path and project settings are empty """ if not path : LOGGER.info("Please define project path in config.py .") sys.exit(1)
request_function = None # get element one try: one = args_list[1] except Exception, e: LOGGER.info("Please provide a function name.") sys.exit(1) # get attr for module import standalone.script attr = dir(standalone.script) if one not in attr: LOGGER.info("%s is not defined." % one) sys.exit(1) if not inspect.isfunction(standalone.script.__dict__[one]): LOGGER.info("%s is not a valid Function." % one) sys.exit(1) else: request_function = standalone.script.__dict__[one] return request_function def args_two(args_list): """ Function to return element two of sys args """ status = True