Ejemplo n.º 1
0
    def create_customer(self, data: dict):
        try:
            LOGGER.debug('Creating customer service')
            self.__validations.data(data=data)
            customer = Customer(**data)
            customer = self.__database.save(document=customer)
            return self.__parser.raw_to_json(
                data=customer.to_mongo().to_dict())

        except NotUniqueError:
            LOGGER.info('Email already used')
            raise EmailAlreadyUsedException()

        except ValidationError as exception:
            LOGGER.debug(traceback.format_exc())
            LOGGER.debug('Invalid fields value')
            raise InvalidFieldsValuesException(validations=exception.to_dict())

        except MissingRequiredFieldsException as exception:
            raise exception

        except Exception as exception:
            LOGGER.debug(traceback.format_exc())
            LOGGER.critical(exception)
            raise exception
Ejemplo n.º 2
0
    def remove_favorite(self, product_id: str, customer_id: str):
        try:
            LOGGER.debug('Removing product to customer list')
            self.__validations.validate_customer_product_id(product_id=product_id, customer_id=customer_id)
            customer = self.__database.get_customer_by_id(customer_id=customer_id)
            product = self.__database.get_product_by_id(product_id=product_id)
            customer = self.__database.pull_product_to_favorites(customer=customer, product=product)
            return self.__parser.parse_customer_favorites(customer=customer)

        except NotUniqueError:
            LOGGER.info('Email already used')
            raise EmailAlreadyUsedException()

        except ValidationError as exception:
            LOGGER.debug(traceback.format_exc())
            LOGGER.debug('Invalid fields value')
            raise InvalidFieldsValuesException(validations=exception.to_dict())

        except MissingRequiredFieldsException as exception:
            raise exception

        except Exception as exception:
            LOGGER.debug(traceback.format_exc())
            LOGGER.critical(exception)
            raise exception
Ejemplo n.º 3
0
    async def create_customer(self, request):
        try:
            LOGGER.debug('Creating customer action')
            payload = await request.json()
            customer = CustomerService().create_customer(data=payload)
            return generate_response(data=customer, status_code=201)

        except JSONDecodeError:
            LOGGER.debug("Invalid payload")
            return generate_response(message=[INVALID_PAYLOAD],
                                     status_code=400)

        except MissingRequiredFieldsException as exception:
            return generate_response(message=exception.messages,
                                     status_code=400)

        except DuplicateKeyError:
            return generate_response(message=[{
                'message': 'Duplicated email'
            }],
                                     status_code=400)

        except (InvalidFieldsValuesException,
                EmailAlreadyUsedException) as exception:
            return generate_response(message=exception.messages,
                                     status_code=422)

        except Exception as exception:
            LOGGER.debug(traceback.format_exc())
            LOGGER.critical(f"Unknown error: {exception}")
            return generate_response(status_code=500, message=[CRITICAL_ERROR])
Ejemplo n.º 4
0
    async def delete_customer(self, request):
        try:
            LOGGER.debug('Delete customer action')
            customer_id = request.path_params.get('customer_id')
            CustomerService().delete_customer(customer_id=customer_id)
            return generate_response(data={'_id': customer_id})
        except JSONDecodeError:
            LOGGER.debug("Invalid payload")
            return generate_response(message=[INVALID_PAYLOAD],
                                     status_code=400)

        except MissingRequiredFieldsException as exception:
            return generate_response(message=exception.messages,
                                     status_code=400)

        except NotFoundException:
            return generate_response(status_code=404)

        except InvalidFieldsValuesException as exception:
            return generate_response(message=exception.messages,
                                     status_code=422)

        except Exception as exception:
            LOGGER.debug(traceback.format_exc())
            LOGGER.critical(f"Unknown error: {exception}")
            return generate_response(status_code=500, message=[CRITICAL_ERROR])
