Exemplo n.º 1
0
def createOwner(request):
    try:
        DB_STR = request.registry.settings['sqlalchemy.url']
        dbCon = DBCon(DB_STR)
        request_params = request.params
        phone = request_params.get('phone')
        password = request_params.get('password')
        if not (password and phone):
            return {
                'status': 'error',
                'data': {
                    'msg': "empty phone/password not allowed"
                }
            }
        qry = "select * from restaurant_owners where phone = '%s'" % (phone)
        owner = dbCon.fetch_one(qry)
        if owner and str(owner['password']) == str(password):
            return {'status': 'success', 'data': {'owner_id': owner['id']}}
        elif owner:
            return {'status': 'error', 'data': {'msg': "wrong password"}}
        else:
            qry = "insert into restaurant_owners values (null, '%s', '%s')" % (
                password, phone)
            owner_id = dbCon.insert(qry)
            return {'status': 'success', 'data': {'owner_id': owner['id']}}
    except:
        return {'status': 'error', 'data': {'msg': "server side error"}}
Exemplo n.º 2
0
def create(request):
    try:
        DB_STR = request.registry.settings['sqlalchemy.url']
        dbCon = DBCon(DB_STR)
        request_params = request.params
        name = request_params.get('name')
        email = request_params.get('email')
        pic_url = request_params.get('pic_url')
        address = request_params.get('address')
        cost_for_2 = request_params.get('cost_for_2')
        lat = request_params['lat']
        lng = request_params['lng']
        owner_id = request_params['owner_id']
        phone = request_params.get('phone')
        if not phone:
            return {
                'status': 'error',
                'data': {
                    'msg': "empty phone not allowed"
                }
            }
        if (len(phone) == 10):
            phone = '91' + phone
        phone = long(phone)
        return _searchOnJustDial(dbCon, request, phone, owner_id, name, lat,
                                 lng, address, cost_for_2, email, pic_url)
    except:
        import traceback
        traceback.print_exc()
        return {'status': 'error', 'data': {'msg': "server side error"}}
Exemplo n.º 3
0
def createDeal(request):
    try:
        DB_STR = request.registry.settings['sqlalchemy.url']
        dbCon = DBCon(DB_STR)
        request_params = request.params
        deal_id = request_params.get('deal_id')
        user_id = request_params.get('user_id')
        qry = "insert into user_deals values (null, %s, %s, '%s')" % (
            user_id, deal_id, 'claimed')
        user_deal_id = dbCon.insert(qry)
        return {'status': 'success', 'data': {'user_deal_id': user_deal_id}}
    except:
        return {'status': 'error', 'data': {'msg': "server side error"}}
Exemplo n.º 4
0
def getMyDeals(request):
    try:
        DB_STR = request.registry.settings['sqlalchemy.url']
        dbCon = DBCon(DB_STR)
        request_params = request.params
        time = request_params.get('time')
        user_id = request_params.get('user_id')

        qry = "select * from user_deals ud join deals d on ud.deal_id = d.id join restaurants r on r.id = d.restaurant_id where ud.user_id = %s and ud.status = 'claimed'" % (
            user_id)
        my_deals = dbCon.fetch_all(qry)
        print my_deals
        my_deals = [_formatMyDeal(deal) for deal in my_deals]
        return {'status': 'success', 'data': {'deals': my_deals}}
    except:
        return {'status': 'error', 'data': {'msg': "server side error"}}
Exemplo n.º 5
0
def getAllRestaurants(request):
    try:
        DB_STR = request.registry.settings['sqlalchemy.url']
        dbCon = DBCon(DB_STR)
        request_params = request.params

        qry = "select * from restaurants"
        restaurants = dbCon.fetch_all(qry)
        restaurants = [{
            'id': restaurant['id'],
            'name': restaurant['name']
        } for restaurant in restaurants]
        return {'status': 'success', 'data': {'restaurants': restaurants}}
    except:
        traceback.print_exc()
        return {'status': 'error', 'data': {'msg': "server side error"}}
