def validate_user_password(ibs_id, set_new=False): ibs = IBSManager() info = ibs.get_user_info(ibs_id) if str(ibs_id) not in info: logger.error('nothing found for validating user : %s' % ibs_id) return None x = info.get(str(ibs_id))
def create_user_from_ibs(uid, update_mode=False): # l = create_logger(None, InfoCodes.creating_ibs_user) enter_update_mode = False ibs = IBSManager() # l.debug("Getting a list of attributes") attr = ibs.get_user_info(uid) attr = attr[str(uid)] password = get_user_info_by_dic( attr, 'normal_password') # ibs.get_user_password(int(uid)) u = get_user_info_by_dic(attr, 'normal_username') # ibs.get_username(uid) service = ibs.get_user_service(uid) user_is_in_ibs_db = IBSUserInfo.objects.filter(ibs_uid=uid).exists() if u is None: # l.error("No username is assigned for this user : %s" % uid) return False, 1, 'empty username' if (update_mode and user_is_in_ibs_db) or User.objects.filter(username=u).exists(): try: if user_is_in_ibs_db: # l.debug("User found in IBS DB") ibs_id = IBSUserInfo.objects.get(ibs_uid=uid).user.pk i_u = User.objects.get(pk=ibs_id) else: # l.debug("User is NOT in IBS DB") i_u = User.objects.get(username=u) enter_update_mode = True if UserProfile.objects.filter(user__username=u).exists(): # l.debug("Found User profile : %s" % i_u.pk) profile = UserProfile.objects.get(user__username=u) else: # l.debug("Profile Created for : %s" % i_u.pk) profile = UserProfile() except Exception as e: # if e.args: # l.error(" ".join([str(a) for a in e.args])) # else: # l.error(e.message) # l.error("Unable to create or update user : IBI : %s" % uid) return False, 7, 'unable to enter update mode' else: # l.info("Creating a new user") i_u = User() profile = UserProfile() # l.debug("User and Profile instances created") i_u.username = u if not i_u.password: i_u.set_password(password) first_name = get_user_info_by_dic(attr, 'name') if not first_name: first_name = 'UNKNOWN!' if len(first_name) > 30: first_name = first_name[0:30] email = get_user_info_by_dic(attr, 'email') if not email: # l.warning("No email for user : %s" % uid) email = '-' if not first_name: # l.warning("First name is empty for : %s" % uid) first_name = '-' i_u.first_name = first_name i_u.is_active = True address = get_user_info_by_dic(attr, 'address') if not address: address = 'Tehran' str_date = get_user_info_by_dic(attr, 'first_login') if not str_date: date = datetime.today() else: date = parse_date_from_str_to_julian(str_date) i_u.date_joined = date profile.address = address geo_loc = get_user_info_by_dic(attr, 'custom_field_Geo') if not geo_loc: geo_loc = '-' profile.geo_code = geo_loc identity_num = get_user_info_by_dic(attr, 'custom_field_NCode') if not identity_num: # l.warning("No identity Number for user : %s" % uid) identity_num = '-' elif len(identity_num) > 10: identity_num = identity_num[0:9] profile.identity_number = identity_num phone = get_user_info_by_dic(attr, 'phone') mobile = get_user_info_by_dic(attr, 'cell_phone') profile.comment = get_user_info_by_dic(attr, 'comment') if not profile.comment: profile.comment = '-' # print 'Checking mobile' if not mobile: mobile = '-' elif len(mobile) > 12: mobile = mobile[0:12] # print 'Mobile passed' # print 'Checking phone' if not phone: phone = '-' elif len(phone) > 15: phone = phone[0:13] # print 'phone passed' profile.telephone = phone profile.mobile = mobile birth_data = parse_date_from_str_to_julian( get_user_info_by_dic(attr, 'birthdate')) i_u.email = email profile.birth_date = birth_data try: i_u.save() profile.user = i_u if birth_data: profile.birth_date_day = birth_data.day profile.birth_date_month = birth_data.month else: profile.birth_date_day = 1 profile.birth_date_month = 1 profile.marriage_date = datetime.today() profile.marriage_date_day = 1 profile.marriage_date_month = 1 profile.save() crm_uid = i_u.pk # l.info("User has been updated : %s" % crm_uid) except Exception as e: # l.error("Saving user data failed for : %s" % uid) # if e.args: # l.error(" ".join([str(a) for a in e.args])) # else: # l.error(e.message) return False, 4, 'unable to create new user' try: if not user_is_in_ibs_db: # l.info("Adding user to IBS DB") ibi = IBSUserInfo() ibi.ibs_uid = int(uid) ibi.user = i_u ibi.save() # l.info("User added to IBS DB") except Exception as e: # l.error("Error while trying to add user to IBS DB") # if e.args: # l.error(" ".join([str(a) for a in e.args])) # else: # l.error(e.message) return False, 6, 'unable to assign user to ibs details' try: # l.debug("Finding if user has any service") g_info = IBSService.objects.get(ibs_name=service) # i_s = IBSService.objects.get(pk=g_info.service_id) except Exception as e: # l.error("Error while trying to find a service for user") # if e.args: # l.error(" ".join([str(a) for a in e.args])) # else: # l.error(e.message) return False, 5, 'unable to find service' if enter_update_mode and UserCurrentService.objects.filter( user=crm_uid).exists(): # l.info("Preparing for updating user service : %s" % uid) i_cs = UserCurrentService.objects.get(user=crm_uid) if i_cs.service_id != g_info.pk: i_cs.service = g_info i_cs.service_property = ServiceProperty.objects.get( pk=g_info.fk_default_service_property_service.get().default.pk) else: # l.info("Creating service information for : %s" % uid) i_cs = UserCurrentService() i_cs.service_property = ServiceProperty.objects.get( pk=g_info.fk_default_service_property_service.get().default.pk) i_cs.service = g_info i_cs.user = i_u i_cs.is_active = True expire_date = parse_date_from_str_to_julian( get_user_info_by_dic(attr, 'abs_exp_date')) if expire_date: i_cs.expire_date = expire_date try: i_cs.save() # l.info("User Service data has been saved") except Exception as e: # l.error("Unable to save user service data : %s" % uid) # if e.args: # l.error(" ".join([str(a) for a in e.args])) # else: # l.error(e.message) return False, 6, 'unable to assign service' return True, uid, crm_uid