Ejemplo n.º 5
0
    def update_customer(self, data: dict, customer_id: str):
        try:
            is_object_id(object_id=customer_id)
            self.__validations.data(data=data, is_update=True)
            LOGGER.debug(f'Updating customer service {customer_id}')
            customer = self.__database.update_customer_by_id(
                customer_id=customer_id, data=data)
            return self.__parser.raw_to_json(
                data=customer.to_mongo().to_dict())

        except NotUniqueError:
            LOGGER.info('Email already used')
            raise EmailAlreadyUsedException()

        except ValueError:
            LOGGER.debug(traceback.format_exc())
            LOGGER.debug('Invalid fields value')
            raise InvalidFieldsValuesException(validations={'field': ''})

        except ValidationError as exception:
            LOGGER.debug(traceback.format_exc())
            LOGGER.debug('Invalid fields value')
            raise InvalidFieldsValuesException(
                validations={exception.field_name: ''})

        except NotFoundException as exception:
            raise exception

        except Exception as exception:
            LOGGER.debug(traceback.format_exc())
            LOGGER.critical(exception)
            raise exception
Ejemplo n.º 6
0
    async def update_customer(self, request):
        try:
            LOGGER.debug('Update customer action')
            customer_id = request.path_params.get('customer_id')
            payload = await request.json()
            customer = CustomerService().update_customer(
                data=payload, customer_id=customer_id)
            return generate_response(data=customer)
        except JSONDecodeError:
            LOGGER.debug("Invalid payload")
            return generate_response(message=[INVALID_PAYLOAD],
                                     status_code=400)

        except MissingRequiredFieldsException as exception:
            return generate_response(message=exception.messages,
                                     status_code=400)

        except NotFoundException:
            return generate_response(status_code=404)

        except (InvalidFieldsValuesException,
                EmailAlreadyUsedException) as exception:
            return generate_response(message=exception.messages,
                                     status_code=422)

        except Exception as exception:
            LOGGER.debug(traceback.format_exc())
            LOGGER.critical(f"Unknown error: {exception}")
            return generate_response(status_code=500, message=[CRITICAL_ERROR])
Ejemplo n.º 7
0
    def update_product(self, data: dict, product_id: str):
        try:
            is_object_id(object_id=product_id)
            self.__validations.product_data(data=data, is_update=True)
            LOGGER.debug(f'Updating product service {product_id}')
            product = self.__database.update_product_by_id(
                product_id=product_id, data=data)
            return self.__parser.raw_to_json(data=product.to_mongo().to_dict())

        except ValueError:
            LOGGER.debug(traceback.format_exc())
            LOGGER.debug('Invalid fields value')
            raise InvalidFieldsValuesException(validations={'field': ''})

        except ValidationError as exception:
            LOGGER.debug(traceback.format_exc())
            LOGGER.debug('Invalid fields value')
            raise InvalidFieldsValuesException(
                validations={exception.field_name: ''})

        except NotFoundException as exception:
            raise exception

        except Exception as exception:
            LOGGER.debug(traceback.format_exc())
            LOGGER.critical(exception)
            raise exception
Ejemplo n.º 8
0
    async def remove_favorite(self, request):
        try:
            LOGGER.debug('Removing customer favorite product action')
            customer_id = request.path_params.get('customer_id')
            product_id = request.path_params.get('product_id')
            customer = FavoritesService().remove_favorite(
                product_id=product_id, customer_id=customer_id)
            return generate_response(data=customer, status_code=201)

        except JSONDecodeError:
            LOGGER.debug("Invalid payload")
            return generate_response(message=[INVALID_PAYLOAD],
                                     status_code=400)

        except MissingRequiredFieldsException as exception:
            return generate_response(message=exception.messages,
                                     status_code=400)

        except DuplicateKeyError:
            return generate_response(message=[{
                'message': 'Duplicated email'
            }],
                                     status_code=400)

        except (InvalidFieldsValuesException,
                EmailAlreadyUsedException) as exception:
            return generate_response(message=exception.messages,
                                     status_code=422)

        except Exception as exception:
            LOGGER.debug(traceback.format_exc())
            LOGGER.critical(f"Unknown error: {exception}")
            return generate_response(status_code=500, message=[CRITICAL_ERROR])