Exemplo n.º 6
0
def getAllMyDeals(request):
    try:
        DB_STR = request.registry.settings['sqlalchemy.url']
        dbCon = DBCon(DB_STR)
        request_params = request.params
        restaurant_id = request_params.get('restaurant_id')
        qry = "select * from deals d join restaurants r on r.id = d.restaurant_id where d.status != 'closed' and d.restaurant_id = %s" % (
            restaurant_id)
        my_deals = dbCon.fetch_all(qry)
        print my_deals
        my_deals = [_formatMyDeal(dbCon, deal) for deal in my_deals]

        return {'status': 'success', 'data': {'deals': my_deals}}
    except:
        import traceback
        traceback.print_exc()
        return {'status': 'error', 'data': {'msg': "server side error"}}
Exemplo n.º 7
0
def create(request):
    try:
        DB_STR = request.registry.settings['sqlalchemy.url']
        dbCon = DBCon(DB_STR)
        request_params = request.params
        name = request_params['name']
        phone = request_params['phone']
        token = request_params['token']
        if not (phone and token):
            return {
                'status': 'error',
                'data': {
                    'msg': "empty phone/token not allowed"
                }
            }
        device_token = request_params.get('device_token')
        if not device_token:
            device_token = ''
        phone = phone.replace('-', '').replace('+', '')
        if len(phone) == 10:
            phone = '91' + phone
        qry = "select * from users where phone = '%s'" % (phone)
        user = dbCon.fetch_one(qry)
        if user:
            return {'status': 'success', 'data': {'user_id': user['id']}}
        stripe.api_key = request.registry.settings['stripe.api_key']
        customer_id, card_id = createStripeCustomer(token)
        if not (phone and token):
            return {
                'status': 'error',
                'data': {
                    'msg': "empty phone not allowed"
                }
            }
        qry = "insert into users values (null, '%s', '%s', '%s', '%s', '%s')" % (
            name, device_token, phone, card_id, customer_id)
        print qry
        user_id = dbCon.insert(qry)
        print user_id
        return {'status': 'success', 'data': {'user_id': user_id}}
    except:
        traceback.print_exc()
        return {'status': 'error', 'data': {'msg': "server side error"}}
Exemplo n.º 8
0
def unclaim(request):
    try:
        DB_STR = request.registry.settings['sqlalchemy.url']
        dbCon = DBCon(DB_STR)
        request_params = request.params
        deal_id = request_params.get('deal_id')
        user_id = request_params.get('user_id')

        qry = "update user_deals set status = 'close' where user_id = %s and deal_id = '%s" % (
            user_id, deal_id)
        user_deal = dbCon.update(qry)

        return {'status': 'success', 'data': {'user_deal_id': user_deal['id']}}
    except:
        traceback.print_exc()
        return {'status': 'error', 'data': {'msg': "server side error"}}


#unclaim a coupon
#dispute
#expire deal
Exemplo n.º 9
0
def charge(request):
    try:
        DB_STR = request.registry.settings['sqlalchemy.url']
        dbCon = DBCon(DB_STR)
        request_params = request.params
        amount = int(float(request_params.get('amount')) * 100)
        user_id = request_params.get('user_id')
        deal_id = request_params.get('deal_id')
        qry = "select * from users u join user_deals ud on u.id = ud.user_id where ud.status = 'claimed' and u.id = %s and ud.deal_id = %s" % (
            user_id, deal_id)
        user = dbCon.fetch_one(qry)
        stripe.api_key = request.registry.settings['stripe.api_key']
        charge_id = chargeStripeCustomer(user['customer_id'], user['card_id'],
                                         amount)

        qry = "update user_deals set status = 'close' where user_id = %s and deal_id = %s" % (
            user_id, deal_id)
        dbCon.update(qry)

        return {'status': 'success', 'data': {'charge_id': charge_id}}
    except:
        traceback.print_exc()
        return {'status': 'error', 'data': {'msg': "server side error"}}
