def results_cuerpo(request): form = AdministratorResultsCuerpoForm(request.GET) cuerpo_list = [] # soon to be filled if form is submitted # verify if form has been sent if form.is_valid(): # get region if selected region = form.cleaned_data["region"] if region: # fetch all cuerpos from db cursor = connections["principal"].cursor() cuerpo_data_list = Region.fetch_all_related(cursor, region.old_id) for cuerpo_data in cuerpo_data_list: # try first to get cuerpo from our records try: cuerpo = Cuerpo.objects.get(old_id=cuerpo_data["id"]) cuerpo_list.append(cuerpo) except Cuerpo.DoesNotExist: cuerpo = Cuerpo.fetch_from_db(cursor, cuerpo_data["id"]) if cuerpo: # is null in case of virtual cuerpo or inconsistent db cuerpo.save() cuerpo_list.append(cuerpo) return render_to_response('administrator/results_cuerpo.html', { 'form': form, 'cuerpo_list': cuerpo_list, 'menu_titles': True, }, context_instance=RequestContext(request))
def results_cuerpo(request): form = AdministratorResultsCuerpoForm(request.GET) cuerpo_list = [] # soon to be filled if form is submitted # verify if form has been sent if form.is_valid(): # get region if selected region = form.cleaned_data["region"] if region: # fetch all cuerpos from db cursor = connections["principal"].cursor() cuerpo_data_list = Region.fetch_all_related(cursor, region.old_id) for cuerpo_data in cuerpo_data_list: # try first to get cuerpo from our records try: cuerpo = Cuerpo.objects.get(old_id=cuerpo_data["id"]) cuerpo_list.append(cuerpo) except Cuerpo.DoesNotExist: cuerpo = Cuerpo.fetch_from_db(cursor, cuerpo_data["id"]) if cuerpo: # is null in case of virtual cuerpo or inconsistent db cuerpo.save() cuerpo_list.append(cuerpo) return render_to_response('administrator/results_cuerpo.html', { 'form': form, 'cuerpo_list': cuerpo_list, 'menu_titles': True, }, context_instance=RequestContext(request))
def authenticate(self, username=None, password=None): user = None cursor = connections['postfix'].cursor() # username needs to be transformed into email # reference: http://stackoverflow.com/questions/3217682/checking-validity-of-email-in-django-python try: validate_email(username) except ValidationError: username = u'@'.join((username, u'bomberos.cl',)) query = "SELECT correopk FROM mailbox WHERE username = %s AND password = %s" params = (username, password,) cursor.execute(query, params) postfix_data = cursor.fetchone() if postfix_data is None: # The username/password do not match logging.info("Username/password for user %s do not match", username) cursor.close() return None try: user = User.objects.get(username=username) except User.DoesNotExist: logging.info("The user (%s) is logging for the first time, initializing data", username) user = User(username=username, password=password) user.is_staff = False user.is_superuser = False user.save() profile = user.get_profile() # If for some reason the user does not have a uid in the database, burn everything and abort if postfix_data[0] is None: logging.info("User %s does not have a correopk", username) profile.delete() user.delete() cursor.close() return None correo_pk = postfix_data[0] # Try and get the user_id from usu_mail query = "SELECT fk_usu FROM mail_usu WHERE fk_mail = %s" params = (correo_pk,) cursor.execute(query, params) user_data = cursor.fetchone() if not user_data: logging.info("User %s with correopk %s does not exist in table 'mail_usu'" % (username, correo_pk)) profile.delete() user.delete() cursor.close() return None profile.old_id = user_data[0] cursor.close() cursor = connections['principal'].cursor() # First of all, determine user role profile.determine_role(cursor) # If user is not reg. op. manager, fetch company if profile.is_regional_operations_manager(): cuerpos = Region.fetch_all_related(cursor, profile.get_region_id()) for cuerpo_data in cuerpos: cuerpo = Cuerpo.fetch_from_db(cursor, cuerpo_data["id"]) if cuerpo: # is null in case of virtual cuerpo or inconsistent db cuerpo.save() else: # Try and get the user company query = "SELECT usu_fk_cia FROM usuarios WHERE usu_id = %s" params = (profile.old_id,) cursor.execute(query, params) user_data = cursor.fetchone() if not user_data: # The user does not exist in the principal database (broken foreign key) # As always, burn and quit logging.error("User %s with id %s does not exist in table 'usuarios'", username, profile.old_id) profile.delete() user.delete() cursor.close() return None old_company_id = user_data[0] company = Company.fetch_from_db(cursor, old_company_id) if not company: logging.error("User %s associated company is foreign key broken (in commune, province, region, or cuerpo)", username) profile.delete() user.delete() cursor.close() return None profile.company = company profile.save() user.save() cursor.close() return user
def authenticate(self, username=None, password=None): user = None cursor = connections['postfix'].cursor() # username needs to be transformed into email # reference: http://stackoverflow.com/questions/3217682/checking-validity-of-email-in-django-python try: validate_email(username) except ValidationError: username = u'@'.join(( username, u'bomberos.cl', )) query = "SELECT correopk FROM mailbox WHERE username = %s AND password = %s" params = ( username, password, ) cursor.execute(query, params) postfix_data = cursor.fetchone() if postfix_data is None: # The username/password do not match logging.info("Username/password for user %s do not match", username) cursor.close() return None try: user = User.objects.get(username=username) except User.DoesNotExist: logging.info( "The user (%s) is logging for the first time, initializing data", username) user = User(username=username, password=password) user.is_staff = False user.is_superuser = False user.save() profile = user.get_profile() # If for some reason the user does not have a uid in the database, burn everything and abort if postfix_data[0] is None: logging.info("User %s does not have a correopk", username) profile.delete() user.delete() cursor.close() return None correo_pk = postfix_data[0] # Try and get the user_id from usu_mail query = "SELECT fk_usu FROM mail_usu WHERE fk_mail = %s" params = (correo_pk, ) cursor.execute(query, params) user_data = cursor.fetchone() if not user_data: logging.info( "User %s with correopk %s does not exist in table 'mail_usu'" % (username, correo_pk)) profile.delete() user.delete() cursor.close() return None profile.old_id = user_data[0] cursor.close() cursor = connections['principal'].cursor() # First of all, determine user role profile.determine_role(cursor) # If user is not reg. op. manager, fetch company if profile.is_regional_operations_manager(): cuerpos = Region.fetch_all_related(cursor, profile.get_region_id()) for cuerpo_data in cuerpos: cuerpo = Cuerpo.fetch_from_db(cursor, cuerpo_data["id"]) if cuerpo: # is null in case of virtual cuerpo or inconsistent db cuerpo.save() else: # Try and get the user company query = "SELECT usu_fk_cia FROM usuarios WHERE usu_id = %s" params = (profile.old_id, ) cursor.execute(query, params) user_data = cursor.fetchone() if not user_data: # The user does not exist in the principal database (broken foreign key) # As always, burn and quit logging.error( "User %s with id %s does not exist in table 'usuarios'", username, profile.old_id) profile.delete() user.delete() cursor.close() return None old_company_id = user_data[0] company = Company.fetch_from_db(cursor, old_company_id) if not company: logging.error( "User %s associated company is foreign key broken (in commune, province, region, or cuerpo)", username) profile.delete() user.delete() cursor.close() return None profile.company = company profile.save() user.save() cursor.close() return user