Esempio n. 1
0
def res_view(request):
    search_phrase = request.GET.get('search_field')
    nokaut_key = request.registry.settings.get('nokaut.key')
    user = request.user

    try :
        all = allegro_api(search_phrase)
    except AllegroNoItemEx:
        all = (None, None)

    try:
        nok = nokaut_api(search_phrase, nokaut_key)
    except NokautNoItemEx:
        nok = (None, None)

    nok_price, nok_link = nok[1], nok[0]
    all_price, all_link = all[1], all[0]

    mode = None
    if all_price and nok_price:
        if all_price < nok_price:
            mode = 'allegro'
        elif all_price > nok_price:
            mode = 'nokaut'
    elif all_price:
        mode = 'allegro'
    elif nok_price:
        mode = 'nokaut'

    if user is not None:
        prev = DBSession.query(UserSearch)\
                        .filter(UserSearch.search_id == user.id)\
                        .filter(UserSearch.search_content == search_phrase).first()

        if prev is not None:
            prev.search_quantity += 1
        else:
            search = UserSearch(
                search_id = user.id,
                search_content = search_phrase,
                all_link = all_link or '#',
                all_price = all_price or 0,
                nok_link = nok_link or '#',
                nok_price = nok_price or 0,
                search_quantity = 1
            )
            DBSession.add(search)

    return {
        'product_name' : search_phrase,
        'allegro_link' : all_link,
        'nokaut_link' : nok_link,
        'allegro_price' : all_price,
        'nokaut_price' : nok_price,
        'won' : mode
        }
Esempio n. 2
0
    def search():
        try :
            all_link, all_price = allegro_api(search_phrase)
        except AllegroNoItemEx:
            all_link, all_price = None, None

        try:
            nok_link, nok_price = nokaut_api(search_phrase, nokaut_key)
        except NokautNoItemEx:
            nok_link, nok_price = None, None

        return all_link, all_price, nok_link, nok_price
def search_result_view(request):

    data = request.GET.get('item')

    if not data:
        return HTTPFound('/')

    allegro_state = nokaut_state = 'price'

    try:
        allegro_price, allegro_url = allegro_api(data)
    except AllegroError:
        allegro_state = 'price'
        allegro_price = 'No product'
        allegro_url = ''

    try:
        nokaut_price, nokaut_url = nokaut_api(data, request.registry.settings.get('nokaut_key'))
    except NokautError:
        nokaut_state = 'price'
        nokaut_price = 'No product'
        nokaut_url = ''

    if nokaut_price != allegro_price:
        if allegro_price < nokaut_price:
            allegro_state = 'price win'
        else:
            nokaut_state = 'price win'

    response = {
        'product': data,
        'allegro_price_state': allegro_state,
        'allegro_price': allegro_price,
        'allegro_url': allegro_url,
        'nokaut_price_state': nokaut_state,
        'nokaut_price': nokaut_price,
        'nokaut_url': nokaut_url,
    }

    product = Product(
        name=data,
        a_price=allegro_price,
        a_url=allegro_url,
        n_price=nokaut_price,
        n_url=nokaut_url,
    )
    DBSession.add(product)

    return response
Esempio n. 4
0
def res_view(request):
    search_phrase = request.GET.get('search_field')
    nokaut_key = request.registry.settings.get('nokaut.key')

    try :
        all = allegro_api(search_phrase)
    except AllegroNoItemEx:
        all = (None, None)

    try:
        nok = nokaut_api(search_phrase,nokaut_key)
    except NokautNoItemEx:
        nok = (None, None)

    nok_price, nok_link = nok[1], nok[0]
    all_price, all_link = all[1], all[0]

    if (nok_price == None and all_price == None) or (nok_price == all_price):
        all_mode, nok_mode = '', ''
    elif nok_price != None and all_price == None:
        all_mode, nok_mode = '', 'win'
    elif nok_price == None and all_price != None:
        all_mode, nok_mode = 'win', ''
    else :
        if all_price < nok_price:
            all_mode, nok_mode = 'win', ''
        elif all_price > nok_price:
            all_mode, nok_mode = '', 'win'

    return {
            'product_name' : search_phrase,
            'allegro_link' : all_link,
            'nokaut_link' : nok_link,
            'allegro_price' : all_price,
            'nokaut_price' : nok_price,
            'allegro_price_mode' : all_mode,
            'nokaut_price_mode' : nok_mode
        }
Esempio n. 5
0
 def test_successfull_price(self, Browser):
     Browser().response().read.return_value = html
     _ , price = lib.allegro_api(self.search_phrase)
     self.assertEqual(price, self.search_price)
Esempio n. 6
0
 def test_succesfull_link(self, Browser):
     Browser().response().read.return_value = html
     link , _ = lib.allegro_api(self.search_phrase)
     self.assertEqual(link, html_link)
def search_result_view(request):

    name = request.GET.get('item')

    if not name:
        return {'error': 'Give a product name to compare'}

    date = datetime.date.today()
    user_id = request.user.id
    error = None
    product_exists = DBSession.query(Product)\
                              .filter(
                                  Product.name == name,
                                  Product.user_id == user_id,
                              ).first()

    if product_exists:
        if (date - product_exists.date).days < 2:
            product_exists.popularity += 1
            return dict(
                product=product_exists,
                error=error,
            )

    try:
        allegro_price, allegro_url = allegro_api(name)
    except AllegroError:
        allegro_price = 0
        allegro_url = ''

    try:
        nokaut_price, nokaut_url = nokaut_api(
            name,
            request.registry.settings.get('nokaut_key')
        )
    except NokautError:
        nokaut_price = 0
        nokaut_url = ''

    popularity = 1

    prod = DBSession.query(Product)\
                    .filter(
                        Product.name == name,
                        Product.user_id == user_id
                    ).first()

    if prod:
        popularity = prod.popularity + 1

    product = Product(
        name=name,
        a_price=allegro_price,
        a_url=allegro_url,
        n_price=nokaut_price,
        n_url=nokaut_url,
        popularity=popularity,
        user_id=user_id,
        date=date,
    )
    DBSession.add(product)

    return dict(
        product=product,
        error=error,
    )