Exemplo n.º 10
0
def create(request):
    try:
        DB_STR = request.registry.settings['sqlalchemy.url']
        dbCon = DBCon(DB_STR)
        request_params = request.params
        text = request_params.get('text')
        start_time = request_params.get('start_time')
        end_time = request_params.get('end_time')
        date = request_params.get('date')
        reservations_allowed = request_params.get('count')
        restaurant_id = request_params.get('restaurant_id')
        deal_type = request_params.get('deal_type')
        deal_msg = request_params.get('deal_msg')
        qry = "insert into deals values (null, '%s', %s, %s, '%s','published', %s, %s ,'%s', '%s')" % (
            text, start_time, end_time, date, reservations_allowed,
            restaurant_id, deal_type, deal_msg)
        print qry
        deal_id = dbCon.insert(qry)
        return {'status': 'success', 'data': {'deal_id': deal_id}}
    except:
        import traceback
        traceback.print_exc()
        return {'status': 'error', 'data': {'msg': "server side error"}}
Exemplo n.º 11
0
def onroute(request):
    try:
        DB_STR = request.registry.settings['sqlalchemy.url']
        dbCon = DBCon(DB_STR)
        request_params = request.params
        lat1 = request_params['lat1']
        lng1 = request_params['lng1']
        lat2 = request_params['lat2']
        lng2 = request_params['lng2']
        time = request_params.get('time')
        date = request_params['date']

        latLngs = [[lat1, lng1]]
        #here findviaroutes
        url = request.registry.settings[
            'justdial.url'] + "/directions/viaroute?z=17&output=json&loc=%s,%s&loc=%s,%s&instructions=true" % (
                lat1, lng1, lat2, lng2)
        print url
        data = requests.get(url)
        data = data.json()
        if data and 'via_points' in data and data['via_points']:
            latLngs.extend(data['via_points'])
        latLngs.append([lat2, lng2])
        print latLngs
        allAvailabeRestaurants = []
        for latLng in latLngs:
            index = 0
            while True and index < JD_COUNT:
                lat = latLng[0]
                lng = latLng[1]
                #http://hack2014.justdial.com/search/json/justdialapicat/restaurants/kebab/bangalore/13043647/77620617/2km/20/0
                url = request.registry.settings[
                    'justdial.url'] + "/search/json/justdialapicat/restaurants/food/bangalore/%s/%s/%s/20/%d" % (
                        _formatTo6Digits(lat), _formatTo6Digits(lng), DISTANCE,
                        index)
                print url
                data = requests.get(url)
                jdRestaurants = data.json()
                jdRestaurants = jdRestaurants['results']
                jdRestaurantDocIds = []
                print jdRestaurants
                for restaurant in jdRestaurants:
                    jdRestaurantDocIds.append(str(restaurant['docId']))
                if jdRestaurantDocIds:
                    print jdRestaurantDocIds
                    jdRestaurantDocIdsStr = "'" + "','".join(
                        jdRestaurantDocIds) + "'"
                    qry = "select * from restaurants r join deals d on r.id = d.restaurant_id where d.date = '%s' and (%s between d.start_time and d.end_time) and r.jd_doc_id in (%s)" % (
                        date, time, jdRestaurantDocIdsStr)
                    print qry
                    availableRestaurants = dbCon.fetch_all(qry)
                    print availableRestaurants
                    availableRestaurants = [
                        _formatRestaurant(restaurant, lat, lng)
                        for restaurant in availableRestaurants
                    ]
                    allAvailabeRestaurants.extend(availableRestaurants)
                index += 1

        if availableRestaurants:
            return {
                'status': 'success',
                'data': {
                    'deals': allAvailabeRestaurants
                }
            }

        return {'status': 'error', 'data': {'msg': "no restaurants exists"}}

    except:
        traceback.print_exc()
        return {'status': 'error', 'data': {'msg': "server side error"}}
