def __init__(self, scan_dir, work_dir): self.recog = Recog.recog16() self.store = Store() self.plot = Plot() self.work_dir = work_dir self.scanner = Scanner(scan_dir, work_dir) self.main_process()
def spawn_engagers(self, providers, entity_type, entity_json_string, force, force_save): self.logger.info('Started spawning engagers flows.') # Iterate over all required providers and launch enrichment result_futures = {} for provider_name in providers: try: result_futures[provider_name] = self.launcher.launch.call_async(provider_name, entity_type, entity_json_string, force) self.logger.info('Launched (async) engagement with %s', provider_name) except EngagementException as ee: self.logger.error('Failed to engage via %s on entity <to specify...> (exception: %s)', provider_name, ee) self.logger.info('Completed spawning all engagers.') engagement_results = {} for provider_name in result_futures.keys(): try: res = result_futures[provider_name].result() engagement_results[provider_name] = EngagementResult.from_json_string(res) self.logger.info('Done collecting results of provider %s', provider_name) except Exception as ee: self.logger.info('Exception raised getting future result of %s: %s. Ignoring this result.', provider_name, ee) self.logger.info('Completed collecting results from all engagers') # Recreate entity new_entity = self._get_entity_by_type(entity_type) entity = new_entity.from_json_string(entity_json_string) self.logger.info('Done recreating entity from json - %s', entity) # Merge all results into entity changed = False redigest_properties = {} for provider_name, engagement_result in engagement_results.items(): if engagement_result.status != EngagementResult.SKIPPED and engagement_result.status != EngagementResult.NOCHANGE: enrich_key = engagement_result.properties_changed['enrich_key'] for k, v in engagement_result.properties_changed.items(): property_changed = entity.set_data(provider_name, enrich_key, k, v) if property_changed and k in LISTS.TRIGGERING_PROPERTIES: redigest_properties[k] = v changed |= property_changed self.logger.info('Done merging properties of %s. Changed = %s', provider_name, changed) pass if changed or force_save: entity.last_update = datetime.datetime.now() # Digest and store entity.digest() Store.set_entity(entity_type, entity) self.logger.info('Stored in Store! (changed=%s, force_save=%s)', changed, force_save) # Redigest other entities self.redigest(redigest_properties) else: self.logger.info('Not stored. No change detected') pass
def new_answer(text, question, user): store = Store() path = '/answers' body = { 'question_id': question.id, 'text': text, } res = store.post(path, body, auth=True) if res.get('error'): return JsonResponse({ 'ok': False, 'msg': f'{res.get("error")}:{res.get("message")}' }) Answer.objects.create(user=user, text=text, question=question, created_date=timezone.localtime()) return JsonResponse({ 'ok': True, 'msg': 'Respuesta Creada correctamente', })
def get_token(request): query = request.GET code = query.get('code') jwt_split = code.split('-') seller_id = jwt_split[2] store = Store(seller_id) store.authorize(code, Store.URI_CALLBACK) return HttpResponse('Autenticacion Exitosa!')
def person_to_person(): """ Get a paths from source person to target person (by IDs) This endpoint returns all paths leading from source person to target company via a referral --- tags: - paths parameters: - name: source_id in: query type: string description: source id of path - name: target_id in: query type: string description: target id of path responses: 200: description: A list of paths sorted by strength. Each path contains array of segments. Each segment is made of [seg-start, relation-type, seg-end] schema: type: array items: properties: source_id: type: string description: The source id of the relation relation_type: type: string description: The type of the relation (e.g. EMPLOYEE_OF, TWITTER_FRIEND, etc.) target_id: type: string description: The target id of the relation """ # Get source/target ids from request source_id = request.args.get('source_id', None) if source_id is None: return 'Missing source id parameter', 400 target_id = request.args.get('target_id', None) if target_id is None: return 'Missing target id parameter', 400 # Check that source/target exist if Store.get_person_by_aid(source_id) is None: return 'No person matching source id', 400 if Store.get_person_by_aid(target_id) is None: return 'No person matching target id', 400 try: paths = Store.get_paths_to_person(source_id, target_id) except Exception as e: tb = traceback.format_exc() return 'Exception %s raised trying to get path. %s' % (e, tb), 500 # Return the paths as json with code 200 response = app.response_class(response=json.dumps(paths), status=200, mimetype='application/json') return response
def handle(self, *args, **options): seller_id = options['seller_id'] store = Store(seller_id=seller_id) products = ProductForStore.objects.filter( status__in=(ProductForStore.ACTIVE, ProductForStore.UNDER_REVIEW)) ids = products.values_list('sku', flat=True) total = len(ids) store.update_items(ids, [{'status': 'paused'}] * total) products.update(status=ProductForStore.PAUSED) logging.getLogger('log_three').info(f'{total} Productos Pausados')
def handle(self, *args, **options): seller_id = options['seller_id'] store = Store(seller_id=seller_id) products = ProductForStore.objects.filter(store_id=seller_id).exclude( sku=None) ids = products.values_list('sku', flat=True) params = [{ 'ids': _ids, 'attributes': 'status,id' } for _ids in store.split_ids(ids)] path = 'items' results = store.map_pool_get([path] * len(params), params) posts = defaultdict(list) for product in [_ for _ in results if _]: if product['code'] == 200: if product['body']['status'] == 'closed': posts['deleted'].append(product['body']['id']) elif product['body']['status'] == 'active': posts['active'].append(product['body']['id']) elif product['body']['status'] == 'paused': posts['paused'].append(product['body']['id']) elif product['body']['status'] == 'under_review': posts['under_review'].append(product['body']['id']) elif product['body']['status'] == 'inactive': posts['inactive'].append(product['body']['id']) else: logging.getLogger('log_three').info(product) logging.getLogger('log_three').info( f"{len(posts['deleted'])} Productos eliminados recientemente.") ProductForStore.objects.filter(sku__in=posts['deleted']).update( status=ProductForStore.CLOSED) logging.getLogger('log_three').info( f"{len(posts['active'])} Productos activos.") ProductForStore.objects.filter(sku__in=posts['active']).update( status=ProductForStore.ACTIVE) logging.getLogger('log_three').info( f"{len(posts['paused'])} Productos pausados.") ProductForStore.objects.filter(sku__in=posts['paused']).update( status=ProductForStore.PAUSED) logging.getLogger('log_three').info( f"{len(posts['under_review'])} Productos bajo revision de ML.") ProductForStore.objects.filter(sku__in=posts['under_review']).update( status=ProductForStore.UNDER_REVIEW) logging.getLogger('log_three').info( f"{len(posts['inactive'])} Productos inactivos por revision de ML." ) ProductForStore.objects.filter(sku__in=posts['inactive']).update( status=ProductForStore.UNDER_REVIEW)
def spawn_engagers_sequentially(self, providers, entity_type, entity, enrichment_behavior, enriched=False): org_entity = copy.deepcopy(entity) # Iterate over all required providers and run enrichment engagement_results = {} el = EngagerLauncher() for provider_name in providers: try: res = el.launch(provider_name, entity_type, entity, enrichment_behavior.force) engagement_results[provider_name] = EngagementResult.from_json_string(res) except EngagementException as ee: self.logger.error('Failed to engage via %s on entity %s (exception: %s)', provider_name, entity.aid, ee) # Recreate entity new_entity = org_entity # Merge all results into entity #changed = False changed = enriched redigest_properties = {} for provider_name, engagement_result in engagement_results.items(): if engagement_result.status != EngagementResult.SKIPPED and engagement_result.status != EngagementResult.NOCHANGE: enrich_key = engagement_result.properties_changed['enrich_key'] for k, v in engagement_result.properties_changed.items(): property_changed = new_entity.set_data(provider_name, enrich_key, k, v) if property_changed and k in LISTS.TRIGGERING_PROPERTIES: redigest_properties[k] = v changed |= property_changed self.logger.info('Done merging properties of %s. Changed = %s', provider_name, changed) pass if changed or enrichment_behavior.force_save: new_entity.last_update = datetime.datetime.now() new_entity.digest() Store.set_entity(entity_type, new_entity) msg = 'Stored in Store! (changed=%s, force_save=%s)' % (changed, enrichment_behavior.force_save) # Redigest other entities self.redigest(redigest_properties) else: msg = 'Not stored. No change detected' self.logger.info(msg) # Prepare information to send to webhook if enrichment_behavior.webhook: payload = {'status_message': msg, 'status_code': 200, 'ts': time.time(), 'aid': new_entity.aid} r = AcureRateUtils.announce(enrichment_behavior.webhook, payload) self.logger.info('Done merging enrichment result into entity. Changed = %s', changed)
def tests_store(self): parts = np.array( pd.read_csv("datasets/parts.csv", header=None, sep=",")) samples = parts[np.random.randint(0, parts.shape[0], 20)] # print(samples) store = Store() for sample in samples: place_id = store.store(sample[0], np.random.rand(1, 10)) part_id, count = store.lookup(place_id) # if count > 0: # print("Test case part {}: Success!", sample) # else: # print("Test case part {}: Failure!", sample) # self.assertTrue(count > 0) self.assertTrue(False)
def circles_people(): """ Get a list of circles the person is part of (by IDs) This endpoint returns all circles of a person --- tags: - circles parameters: - name: person_id in: query type: string description: source id of path responses: 200: description: A list of circles of a person schema: id: return_test properties: props: type: string description: The test default: 'test' result: type: string description: The test default: 'test' """ # @@@ person_id = request.json.get('person_id') print('Get circle list of person %s' % person_id) circles = Store.get_circles(person_id)
class Runner: def __init__(self, scan_dir, work_dir): self.recog = Recog.recog16() self.store = Store() self.plot = Plot() self.work_dir = work_dir self.scanner = Scanner(scan_dir, work_dir) self.main_process() def process(self, imgpath, amount): place_id = ''.join(self.recog.recog(imgpath, box_view=True)) print(place_id) parts_id = self.store.store(place_id, amount) self.plot.plotAt(parts_id) self.plot.plot() def get_imgpath(self, imgpath): return ''.join([self.work_dir, "/", imgpath]) def main_process(self): while True: if self.scanner.have_imgname: imgname = self.scanner.get_imgname() print("new image load: ", imgname) self.process(self.get_imgpath(imgname), 1) sleep(1)
def person_relations_by_id(person_id): """ Get a person relations by ID This endpoint returns all relations of a person by a given ID in a list format --- tags: - person parameters: - name: person_id in: path type: string description: id of person required: true responses: 200: description: Returns a list of relations schema: type: array items: properties: source_id: type: string description: The source id of the relation relation_type: type: string description: The type of the relation (e.g. EMPLOYEE_OF, TWITTER_FRIEND, etc.) target_id: type: string description: The target id of the relation reltion_properties: type: string description: String with comma-separated key:value properties of this relation 400: description: Bad request 404: description: Person not found """ person = Store.get_person_by_aid(person_id) if person is None: return 'Person with id %s not found' % person_id, 404 relations = person.get_relations() data = [] for source_aid, relation_type, target_aid, relation_properties in relations: # TODO: move relation_properties from string to array data_element = { 'relation_type': relation_type, 'relation_properties': relation_properties, 'source_id': source_aid, 'target_id': target_aid } data.append(data_element) response = app.response_class(response=json.dumps(data), status=200, mimetype='application/json') return response
def get_or_create_buyer(buyer_id: int, buyer_draw=dict()): store = Store() buyer = Buyer.objects.filter(id=buyer_id).first() if buyer and ((not buyer_draw) or (buyer.first_name)): return buyer if not buyer_draw: path = '/sites/MLV/search' params = {'seller_id': buyer_id} buyer_api = store.get(path, params) nickname = buyer_api['seller']['nickname'] buyer = Buyer.objects.create(id=buyer_id, nickname=nickname) else: phone_draw = buyer_draw.get('phone') phone = '' if phone_draw.get('area_code'): phone += phone_draw.get('area_code').replace(' ', '') if phone_draw.get('number'): phone += phone_draw.get('number').replace('-', '').replace(' ', '') if len(phone) > 5: phone = int(phone) else: phone = 0 if not buyer: buyer = Buyer.objects.create( id=buyer_id, nickname=buyer_draw.get('nickname'), phone=phone, first_name=buyer_draw.get('first_name'), last_name=buyer_draw.get('last_name'), ) else: buyer.phone = phone buyer.nickname = buyer_draw.get('nickname') buyer.first_name = buyer_draw.get('first_name') buyer.last_name = buyer_draw.get('last_name') buyer.save() logging.getLogger('log_three').info( f'Comprador Actualizado/Registrado: {buyer}') return buyer
class Main(object): def __init__(self): self.store = Store() def run(self): print('[INFO] 正在获取教师链接...并区分第一系列') get1 = Get_url() get1.run() print('[INFO] 正在获取教师链接...并区分第二系列') get2 = Get_url2() get2.run() print('[INFO] 正在获取教师链接...并区分第三系列') get3 = Get_url3() get3.run() print('[INFO] 正在获取教师链接...并区分第四系列') get4 = Get_url4() get4.run() print('[INFO] 正在获取教师链接...并区分第五系列') get5 = Get_url5() get5.run() print('[INFO] 正在爬取第一系列...') one = OneSpider(self.store) one.run() print('[SUCCESS] 爬取成功! 一共 {} 名...'.format(self.store.count)) print('[INFO] 正在爬取第二系列...') two = TwoSpider(self.store) two.run() print('[SUCCESS] 爬取成功! 一共 {} 名...'.format(self.store.count)) print('[INFO] 正在爬取第三系列...') three = ThreeSpider(self.store) three.run() print('[SUCCESS] 爬取成功! 一共 {} 名...'.format(self.store.count)) print('[INFO] 正在爬取第四系列...') four = FourSpider(self.store) four.run() print('[SUCCESS] 爬取成功! 一共 {} 名...'.format(self.store.count)) print('[INFO] 正在爬取第五系列...') five = FiveSpider(self.store) five.run() self.store.save() print('[SUCCESS] 爬取成功! 一共 {} 名...'.format(self.store.count))
def company_relations_by_domain(): """ Get a company relations by DOMAIN This endpoint returns all properties of a company by a given DOMAIN in a key/value fashion --- tags: - company parameters: - name: domain in: query type: string description: domain of a company required: true - name: filter in: query type: string description: relation type to use as filter (case-insensitive) responses: 200: description: A single company item schema: properties: property-1: type: string description: A property default: 'value-1' property-2: type: string description: A property default: 'value-2' property-N: type: string description: A property default: 'value-N' 400: description: Bad request. Missing/wrong parameter. 404: description: Company not found """ domain = request.args.get('domain', None) if domain is None: return 'No domain provided. Mandatory parameter', 400 company = Store.get_company({"domain": domain}) if company is None: return 'No company with domain %s found' % domain, 404 filter = request.args.get('filter', None) data = company.get_relations(filter) response = app.response_class(response=json.dumps(data), status=200, mimetype='application/json') return response
def handle(self, *args, **options): seller_id=config('MELI_ME_ID') store = Store(seller_id=seller_id) path = '/questions/search' params = { 'seller_id':seller_id, 'status':'UNANSWERED', 'offset':0, } questions_api = store.get(path, params) total = questions_api['total'] limit = questions_api['limit'] questions_draw = questions_api['questions'] params = [{ 'seller_id':seller_id, 'status':'UNANSWERED', 'offset':offset, 'attributes':'questions' }for offset in range(limit,total,limit)] for questions_api in store.map_pool_get([path]*len(params), params): questions_draw += questions_api['questions'] questions_draw = questions_draw[::-1] count=0 for question_draw in questions_draw: question,exist = new_question(question_draw) if exist: break else: logging.getLogger('log_three').info(f'Nueva Pregunta de {question.buyer} en \ {question.product.title} a las {question.date_created.strftime("%H:%M:%S")}') count +=1 logging.getLogger('log_three').info(f'Nuevas Preguntas: {count}. Sin responder: {total}')
def check_company(self, name, domain): if domain == 'angel.co' or domain == 'itunes.apple.com': return # Check if appears in database #q = {'deduced.aliases': name} q = {'deduced.domain': domain} cursor = Store.get_entities('company', q, mongo_query=True) for entity in cursor: self.found += 1 self.logger.info('Found %s in our DB (so far found %d)', name, self.found) pass
def handle(self, *args, **options): limit = options['limit'] seller_id = options['seller_id'] start = datetime.now() logging.getLogger('log_three').info( 'Aplicando filtro de malas palabras a productos') filter_bad_products(seller_id) logging.getLogger('log_three').info( 'Filtrado completado, tiempo de ejecucion {:.2f} seg'.format( (datetime.now() - start).total_seconds())) store = Store(seller_id=seller_id) start = datetime.now() logging.getLogger('log_three').info('Consultando la base de datos') products = ProductForStore.objects.exclude(sku=None).filter( product__quantity__gt=0, product__available=True, status=ProductForStore.PAUSED, sale_price__gt=0, store_id=seller_id)[:limit] logging.getLogger('log_three').info( 'Fin de la consulta, tiempo de ejecucion {:.2f} seg'.format( (datetime.now() - start).total_seconds())) ids = products.values_list('sku', flat=True) total = products.count() results = store.update_items(ids, [{'status': 'active'}] * total) posts_active = list() for product in results: if product.get('status') == 'active': posts_active.append(product['id']) else: logging.getLogger('log_three').warning( f'Producto no actualizado: {product}') ProductForStore.objects.filter(sku__in=posts_active).update( status=ProductForStore.ACTIVE) logging.getLogger('log_three').info( f"{len(posts_active)} Productos activados.")
def redigest(self, redigest_properties): if True: return self.logger.info('Started redigest according to these properties: %s', redigest_properties) # TODO: fix the below to work also with companies - be agnostic to the entity type! # Run a query to get all those entities who have the value in the field "unrecognized_<prop>": for k, v in redigest_properties.items(): if k == 'name': # TODO: temp code. Need to align Mongo properties and all is well... k = 'companies' q = {'deduced.unrecognized_%s' % k: v} cursor = Store.get_entities('people', q, mongo_query=True) for entity in cursor: entity.digest() Store.set_entity('people', entity) self.logger.info('Done redigesting entities of property %s', k) self.logger.info('Done redigesting all entities.') pass
def person_properties_by_email(): """ Get a person properties by email This endpoint returns all properties of a person by a given EMAIL in a key/value fashion --- tags: - person parameters: - name: email in: query type: string description: email of person required: true responses: 200: description: A single user item schema: properties: property-1: type: string description: A property default: 'value-1' property-2: type: string description: A property default: 'value-2' property-N: type: string description: A property default: 'value-N' 400: description: Bad request. Missing/wrong parameter. 404: description: Person not found """ email = request.args.get('email', None) if email is None: return 'No email provided', 400 person = Store.get_person({"email": email}) if person is None: return 'Person with email %s not found' % email, 404 data = person.get_properties() data['aid'] = person.aid response = app.response_class(response=json.dumps(data), status=200, mimetype='application/json') return response
def handle(self, *args, **options): seller_id = options['seller_id'] fields = options['fields'] only_actives = options['only_actives'] store = Store(seller_id=seller_id) products = ProductForStore.objects.filter( store=store.business_model).exclude( sku=None).select_related('product') if only_actives: products.filter(status=ProductForStore.ACTIVE) else: products.filter( status_in=[ProductForStore.ACTIVE, ProductForStore.PAUSED]) bodys = dict() if 'price' in fields: update_stock(products, bodys, store) return store.update_items(list(bodys.keys()), list(bodys.values())) if 'description' in fields: update_descriptions(products, bodys, store) return store.update_items(list(bodys.keys()), list(bodys.values()))
def person_properties_by_id(person_id): """ Get a person properties by ID This endpoint returns all properties of a person by a given ID in a key/value fashion --- tags: - person parameters: - name: person_id in: path type: string description: id of person required: true responses: 200: description: A single user item schema: properties: property-1: type: string description: A property default: 'value-1' property-2: type: string description: A property default: 'value-2' property-N: type: string description: A property default: 'value-N' 400: description: Bad request 404: description: Person not found """ if len(person_id) == 0: return 'Missing person id', 400 person = Store.get_person_by_aid(person_id) if person is None: return 'Person with id %s not found' % person_id, 404 data = person.get_properties() response = app.response_class(response=json.dumps(data), status=200, mimetype='application/json') return response
def company_properties_by_id(company_id): """ Get a company properties by ID This endpoint returns all properties of a company by a given ID in a key/value fashion --- tags: - company parameters: - name: company_id in: path type: string description: id of company required: true responses: 200: description: A single company item schema: properties: property-1: type: string description: A property default: 'value-1' property-2: type: string description: A property default: 'value-2' property-N: type: string description: A property default: 'value-N' 404: description: Company not found """ company = Store.get_company_by_aid(company_id) if company is None: return 'Company with id %s not found' % company_id, 404 data = company.get_properties() response = app.response_class(response=json.dumps(data), status=200, mimetype='application/json') return response
def handle(self, *args, **options): seller_id = options['seller_id'] logging.getLogger('log_three').info('Aplicando filtro de malas palabras a productos') filter_bad_products(seller_id=seller_id) start = datetime.now() logging.getLogger('log_three').info('Consultando la base de datos') store = Store(seller_id=seller_id) business = BusinessModel.objects.get(pk=store.SELLER_ID) products = ProductForStore.objects.filter( store=business, sku=None, product__available=True, product__quantity__gt=2).select_related('product') logging.getLogger('log_three').info(f'Fin de la consulta, tiempo de ejecucion {datetime.now()-start}') slices = 100 USD = History.objects.order_by('-datetime').first() price_usd = USD.country(business.country) + business.usd_variation limit_per_day = False with ThreadPoolExecutor(max_workers=3) as executor: for lap, _products in enumerate(chunks(products, slices)): logging.getLogger('log_three').info(f'PUBLICANDO {(lap)*slices}-{(lap+1)*slices}') response = executor.map( store.publish, _products, [price_usd]*len(_products)) for product in response: if not product['ok'] and product.get('data'): limit_per_day = (product['data'].get('message') == 'daily_quota.reached') if limit_per_day: break if limit_per_day: break
import random import json from store.store import Store store = Store() class Actor: """ male or female actor """ def __init__(self, gender='male', name='', node_id=0): self.gender = gender self.name = name self.node_id = node_id ## irrelevant attributes ## also make the irrelevant attributes random. Not every entity will have them all self.attributes = { 'work': '', 'school': '', 'location_born': '', 'social_media_active': False, 'social_media_preferred': '', 'political_views': '', 'hobby': '', 'sport': '', } self.attribute_store = store.attribute_store self.fill_attributes()
def handle(self, *args, **options): seller_id = options['seller_id'] store = Store(seller_id=seller_id) now = timezone.now() last_hour = now - timedelta(hours=1) params = { 'seller': store.SELLER_ID, 'order.date_created.from': f'{last_hour.strftime("%Y-%m-%d")}T{last_hour.strftime("%H")}:00:00.000-00:00', 'order.date_created.to': f'{now.strftime("%Y-%m-%d")}T{now.strftime("%H")}:00:00.000-00:00', 'sort': 'date_desc' } path = '/orders/search' orders_api_draw = store.get(path, params, auth=True) orders_api = orders_api_draw.get('results') for order_draw in orders_api: news_draw = list() offer_id = order_draw.get('id') buyer_api = order_draw.get('buyer') # REGISTRAR COMPRADOR buyer = get_or_create_buyer(int(buyer_api.get('id')), buyer_api) # VERIFICAR EXISTENCIA DEL PRODUCTO product_api = order_draw.get('order_items')[0] quantity = product_api['quantity'] sku = product_api.get('item').get('id') product = ProductForStore.objects.filter(sku=sku).first() if not product: msg = f'El producto con sku={sku} no se encuentra en nuestra \ base de datos y fue orfertado bajo el pedido {offer_id} del comprador {buyer_api}' # LEVANTAR NOVEDAD logging.getLogger('log_three').warning(msg) continue USD = History.objects.order_by('-datetime').first() if product.sale_price * USD.country( store.country) > product_api.get('unit_price'): # LEVANTAR NOVEDAD msg = f'El precio acortado por el producto con sku={sku} no es rentable.' news_draw.append(msg) logging.getLogger('log_three').warning(msg) res = store.verify_existence(product) if not res.get('ok'): # LEVANTAR NOVEDAD msg = f'El producto con sku={sku} esta agotado' news_draw.append(msg) logging.getLogger('log_three').warning(msg) order = Order.objects.filter(store_order_id=offer_id) if order: break pay = Pay.objects.create(amount=float(product_api['unit_price'])) invoice = Invoice.objects.create(pay=pay) order = Order.objects.create(store_order_id=offer_id, product=product, quantity=quantity, buyer=buyer, invoice=invoice) for msg in news_draw: New.objects.create(user=store.attentive_user, message=msg, order=order)
def cancel_order(request): json_data=json.loads(request.body) message = json_data['message'] order_id = int(json_data['orderId']) reason = int(json_data['reason']) rating = int(json_data['rating']) order = Order.objects.filter(store_order_id=order_id).select_related('product').select_related('product__store').first() if not order: return JsonResponse({ 'ok': False, 'msg': 'El numero de pedido no existen. Esto debe ser un error, consulte al desarrollador.' }) if order.state == order.CANCELLED: return JsonResponse({ 'ok': False, 'msg': f'Esta orden ya fue cancelada anteriormente.' }) if order.state > Order.OFFERED: return JsonResponse({ 'ok': False, 'msg': f'Esta orden ya tiene un pago registrado, no es posible cancelar la misma.' }) body = { 'fulfilled':False, 'message':'No se pudo concretar la venta', 'reason':FeedBack.REASON_MELI[reason], 'rating':'neutral', } store = Store(seller_id=order.product.store.seller_id) res = store.post( path=f'/orders/{order_id}/feedback', body=body, auth=True, ) if res[0] == 'Feedback already exists': FeedBack.objects.create( raiting=rating, reason=reason, fulfilled=False, user=request.user, message=message, order=order ) order.state = Order.CANCELLED order.save() return JsonResponse({ 'ok': True, 'msg': 'Orden cancelada correctamente.', 'data': [] }) else: return JsonResponse({ 'ok': False, 'msg': res, 'data': [] })
def new_pay( request, order_id): if request.method != 'POST': raise Http404 json_data=json.loads(request.body) pay_reference = int(json_data.get('pay_reference')) quantity = int(json_data.get('quantity')) user_name = request.user.first_name if not (pay_reference and quantity): return JsonResponse({ 'ok': False, 'msg': f'{user_name} Haz ingresado un dato incorrecto, verifica e intenta de nuevo' }) order = Order.objects.filter(store_order_id=order_id).select_related('product').select_related('product__store').select_related('buyer').select_related('invoice').select_related('invoice__pay').first() if order.state >= Order.PROCESSING: return JsonResponse({ 'ok': False, 'msg': f'{user_name} ya esta orden fue procesada. Contacta con un supervisor si deseas hacer cambios en ella.' }) if order.state == Order.CANCELLED: return JsonResponse({ 'ok': False, 'msg': f'Esta orden ya fue concelada. No se puede modificar su estado.' }) if order.invoice.user: return JsonResponse({ 'ok': False, 'msg': f'{user_name} ya esta orden contine un pago relacionado' }) store = Store(seller_id=order.product.store.seller_id) params = { 'attributes': 'date_created,buyer,order_items' } path = f'/orders/{order_id}' result = store.get(path,params, auth=True) buyer = order.buyer product_api = result.get('order_items')[0] product = order.product USD = History.objects.order_by('-datetime').first() if product.sale_price*USD.country(store.country) > product_api.get('unit_price'): msg = f'El producto ha subido de precio.' New.objects.create( user=request.user, message=msg, order=order ) return JsonResponse({ 'ok': False, 'msg': f'{user_name}. {msg}, Contacta con tu supervisor.', }) # res = store.verify_existence(product) # if not res.get('ok'): # New.objects.create( # user=request.user, # message=res.get("msg"), # order=order # ) # return JsonResponse({ # 'ok': False, # 'msg': f'{user_name}, {res.get("msg")}', # }) pay = Pay.objects.filter(reference=pay_reference).first() if pay: msg = f'{user_name}. El numero de referencia de ese pago ya fue registrado anteriormente.' New.objects.create( user=store.attentive_user, message=msg, order=order ) return JsonResponse({ 'ok': False, 'msg': msg }) order.invoice.pay.reference = pay_reference order.invoice.pay.save() order.quantity = quantity order.state = Order.PAID_OUT order.save() msg = f'Referencia de pago agregada correctamente.' New.objects.create( user=request.user, message=msg, order=order ) return JsonResponse({ 'ok': True, 'msg': f'{user_name}. {msg}' })
def _enrich_entity(self, entity_type, enrichment_key, enrichment_behavior, enrichment_data=None, enrichment_source=None): """ Enrich a person - either with provided data or external enrichment (or both) :param enrichment_key: the search key to be used to retrieve the object :param enrichment_behavior: object determining external enrichment, dates, force, new, etc. ;param enrichment_data: an EnrichmentData object or Array of objects including data rows to add ;param enrichment_source: an EnrichmentSource object specifying the source of the added data :return: the person entity after the enrichment process """ status_code = EnrichmentException.ALL_OK status_message = "Enrichment completed succesfully (behavior: %s)" % str( enrichment_behavior) # Validate parameters if enrichment_data and not enrichment_source: raise EnrichmentException( "Cannot enrich with additional data without enrichment source.", EnrichmentException.BAD_REQUEST) # Decide which external providers are to be used (all, selective list or empty list) providers = self._decide_providers(enrichment_behavior) try: updated_entities = [] changed = False # Get person from the Store # TODO: in case too many results are returned - they are in-memory - need to limit entities = Store.get_entities( entity_type, enrichment_key, single_result=False, mongo_query=enrichment_behavior.mongo_query) if len(entities) == 0: if enrichment_behavior.create_new: self.logger.info( 'Enriching on %s. Could not locate entities in %s collection, creating a new entity.', enrichment_key, entity_type) if entity_type == 'people': entities = [AcureRatePerson()] elif entity_type == 'company': entities = [AcureRateCompany()] # If no provider, add a Dummy engager, so the system digests and stores the data if not providers: providers = ['System'] elif 'System' not in providers: providers.insert(0, 'System') else: msg = 'Attempting enrichment on key %s. Could not locate entities matching key (Behavior::create_new = False)' % enrichment_key raise EnrichmentException( msg, EnrichmentException.CONTACT_NOT_FOUND) elif len(entities) > 1 and not enrichment_behavior.enrich_multiple: msg = 'Enrichment data %s returns %d entities but enrich_multiple=False. Not enriching' % ( enrichment_key, len(entities)) raise EnrichmentException( msg, EnrichmentException.MULTIPLE_CONTACTS) # Go over all entities retrieved from store (per given key) #with ClusterRpcProxy(EnrichmentServiceConfig.AMQP_CONFIG, timeout=None) as rpc: rpc = None if True: for entity in entities: # If new enriched data provided, merge it into received entity if enrichment_data and len(enrichment_data) > 0: enrichment_data.append( EnrichmentData('last_run_time', datetime.datetime.now(), 'override-no-change')) # enrichment_data.append(EnrichmentData('data_source', enrichment_source.source_type, 'override')) # enrichment_data.append(EnrichmentData('enrich_key', enrichment_source.source_key, 'override')) changed |= entity.merge_data( enrichment_source.source_type, enrichment_source.source_key, enrichment_data) #changed |= entity.merge_data('System', 'nokey', enrichment_data) if changed or enrichment_behavior.digest: changed = entity.digest() # Initiate engagement manager to enrich via providers if True: EngagementManager().spawn_engagers_sequentially( providers, entity_type, entity, enrichment_behavior, changed) else: rpc.engagement_manager.spawn_engagers.call_async( providers, entity_type, entity.to_json_string(), enrichment_behavior.force, enrichment_behavior.force_save) except EnrichmentException as e: self.logger.warning(e) if enrichment_behavior.webhook: r = AcureRateUtils.announce( enrichment_behavior.webhook, { 'status_message': e.message, 'status_code': e.code, 'ts': time.time() }) if r: self.logger.info( 'Sent post request to webhook at %s. Content: %s. Code: %s', enrichment_behavior.webhook, r.content, r.status_code) except Exception as e: msg = 'Failed to enrich %s entity. Key: %s. Reason: %s' % ( entity_type, enrichment_key, e) self.logger.error(msg, exc_info=True) if enrichment_behavior.webhook: r = AcureRateUtils.announce( enrichment_behavior.webhook, { 'status_message': msg, 'status_code': EnrichmentException.FATAL_ERROR, 'ts': time.time() }) if r: self.logger.info( 'Sent post request to webhook at %s. Content: %s. Code: %s', enrichment_behavior.webhook, r.content, r.status_code) return updated_entities
def __init__(self): self.store = Store()