Ejemplo n.º 9
0
    async def get_products(self, request):
        try:
            LOGGER.debug('Getting products action')
            page = request.query_params.get('page')
            products = ProductService().get_products(page=page)
            return generate_response(data=products)
        except JSONDecodeError:
            LOGGER.debug("Invalid payload")
            return generate_response(message=[INVALID_PAYLOAD],
                                     status_code=400)

        except MissingRequiredFieldsException as exception:
            return generate_response(message=exception.messages,
                                     status_code=400)

        except NotFoundException:
            return generate_response(status_code=404)

        except InvalidFieldsValuesException as exception:
            return generate_response(message=exception.messages,
                                     status_code=422)
        except Exception as exception:
            LOGGER.debug(traceback.format_exc())
            LOGGER.critical(f"Unknown error: {exception}")
            return generate_response(status_code=500, message=[CRITICAL_ERROR])
Ejemplo n.º 10
0
    async def update_product(self, request):
        try:
            LOGGER.debug('Update product action')
            product_id = request.path_params.get('product_id')
            payload = await request.json()
            product = ProductService().update_product(data=payload,
                                                      product_id=product_id)
            return generate_response(data=product)
        except JSONDecodeError:
            LOGGER.debug("Invalid payload")
            return generate_response(message=[INVALID_PAYLOAD],
                                     status_code=400)

        except MissingRequiredFieldsException as exception:
            return generate_response(message=exception.messages,
                                     status_code=400)

        except NotFoundException:
            return generate_response(status_code=404)

        except InvalidFieldsValuesException as exception:
            return generate_response(message=exception.messages,
                                     status_code=422)

        except Exception as exception:
            LOGGER.debug(traceback.format_exc())
            LOGGER.critical(f"Unknown error: {exception}")
            return generate_response(status_code=500, message=[CRITICAL_ERROR])
Ejemplo n.º 11
0
    async def get_market_by_registry(self, request):
        try:
            LOGGER.debug('Getting market action')
            record = request.path_params.get('market_registry')
            market = self.service.get_market_by_registry(registry=record)
            return generate_response(data=market, status_code=200)

        except JSONDecodeError:
            LOGGER.debug("Invalid payload")
            return generate_response(message=[INVALID_PAYLOAD],
                                     status_code=400)

        except NotFoundException:
            LOGGER.debug('Market not found')
            return generate_response(status_code=404)

        except InvalidRecordException as exception:
            LOGGER.debug("Invalid record param")
            return generate_response(message=exception.messages,
                                     status_code=422)

        except Exception as exception:
            LOGGER.debug(traceback.format_exc())
            LOGGER.critical(f"Unknown error: {exception}")
            return generate_response(status_code=500, message=[CRITICAL_ERROR])
Ejemplo n.º 12
0
    async def create_market(self, request):
        try:
            LOGGER.debug('Creating market action')
            payload = await request.json()
            market = self.service.create_market(data=payload)
            return generate_response(data=market, status_code=201)

        except JSONDecodeError:
            LOGGER.debug("Invalid payload")
            return generate_response(message=[INVALID_PAYLOAD],
                                     status_code=400)

        except InvalidRecordException as exception:
            LOGGER.debug("Invalid record param")
            return generate_response(message=exception.messages,
                                     status_code=422)

        except MissingRequiredFieldsException as exception:
            return generate_response(message=exception.messages,
                                     status_code=400)

        except DataError:
            LOGGER.debug("Invalid value informed")
            return generate_response(message=[INVALID_FIELD_VALUE],
                                     status_code=422)

        except IntegrityError:
            return generate_response(message=[REGISTRY_ALREADY_USED],
                                     status_code=422)

        except Exception as exception:
            LOGGER.debug(traceback.format_exc())
            LOGGER.critical(f"Unknown error: {exception}")
            return generate_response(status_code=500, message=[CRITICAL_ERROR])