Exemplo n.º 12
0
def nearby(request):
    try:
        DB_STR = request.registry.settings['sqlalchemy.url']
        dbCon = DBCon(DB_STR)
        request_params = request.params
        lat = request_params['lat']
        lng = request_params['lng']
        time = request_params['time']
        date = request_params['date']
        index = 0
        #Since just dial gives only 20 items in a single call
        #TODO call it in thread
        allAvailabeRestaurants = []
        while True and index < JD_COUNT:
            #http://hack2014.justdial.com/search/json/justdialapicat/restaurants/kebab/bangalore/13043647/77620617/2km/20/0
            url = request.registry.settings[
                'justdial.url'] + "/search/json/justdialapicat/restaurants/food/bangalore/%s/%s/%s/20/%d" % (
                    _formatTo6Digits(lat), _formatTo6Digits(lng), DISTANCE,
                    index)
            print url
            data = requests.get(url)
            jdRestaurants = data.json()
            jdRestaurants = jdRestaurants['results']
            jdRestaurantDocIds = []
            print jdRestaurants
            for restaurant in jdRestaurants:
                jdRestaurantDocIds.append(str(restaurant['docId']))
            if jdRestaurantDocIds:
                print jdRestaurantDocIds
                jdRestaurantDocIdsStr = "'" + "','".join(
                    jdRestaurantDocIds) + "'"
                qry = "select * from restaurants r join deals d on r.id = d.restaurant_id where d.date = '%s' and (%s between d.start_time and d.end_time) and r.jd_doc_id in (%s) group by r.id order by d.end_time desc" % (
                    date, time, jdRestaurantDocIdsStr)
                print qry
                availableRestaurants = dbCon.fetch_all(qry)
                print availableRestaurants
                availableRestaurants = [
                    _formatRestaurant(restaurant, lat, lng)
                    for restaurant in availableRestaurants
                ]
                allAvailabeRestaurants.extend(availableRestaurants)
            index += 1
        if len(allAvailabeRestaurants) < 10:
            ourBoundingBox = boundingBox(lat, lng, 3)
            qry = "select * from restaurants r join deals d on r.id = d.restaurant_id where d.date = '%s' and (%s between d.start_time and d.end_time) and (r.latitude between %s and %s) and (r.longitude between %s and %s)  group by r.id order by d.end_time desc" % (
                date, time, ourBoundingBox[0], ourBoundingBox[2],
                ourBoundingBox[1], ourBoundingBox[3])
            print qry
            availableRestaurants = dbCon.fetch_all(qry)
            print availableRestaurants
            availableRestaurants = [
                _formatRestaurant(restaurant, lat, lng)
                for restaurant in availableRestaurants
            ]
            allAvailabeRestaurants.extend(availableRestaurants)
        #if deal is availabe for the matching date time return it
        if allAvailabeRestaurants:
            sortedAllAvailabeRestaurants = sorted(allAvailabeRestaurants,
                                                  key=lambda k:
                                                  (-int(k['end_time'])))
            returnVal = []
            pickedRestaurantIds = []
            for sortedAllAvailabeRestaurant in sortedAllAvailabeRestaurants:
                if sortedAllAvailabeRestaurant[
                        'restaurant_id'] not in pickedRestaurantIds:
                    returnVal.append(sortedAllAvailabeRestaurant)
                pickedRestaurantIds.append(
                    sortedAllAvailabeRestaurant['restaurant_id'])
            return {'status': 'success', 'data': {'deals': returnVal}}

        return {'status': 'error', 'data': {'msg': "no restaurants exists"}}

    except:
        import traceback
        traceback.print_exc()
        return {'status': 'error', 'data': {'msg': "server side error"}}