def process_xml_file(self, filename): print('Buscando problemas en {}\n'.format(filename)) try: fileCfdi = CfdiXml(filename) rfcs = [fileCfdi.get_rfc_emisor(), fileCfdi.get_rfc_receptor()] blacklist = Blacklist.find_by_rfcs(rfcs=rfcs) e = 0 if blacklist: for bl in blacklist: archivo = bl.get('defi') and 'Definitivos' or 'Presuntos' e = 1 line = 'Problema con archivo {}\nRFC: {}\nPersona {}\n' \ 'Línea {} del archivo de {} del SAT\n' \ .format( filename, bl.get('rfc'), bl.get('name'), bl.get('csv_line'), archivo) else: line = 'Ningún problema detectado.' except: e = 1 line = 'Error de Lectura en el archivo:\n{}'.format(filename) print(line) print('-----------------------------------------\n') return e, line
def validate_friend_request(sender_id, reciever_id): '''Return false if three request made, unresponded request exists, any request was already accepted or user is blocked.''' if sender_id == reciever_id: return False if Blacklist(app.config['blacklists_location'], reciever_id).find_user(sender_id): return False all_requests = FriendRequest(get_db()).get_requests_by_sender_and_reciever( sender_id, reciever_id) if len(all_requests) >= 3: return False #add requests send by reciever to sender all_requests.extend( FriendRequest(get_db()).get_requests_by_sender_and_reciever( reciever_id, sender_id)) for request in all_requests: if request['status'] == 0: return False for request in all_requests: if request['status'] == 1: return False return True
def setUp(self): self.client = mock_strict_redis_client() self.encoder = Mock() self.encoder.encode = self.reverse_string self.blacklist = Blacklist(self.client, self.encoder) self.poll_texts_key_name = "blacklist:poll_texts:231" self.poll_contacts_key_name = "blacklist:poll_contacts:231"
def post(request): # Confirm we're dealing with JSON input and that we have the # required fields if not request.json: abort(400, 'Request is not JSON') missing_fields = __get_missing_fields(request.json) if len(missing_fields) > 0: abort(400, 'Request missing the following fields: ' + ', '.join(missing_fields)) # Check the blacklist DB for the email address and throw an error # if we find it there blacklist = Blacklist() try: if blacklist.is_on_blacklist(request.json['to']): abort(400, 'Email addressee was found on blacklist') except SqlError as e: abort(500, 'Failed to read from database') email_sender = EmailIntegrationFactory.get_integration(settings['email_integration'], request.json) return make_response(jsonify(email_sender.get_schema()), 202)
def post(request): # Confirm we're dealing with JSON input and that we have the # required fields if not request.json: abort(400, 'Request is not JSON') missing_fields = __get_missing_fields(request.json) if len(missing_fields) > 0: abort(400, 'Request missing the following fields: ' + ', '.join(missing_fields)) # Check the blacklist DB for the email address and throw an error # if we already find it there (UNIQUE constraint) blacklist = Blacklist() try: last_row = blacklist.add_email(request.json['email_address']) except IntegrityError as e: abort(400, 'Email address already exists in database') except SqlError: abort(500, 'Failed to write to database') return make_response(jsonify({'id': last_row, **request.json}), 201)
def load_csv(filename, **kwargs): with session_scope() as s: header_row = kwargs.get('header_row') or 2 rfc_col = kwargs.get('rfc_col') or 1 name_col = kwargs.get('name_col') or 2 def_col = kwargs.get('def_col') or 3 pre_fecha_col = kwargs.get('pre_fecha_col') or 5 pre_fecha_fmt = kwargs.get('pre_fecha_fmt') or "%d/%m/%Y" def_fecha_col = kwargs.get('def_fecha_col') or False def_fecha_fmt = kwargs.get('def_fecha_fmt') or False with open(filename, 'rb') as f: try: encoding = chardet.detect(f.read()).get('encoding') except Exception: encoding = "utf-8" with open(filename, encoding=encoding) as f: csv_reader = csv.reader(f, delimiter=',') line_count = 0 for row in csv_reader: if line_count > header_row and ( 'XXXXX' not in row[rfc_col] and row[rfc_col] not in ''): data = { 'rfc': row[rfc_col], 'name': row[name_col], 'csv_line': line_count + 1, } if row[def_col] in ['Definitivo', 'DEFINITIVO', 'definitivo']: data['defi'] = True else: data['defi'] = False # Fecha Publicación Página del SAT Presuntos if row[pre_fecha_col]: try: data['pre_fecha'] = datetime.strptime(row[pre_fecha_col], pre_fecha_fmt) except Exception: print('Error Fecha Presuntos Fila {}'.format(line_count + 1)) raise if def_fecha_col and row[def_fecha_col]: try: data['def_fecha'] = datetime.strptime(row[def_fecha_col], def_fecha_fmt) except Exception: print('Error Fecha Definitivos Fila {}'.format(line_count + 1)) print("Adding {}-{}".format(data.get('rfc'), data.get('name'))) blacklist = Blacklist(**data) s.add(blacklist) line_count += 1
def logout(): user = logout.user_id if not isinstance(user, str): blacklist_token = Blacklist(logout.auth_token) try: blacklist_token_repository.save(blacklist_token) response = { 'status': 'success', 'message': 'Logged out successfully' } return make_response(jsonify(response)), 200 except Exception as e: response = { 'status': 'fail', 'message': e } return make_response(jsonify(response)), 200 else: response = { 'status': 'fail', 'message': user } return make_response(jsonify(response)), 401
def send_friend_request_response(id): db = get_db() answer = request.json['answer'] request_id = request.json['request_id'] friend_request = FriendRequest(db).get_request_by_id(request_id) user = User(db) current_user = user.get_user_by_username(session['username']) friend_id = request.json['user_id'] friend = user.get_user(friend_id) if friend_request is not None: if answer == 1: FriendRequest(db).update_status(request_id, 1) Room(db).create_room(current_user['user_id'], friend_id) new_room = Room(db).get_users_room(current_user['user_id'], friend_id) response = { 'message': 'Response accepted.', 'answer': True, 'request_id': request_id, 'room': new_room['room_name'] } user_button = render_template('friend_button.html', friend=current_user) friend_button = render_template('friend_button.html', friend=friend) socketio.emit('friend request answer', (response, user_button, current_user['user_id']), room=friend['room']) socketio.emit('friend request answer', (response, friend_button, friend_id), room=current_user['room']) elif answer == 2: FriendRequest(db).update_status(request_id, 2) response = { 'message': 'Response rejected.', 'answer': False, 'request_id': request_id } socketio.emit('friend request answer', response, room=friend['room']) elif answer == 3: FriendRequest(db).update_status(request_id, 2) Blacklist(app.config['blacklists_location'], current_user['user_id']).add_user(friend_id) response = { 'message': 'User blocked.', 'answer': False, 'request_id': request_id } socketio.emit('friend request answer', response, room=friend['room']) else: response = {'error': 'something goes wrong'} #check if user have more unresponded requests awaiting_friend_request = FriendRequest(db).get_waiting_request( current_user['user_id']) if awaiting_friend_request is not None: sender = User(db).get_user(awaiting_friend_request['sender_id']) emit_friend_request(sender, current_user['room'], awaiting_friend_request['request_id']) return jsonify(**response)
class TestBlacklist(TestCase): def setUp(self): self.client = mock_strict_redis_client() self.encoder = Mock() self.encoder.encode = self.reverse_string self.blacklist = Blacklist(self.client, self.encoder) self.poll_texts_key_name = "blacklist:poll_texts:231" self.poll_contacts_key_name = "blacklist:poll_contacts:231" def reverse_string(self, text): return text[::-1] def test_that_poll_text_can_be_black_listed(self): self.blacklist.poll_text(231, "sample text") self.assertTrue(self.client.sismember(self.poll_texts_key_name, self.reverse_string("sample text"))) def test_that_an_array_of_poll_texts_can_be_black_listed(self): self.blacklist.poll_text(231, ["sample 1", "sample 2", "sample 3"]) self.assertTrue(self.client.sismember(self.poll_texts_key_name, self.reverse_string("sample 1"))) self.assertTrue(self.client.sismember(self.poll_texts_key_name, self.reverse_string("sample 2"))) self.assertTrue(self.client.sismember(self.poll_texts_key_name, self.reverse_string("sample 3"))) self.assertFalse(self.client.sismember(self.poll_texts_key_name, self.reverse_string("sample 4"))) def test_that_only_inserted_text_exist(self): self.blacklist.poll_text(231, "valid text") self.assertFalse(self.blacklist.has_poll_text(231, "invalid text")) self.assertTrue(self.blacklist.has_poll_text(231, "valid text")) def test_that_poll_text_can_be_deleted(self): text_to_delete = "text to delete" self.blacklist.poll_text(231, text_to_delete) self.blacklist.poll_text(231, "text not to delete") self.assertTrue(self.blacklist.has_poll_text(231, text_to_delete)) self.blacklist.delete_poll_text(231, text_to_delete) self.assertFalse(self.blacklist.has_poll_text(231, text_to_delete)) self.assertTrue(self.blacklist.has_poll_text(231, "text not to delete")) def test_that_poll_contacts_can_be_black_listed(self): ones = "1111111" twos = "2222222" threes = "33333" self.blacklist.poll_contacts(231, [ones, twos]) self.assertTrue(self.client.sismember(self.poll_contacts_key_name, ones)) self.assertTrue(self.client.sismember(self.poll_contacts_key_name, twos)) self.assertFalse(self.client.sismember(self.poll_contacts_key_name, threes)) def test_that_inserted_poll_contact_exists(self): ones = "1111111" self.blacklist.poll_contacts(231, ones) self.assertTrue(self.blacklist.has_poll_contact(231, ones)) def test_that_poll_contacts_can_be_deleted(self): ones = "1111111" twos = "2222222" threes = "33333" self.blacklist.poll_contacts(231, [ones, twos, threes]) self.assertTrue(self.blacklist.has_poll_contact(231, ones)) self.assertTrue(self.blacklist.has_poll_contact(231, twos)) self.assertTrue(self.blacklist.has_poll_contact(231, threes)) self.blacklist.delete_poll_contacts(231, [twos,threes]) self.assertTrue(self.blacklist.has_poll_contact(231, ones)) self.assertFalse(self.blacklist.has_poll_contact(231, twos)) self.assertFalse(self.blacklist.has_poll_contact(231, threes))
def post(self): try: requestDict = request.get_json() if not requestDict: response = {'error': 'No input data provided'} return response, status.HTTP_400_BAD_REQUEST dni = requestDict['dni'] reason = requestDict['reason'] blackLists = Blacklist.query.all() for bl in blackLists: if (dni == bl.documentNumber): response = { 'error': 'Esta persona ya se encuentra en la blackList' } return response, status.HTTP_400_BAD_REQUEST blacklistClassification = BlacklistClassification.query.filter_by( description=reason).first() if (blacklistClassification is None): #Si no está la clasificación blacklistClassification = BlacklistClassification( name="Regular", description=reason, active=1) blacklistClassification.add(blacklistClassification) db.session.flush() blacklist = Blacklist( documentType="DNI", documentNumber=dni, active=1, idBlacklistClassification=blacklistClassification.id) blacklist.add(blacklist) db.session.commit() response = {'ok': 'Añadido a la Blacklist correctamente'} return response, status.HTTP_201_CREATED blClassification = BlacklistClassification.query.filter_by( description=reason).first() blClassification = blacklistClassification.toJson() blacklist = Blacklist(documentType="DNI", documentNumber=dni, active=1, idBlacklistClassification=blClassification[ 'idBlacklistClassification']) blacklist.add(blacklist) db.session.commit() response = {'ok': 'Añadido a la Blacklist correctamente'} return response, status.HTTP_201_CREATED except SQLAlchemyError as e: db.session.rollback() response = {'error': str(e)} return response, status.HTTP_400_BAD_REQUEST except Exception as e: db.session.rollback() response = { 'error': 'An error ocurred. Contact cat-support asap. ' + str(e) } return response, status.HTTP_400_BAD_REQUEST
def post(self): try: file = request.files['file'] if file.filename == '': response = {'error': 'No selected files'} return response, status.HTTP_400_BAD_REQUEST if file and allowed_file(file.filename): df = None # [documentNumber], fatherLastname, motherLastname, name, sex, [birthDate] try: df = pd.read_csv(file, header=None) except: df = pd.read_excel(file, header=None) n = df[0].size response = {} response['badIndexes'] = [] response['badReasons'] = [] for i in range(n): documentNumber = df[0][i] fatherLastname = df[1][i] motherLastname = df[2][i] name = df[3][i] sex = df[4][i] birthDate = df[5][i] logging.debug(df[0][i], df[1][i], df[2][i], df[3][i], df[4][i], df[5][i]) #Obtener firstName y middleName listNames = [x for x in name.split()] firstName = listNames[0] middleName = "" if len(listNames) > 1: middleName = listNames[1] if isinstance(documentNumber, float): if np.isnan(documentNumber): documentNumber = None else: documentNumber = str(int(documentNumber)) documentNumber = fixDocumentNumber(documentNumber) else: documentNumber = fixDocumentNumber(documentNumber) logging.debug(birthDate, type(birthDate)) if isinstance(birthDate, float): if np.isnan(birthDate): logging.debug("isnan") birthDate = datetime.now() - timedelta(days=7300) if isinstance(birthDate, str): lstDate = birthDate.split("/") dd, mm, yyyy = lstDate[0], lstDate[1], lstDate[2] birthDate = date(yyyy, mm, dd) if not isinstance(birthDate, datetime): birthDate = datetime.now() - timedelta(days=7300) if pd.isnull(birthDate): logging.debug("ES NAT") birthDate = birthDate = datetime.now() - timedelta( days=7300) logging.debug("IMPRIMIENTO BD") logging.debug(birthDate) if documentNumber: blacklist = Blacklist.query.filter_by( documentNumber=documentNumber).first() if blacklist: logging.debug("esta repetido xd") response['badIndexes'].append(i) response['badReasons'].append( "Usuario ya registrado en blacklist") else: blacklist = Blacklist( documentNumber=documentNumber, documentType=getDocumentType(documentNumber), active=1, idBlacklistClassification=1) blacklist.add(blacklist) person = Person.query.filter_by( documentNumber=documentNumber).first() if not person: person = Person( documentNumber=documentNumber, documentType=blacklist.documentType, fatherLastname=fatherLastname, motherLastname=motherLastname, firstName=firstName, middleName=middleName, birthdate=birthDate, address= "Av. Universitaria 1801, Lima 15108, Peru", nationality="per", vehicle1Plate="XXX000", vehicle2Plate="XXX000", gender="M") person.add(person) else: person = Person.query.filter_by( firstName=firstName, motherLastname=motherLastname, fatherLastname=fatherLastname).first() logging.debug("xd") if person: logging.debug( "Encontro persona, revisando si hay blacklist registrado" ) blacklist = Blacklist.query.filter_by( documentNumber=person.documentNumber).first() logging.debug(blacklist) if blacklist: logging.debug("esta repetido xd x2") response['badIndexes'].append(i) response['badReasons'].append( "Usuario ya registrado en blacklist") else: blacklist = Blacklist( documentNumber=person.documentNumber, documentType=person.documentType, active=1, idBlacklistClassification=1) blacklist.add(blacklist) else: response['badIndexes'].append(i) response['badReasons'].append( "Usuario sin match en la base de datos") logging.debug("todo bien") db.session.commit() response['ok'] = 'Registros agregados correctamente' return response, status.HTTP_200_OK else: response = {'error': 'Bad file sent. Please check extension.'} return response, status.HTTP_400_BAD_REQUEST except SQLAlchemyError as e: db.session.rollback() response = {'error': str(e)} return response, status.HTTP_400_BAD_REQUEST except Exception as e: db.session.rollback() response = {'error': str(e)} logging.debug(str(e)) return response, status.HTTP_400_BAD_REQUEST