Ejemplo n.º 13
0
def takeAll(f1,f2,*args,**kwargs):
  c1,c2 = f1['content'],f2['content']

  # Assert:
  # 1. Content is a list
  # 2. There is only one type of element in each list
  assert type(c1)==type(c2)==list
  assert len(set([type(i) for i in c1]))==len(set([type(i) for i in c1]))==1

  #If the elements aren't dicts, simply return the union
  if not isinstance(c1[0], dict):
    return list( set(c1).union(c2) ) 

  #If the elements are dicts, we need to deconstruct each dict, compare if it is duplicated, and then re-constuct
  #We won't go deeper than the k,v pair: ie, we will ignore if v is a nested structure
  elif isinstance(c1[0], dict):
    result = {}
    for L in [c1,c2]:
      L = L if isinstance(L,list) else [L]
      for D in L:
        for k,v in D.iteritems():
          v = v if isinstance(v,list) else [v]
          if k not in result:
            result[k] = []
          if v not in result[k]:
            result[k].append(v)
    for k,v in result.iteritems():
      #Flatten the values of the dict
      result[k] = list(itertools.chain(*v))
    return {'content':[result], '@origin': '%s; %s' % (f1['@origin'],f2['@origin'])}

  #If elements are neither, we have a problem!
  LOGGER.critical('takeAll merger didnt get normalized data')
  raise TypeError (c1,c2)
Ejemplo n.º 14
0
    def delete_customer(self, customer_id: str):
        try:
            is_object_id(object_id=customer_id)
            LOGGER.debug(f'Deleting customer service {customer_id}')
            self.__database.delete_customer_by_id(customer_id=customer_id)

        except NotFoundException as exception:
            raise exception

        except Exception as exception:
            LOGGER.debug(traceback.format_exc())
            LOGGER.critical(exception)
            raise exception
Ejemplo n.º 15
0
 def get_products(self, page: str):
     try:
         LOGGER.debug('Getting products service')
         parse_page = self.__parser.page(page=page)
         products, products_total = self.__database.get_products(
             page=parse_page)
         return self.__parser.products(data=list(products.as_pymongo()),
                                       total=products_total,
                                       page=parse_page)
     except Exception as exception:
         LOGGER.debug(traceback.format_exc())
         LOGGER.critical(exception)
         raise exception
Ejemplo n.º 16
0
    def delete_product(self, product_id: str):
        try:
            is_object_id(object_id=product_id)
            LOGGER.debug(f'Deleting product service {product_id}')
            self.__database.delete_product_by_id(product_id=product_id)

        except NotFoundException as exception:
            raise exception

        except Exception as exception:
            LOGGER.debug(traceback.format_exc())
            LOGGER.critical(exception)
            raise exception
Ejemplo n.º 17
0
    def get_product(self, product_id: str):
        try:
            is_object_id(object_id=product_id)
            LOGGER.debug(f'Getting product {product_id}')
            product = self.__database.get_product_by_id(product_id=product_id)
            return self.__parser.raw_to_json(data=product.to_mongo().to_dict())

        except NotFoundException as exception:
            raise exception

        except Exception as exception:
            LOGGER.debug(traceback.format_exc())
            LOGGER.critical(exception)
            raise exception
Ejemplo n.º 18
0
    def get_customer(self, customer_id: str):
        try:
            is_object_id(object_id=customer_id)
            LOGGER.debug(f'Getting customer {customer_id}')
            customer = self.__database.get_customer_by_id(
                customer_id=customer_id)
            return self.__parser.parse_customer_favorites(customer=customer)

        except NotFoundException as exception:
            raise exception

        except Exception as exception:
            LOGGER.debug(traceback.format_exc())
            LOGGER.critical(exception)
            raise exception
Ejemplo n.º 19
0
def takeAll(f1,f2,*args,**kwargs):
  c1,c2 = ensureList(f1['content']),ensureList(f2['content'])

  # Assert:
  # 1. Content is a list
  # 2. There is only one type of element in each list
  assert len(set([type(i) for i in c1]))==len(set([type(i) for i in c1]))==1

  #If the elements aren't dicts, simply return the union
  if not isinstance(c1[0], dict):
    res = []
    for c in set(c1).union(c2):
      origin = []
      if c in c1:
        origin.append(f1['@origin'])
      if c in c2:
        origin.append(f2['@origin'])
      res.append({
        'content': c,
        '@origin': '; '.join(origin),
        })
    return res

  #If the elements are dicts, we need to deconstruct each dict, compare if it is duplicated, and then re-constuct
  #We won't go deeper than the k,v pair: ie, we will ignore if v is a nested structure
  elif isinstance(c1[0], dict):
    res = []
    for f in [f1,f2]:
      for c in ensureList(f['content']):
        if c in res:
          continue
        origin = []
        if c in ensureList(f1['content']):
          origin.append(f1['@origin'])
        if c in ensureList(f2['content']):
          origin.append(f2['@origin'])
        res.append({
          'content': c.get('content',c),
          '@origin': '; '.join(list(set(origin))),
        })
    return {'content':res,'@origin': '%s; %s' % (f1['@origin'],f2['@origin'])}

  #If elements are neither, we have a problem!
  LOGGER.critical('takeAll merger didnt get normalized data')
  raise TypeError (c1,c2)
Ejemplo n.º 20
0
    def create_product(self, data: dict):
        try:
            LOGGER.debug('Creating product service')
            self.__validations.product_data(data=data)
            product = Product(**data)
            product = self.__database.save(document=product)
            return self.__parser.raw_to_json(data=product.to_mongo().to_dict())

        except ValidationError as exception:
            LOGGER.debug(traceback.format_exc())
            LOGGER.debug('Invalid fields value')
            raise InvalidFieldsValuesException(validations=exception.to_dict())

        except MissingRequiredFieldsException as exception:
            raise exception

        except Exception as exception:
            LOGGER.debug(traceback.format_exc())
            LOGGER.critical(exception)
            raise exception
Ejemplo n.º 21
0
    def get_race_info_after(self, html) -> dict:
        soup = BS(html, "html.parser")

        main_window = soup.find("div", attrs={"id": "ReportBody"})

        if not main_window:
            LOGGER.warning(
                "Could not find ReportBody on %s because of the race did not start yet"
                % self.url)
            return []

        main_table = main_window.find("table", class_="rp-table rp-results")
        tbodies = main_table.find_all("tbody", class_="rp-table-row")

        for tbody in tbodies:
            local_race_data = {}
            trs = tbody.find_all("tr")

            upper_tds = trs[0].find_all('td')
            lower_tds = trs[1].find_all('td')

            horse_number = trs[0].find_all("td")[0].find_all("span")[0].text

            # Some runners have no number they ended a race
            local_race_data["horse_number"] = horse_number

            position_spans = trs[0].find_all("td")[0].find_all("span")

            local_race_data["position"] = ''
            if len(position_spans) > 1:
                local_race_data["position"] = position_spans[1].text[1:-1]

            horse_name = upper_tds[4].text.split('.')[-1].strip()
            local_race_data["horse_name"] = horse_name

            jokey = upper_tds[10].text.strip()
            local_race_data["jokey"] = jokey

            trainer = lower_tds[3].text.strip()
            local_race_data["trainer"] = trainer

            horse_age = int(upper_tds[11].text.strip())
            local_race_data["horse_age"] = horse_age

            horse_weight = upper_tds[12].text.strip()
            local_race_data["horse_weight"] = horse_weight

            bsp = upper_tds[15].text.strip()
            local_race_data["bsp"] = float(bsp) if len(bsp) > 0 else 0

            bpsp = lower_tds[-1].text.strip()[1:-1]
            local_race_data["bpsp"] = float(bpsp) if len(bpsp) > 0 else 0

            try:
                high, low = trs[0].find_all("td")[16].text.strip().split('/')
            except IndexError:
                LOGGER.critical("Cookies have been expired for %s" % self.url)
                print(
                    '\nATTENTION: Your Cookies expired!\nPlease UPDATE cookies!\n'
                )
                exit()

            local_race_data["high"] = float(high) if high.isdigit() else 0
            local_race_data["low"] = float(low) if low.isdigit() else 0

            B2L = local_race_data["bsp"] - local_race_data["low"]
            local_race_data["B2L"] = B2L

            L2B = local_race_data["high"] - local_race_data["bsp"]
            local_race_data["L2B"] = L2B

            runners = len(tbodies)
            local_race_data["runners"] = runners

            local_race_data.update(self.date_and_time)

            row = list(local_race_data.values())
            self.race_data.append(row[14:] + row[:14])

        return local_race_data