Exemplo n.º 1
0
def show_notifications(request):
    user_id = request.user.id

    query = '''
    SELECT n.id, COALESCE(profile.avatar, artist.avatar) AS avatar, n.long_text AS text, n.time AS time, n.is_readed AS is_readed
    FROM notifications_notification as n
    INNER JOIN auth_user AS _user ON n.sender_id = _user.id
    LEFT JOIN users_profile AS profile ON _user.id = profile.id
    LEFT JOIN artists_artist AS artist ON _user.id = artist.id
    LEFT JOIN salons_salon AS salon ON _user.id = salon.id
    WHERE n.receiver_id = %s
    '''

    cursor = connection.cursor()
    cursor.execute(query, [user_id])
    notifications = dictfetchall(cursor)
    _notifications = []
    for n in notifications:
        n["avatar"] = MEDIA_ROOT + n["avatar"][7:] if n["avatar"] else ""
        _notifications.append(n["id"])

    _notifications = Notification.objects.filter(id__in=_notifications).update(
        is_readed=True)

    notifications = paginator(request, notifications, 10)

    return render(request, 'notifications/notifications.html',
                  {'notifications': notifications})
Exemplo n.º 2
0
def get_notifications(request):
    user_id = request.user.id

    query = '''
    SELECT n.id, COALESCE(profile.avatar, artist.avatar) AS avatar, n.short_text AS text, n.time AS time, n.is_readed AS is_readed
    FROM notifications_notification as n
    INNER JOIN auth_user AS _user ON n.sender_id = _user.id
    LEFT JOIN users_profile AS profile ON _user.id = profile.id
    LEFT JOIN artists_artist AS artist ON _user.id = artist.id
    LEFT JOIN salons_salon AS salon ON _user.id = salon.id
    WHERE n.receiver_id = %s
    ORDER BY n.id DESC
    LIMIT 5;
    '''

    cursor = connection.cursor()
    cursor.execute(query, [user_id])
    notifications = dictfetchall(cursor)
    _notifications = []
    for n in notifications:
        # n["avatar"] = n["avatar"][7:]
        _notifications.append(n["id"])

    _notifications = Notification.objects.filter(id__in = _notifications).update(is_readed=True)

    return HttpResponse(simplejson.dumps(notifications), content_type="application/json")
Exemplo n.º 3
0
def get_notifications(request):
    user_id = request.user.id

    query = '''
    SELECT n.id, COALESCE(profile.avatar, artist.avatar) AS avatar, n.short_text AS text, n.time AS time, n.is_readed AS is_readed
    FROM notifications_notification as n
    INNER JOIN auth_user AS _user ON n.sender_id = _user.id
    LEFT JOIN users_profile AS profile ON _user.id = profile.id
    LEFT JOIN artists_artist AS artist ON _user.id = artist.id
    LEFT JOIN salons_salon AS salon ON _user.id = salon.id
    WHERE n.receiver_id = %s
    ORDER BY n.id DESC
    LIMIT 5;
    '''

    cursor = connection.cursor()
    cursor.execute(query, [user_id])
    notifications = dictfetchall(cursor)
    _notifications = []
    for n in notifications:
        # n["avatar"] = n["avatar"][7:]
        _notifications.append(n["id"])

    _notifications = Notification.objects.filter(id__in=_notifications).update(
        is_readed=True)

    return HttpResponse(simplejson.dumps(notifications),
                        content_type="application/json")
Exemplo n.º 4
0
def show_notifications(request):
    user_id = request.user.id

    query = '''
    SELECT n.id, COALESCE(profile.avatar, artist.avatar) AS avatar, n.long_text AS text, n.time AS time, n.is_readed AS is_readed
    FROM notifications_notification as n
    INNER JOIN auth_user AS _user ON n.sender_id = _user.id
    LEFT JOIN users_profile AS profile ON _user.id = profile.id
    LEFT JOIN artists_artist AS artist ON _user.id = artist.id
    LEFT JOIN salons_salon AS salon ON _user.id = salon.id
    WHERE n.receiver_id = %s
    '''

    cursor = connection.cursor()
    cursor.execute(query, [user_id])
    notifications = dictfetchall(cursor)
    _notifications = []
    for n in notifications:
        n["avatar"] = MEDIA_ROOT + n["avatar"][7:] if n["avatar"] else "" 
        _notifications.append(n["id"])

    _notifications = Notification.objects.filter(id__in = _notifications).update(is_readed=True)

    notifications = paginator(request, notifications, 10)

    return render(request, 'notifications/notifications.html', {'notifications': notifications})
Exemplo n.º 5
0
def get_notifications(request):
    user = request.user
    receiver_id = user.id

    query = '''
    SELECT n.id, COALESCE(profile.avatar, artist.avatar, salon.avatar) AS avatar, n.short_text AS text, n.time AS time, n.is_readed AS is_readed, n.sender_id AS sender_id
    FROM notifications_notification as n
    INNER JOIN auth_user AS _user ON n.sender_id = _user.id
    LEFT JOIN users_profile AS profile ON _user.id = profile.user_id
    LEFT JOIN artists_artist AS artist ON _user.id = artist.user_id
    LEFT JOIN salons_salon AS salon ON _user.id = salon.user_id
    WHERE n.receiver_id = %s
    ORDER BY n.id DESC
    LIMIT 5;
    '''

    if user.related_with == 'salons':
        receiver_id = user.salon.id
        query = '''
        SELECT DISTINCT n.id, COALESCE(profile.avatar, artist.avatar, salon.avatar) AS avatar, n.short_text AS text, n.time AS time, n.is_readed AS is_readed, n.sender_id AS sender_id
        FROM salons_salon AS s
        INNER JOIN artists_artist AS a ON s.id = a.salon_id
        INNER JOIN auth_user AS u ON (a.user_id = u.id) or (u.id = ''' + str(
            user.id) + ''')
        INNER JOIN notifications_notification AS n ON n.receiver_id = u.id
        LEFT JOIN users_profile AS profile ON n.sender_id = profile.user_id
        LEFT JOIN artists_artist AS artist ON n.sender_id = artist.user_id
        LEFT JOIN salons_salon AS salon ON n.sender_id = salon.user_id
        WHERE s.id = %s
        ORDER BY n.id DESC
        LIMIT 5;
        '''

    cursor = connection.cursor()
    cursor.execute(query, [receiver_id])
    notifications = dictfetchall(cursor)
    _notifications = []
    for n in notifications:
        if n["sender_id"] == 1:
            n["avatar"] = "{0}img/logo_old.png".format(STATIC_URL)
        _notifications.append(n["id"])

    _notifications = Notification.objects.filter(id__in=_notifications).update(
        is_readed=True)

    return HttpResponse(simplejson.dumps(notifications),
                        content_type="application/json")
Exemplo n.º 6
0
def show_notifications(request):
    user = request.user
    receiver_id = user.id
    query = '''
    SELECT n.id, COALESCE(profile.avatar, artist.avatar, salon.avatar) AS avatar, n.long_text AS text, n.time AS time, n.is_readed AS is_readed, n.sender_id AS sender_id
    FROM notifications_notification as n
    INNER JOIN auth_user AS _user ON n.sender_id = _user.id
    LEFT JOIN users_profile AS profile ON _user.id = profile.user_id
    LEFT JOIN artists_artist AS artist ON _user.id = artist.user_id
    LEFT JOIN salons_salon AS salon ON _user.id = salon.user_id
    WHERE n.receiver_id = %s
    ORDER BY n.id DESC
    '''

    if user.related_with == 'salons':
        receiver_id = user.salon.id
        query = '''
        SELECT DISTINCT (n.id), COALESCE(profile.avatar, artist.avatar, salon.avatar) AS avatar, n.long_text AS text, n.time AS time, n.is_readed AS is_readed, n.sender_id AS sender_id
        FROM salons_salon AS s
        INNER JOIN artists_artist AS a ON s.id = a.salon_id
        INNER JOIN auth_user AS u ON (a.user_id = u.id) OR (u.id = ''' + str(
            user.id) + ''')
        INNER JOIN notifications_notification AS n ON n.receiver_id = u.id
        LEFT JOIN users_profile AS profile ON n.sender_id = profile.user_id
        LEFT JOIN artists_artist AS artist ON n.sender_id = artist.user_id
        LEFT JOIN salons_salon AS salon ON n.sender_id = salon.user_id
        WHERE s.id = %s
        ORDER BY n.id DESC
        '''

    cursor = connection.cursor()
    cursor.execute(query, [receiver_id])
    notifications = dictfetchall(cursor)
    _notifications = []
    for n in notifications:
        n["avatar"] = MEDIA_ROOT + n["avatar"][7:] if n["avatar"] else ""
        _notifications.append(n["id"])

    _notifications = Notification.objects.filter(id__in=_notifications).update(
        is_readed=True)

    notifications = paginator(request, notifications, 10)

    return render(request, 'notifications/notifications.html',
                  {'notifications': notifications})
Exemplo n.º 7
0
def get_notifications(request):
    user = request.user
    receiver_id = user.id

    query = '''
    SELECT n.id, COALESCE(profile.avatar, artist.avatar, salon.avatar) AS avatar, n.short_text AS text, n.time AS time, n.is_readed AS is_readed, n.sender_id AS sender_id
    FROM notifications_notification as n
    INNER JOIN auth_user AS _user ON n.sender_id = _user.id
    LEFT JOIN users_profile AS profile ON _user.id = profile.user_id
    LEFT JOIN artists_artist AS artist ON _user.id = artist.user_id
    LEFT JOIN salons_salon AS salon ON _user.id = salon.user_id
    WHERE n.receiver_id = %s
    ORDER BY n.id DESC
    LIMIT 5;
    '''

    if user.related_with == 'salons':
        receiver_id = user.salon.id
        query = '''
        SELECT DISTINCT n.id, COALESCE(profile.avatar, artist.avatar, salon.avatar) AS avatar, n.short_text AS text, n.time AS time, n.is_readed AS is_readed, n.sender_id AS sender_id
        FROM salons_salon AS s
        INNER JOIN artists_artist AS a ON s.id = a.salon_id
        INNER JOIN auth_user AS u ON (a.user_id = u.id) or (u.id = '''+str(user.id)+''')
        INNER JOIN notifications_notification AS n ON n.receiver_id = u.id
        LEFT JOIN users_profile AS profile ON n.sender_id = profile.user_id
        LEFT JOIN artists_artist AS artist ON n.sender_id = artist.user_id
        LEFT JOIN salons_salon AS salon ON n.sender_id = salon.user_id
        WHERE s.id = %s
        ORDER BY n.id DESC
        LIMIT 5;
        '''

    cursor = connection.cursor()
    cursor.execute(query, [receiver_id])
    notifications = dictfetchall(cursor)
    _notifications = []
    for n in notifications:
        if n["sender_id"] == 1:
            n["avatar"] = "{0}img/logo_old.png".format(STATIC_URL)
        _notifications.append(n["id"])

    _notifications = Notification.objects.filter(id__in=_notifications).update(is_readed=True)

    return HttpResponse(simplejson.dumps(notifications), content_type="application/json")
Exemplo n.º 8
0
def show_notifications(request):
    user = request.user
    receiver_id = user.id
    query = '''
    SELECT n.id, COALESCE(profile.avatar, artist.avatar, salon.avatar) AS avatar, n.long_text AS text, n.time AS time, n.is_readed AS is_readed, n.sender_id AS sender_id
    FROM notifications_notification as n
    INNER JOIN auth_user AS _user ON n.sender_id = _user.id
    LEFT JOIN users_profile AS profile ON _user.id = profile.user_id
    LEFT JOIN artists_artist AS artist ON _user.id = artist.user_id
    LEFT JOIN salons_salon AS salon ON _user.id = salon.user_id
    WHERE n.receiver_id = %s
    ORDER BY n.id DESC
    '''

    if user.related_with == 'salons':
        receiver_id = user.salon.id
        query = '''
        SELECT DISTINCT (n.id), COALESCE(profile.avatar, artist.avatar, salon.avatar) AS avatar, n.long_text AS text, n.time AS time, n.is_readed AS is_readed, n.sender_id AS sender_id
        FROM salons_salon AS s
        INNER JOIN artists_artist AS a ON s.id = a.salon_id
        INNER JOIN auth_user AS u ON (a.user_id = u.id) OR (u.id = '''+str(user.id)+''')
        INNER JOIN notifications_notification AS n ON n.receiver_id = u.id
        LEFT JOIN users_profile AS profile ON n.sender_id = profile.user_id
        LEFT JOIN artists_artist AS artist ON n.sender_id = artist.user_id
        LEFT JOIN salons_salon AS salon ON n.sender_id = salon.user_id
        WHERE s.id = %s
        ORDER BY n.id DESC
        '''

    cursor = connection.cursor()
    cursor.execute(query, [receiver_id])
    notifications = dictfetchall(cursor)
    _notifications = []
    for n in notifications:
        n["avatar"] = MEDIA_ROOT + n["avatar"][7:] if n["avatar"] else ""
        _notifications.append(n["id"])

    _notifications = Notification.objects.filter(id__in=_notifications).update(is_readed=True)

    notifications = paginator(request, notifications, 10)

    return render(request, 'notifications/notifications.html', {'notifications': notifications})
Exemplo n.º 9
0
def search(request):
    query = request.GET.get("q", None)
    lat = request.GET.get("lat", None)
    lng = request.GET.get("lng", None)
    budget = request.GET.get("budget", None)
    gender = request.GET.get("gender", None)
    rating = request.GET.get("rating", None)
    date = request.GET.get("date", None)
    hour = request.GET.get("hour", None)
    tags = request.GET.get("tags", None)
    sorted_by = request.GET.get("sorted_by", None)
    start_price = 0
    end_price = 15000
    point = Point(23.31326937672202, 42.68336526966131)
    type_of_order = ['listing_id', '-listing_id', '-likes', '-comments']
    if query:
        query = query.split(" ")
        if tags:
            tags = tags.split(',')
            query = query + tags
        sq = SQ()
        for q in query:
            sq.add(SQ(tags__contains=q), SQ.OR)
            sq.add(SQ(title__contains=q), SQ.OR)
            sq.add(SQ(description__contains=q), SQ.OR)
    else:
        sq = SQ()

    if budget:
        start_price = int(budget.split('-')[0])
        end_price = int(budget.split('-')[1])

    if gender:
        gender = int(gender)
        if gender == 0:
            gender = [0]
        elif gender == 2:
            gender = [1]
        else:
            gender = [0, 1, 2]
    else:
        gender = [0, 1, 2]

    # if lat and lng:
    #     lat = float(lat)
    #     lng = float(lng)
    # point = Point(lat, lng)

    if date and date.isdigit():
        date = int(date)
    else:
        date = None

    if not (hour is None or hour == "-1"):
        hour = int(hour)
    else:
        hour = -1

    if rating:
        rating = int(rating)
    else:
        rating = 0

    if sorted_by:
        sorted_by = type_of_order[int(sorted_by)]
    else:
        sorted_by = 'listing_id'

    partial_query = SearchQuerySet().models(Listing).filter(sq)
    partial_query = [l.price for l in partial_query]

    if len(partial_query) >= 2:
        price_list = [min(partial_query), max(partial_query)]
    else:
        price_list = [0, 500]
    print(price_list)

    price_list = [min(price_list), max(price_list)]
    _listings = SearchQuerySet().models(Listing).filter(sq).filter(
        gender__in=gender,
        price__gte=start_price,
        price__lte=end_price,
        status=1,
        rating__gte=rating)
    print("numbers", len(_listings))
    # .dwithin('location', point, D(km=1500000))

    if date and not (hour is not None and hour == -1):
        ''' hour in seconds is the time in utc seconds from the date to the required time '''
        work_days = [("mon_start", "mon_end"), ("tues_start", "tues_end"),
                     ("wed_start", "wed_end"), ("thurs_start", "thurs_end"),
                     ("fri_start", "fri_end"), ("sat_start", "sat_end"),
                     ("sun_start", "sun_end")]

        # get listings IDs which was already filtered and make in format (1,1,3,3,5)
        listings_ids = [l.listing_id for l in _listings]
        listings_ids = str(listings_ids)[1:-1]

        # get the index of the day from the week
        week_days = datetime.datetime.fromtimestamp(date).strftime('%w')
        week_days = work_days[int(week_days)]

        # hour in seconds
        hour_in_seconds = 28800 + hour * 1800

        # start range is a variable which will be used for the following things: time in seconds from booking start
        start_range = date + hour_in_seconds

        query = "SELECT DISTINCT listing.id, listing.title, listing.likes, listing.price, listing.artist_id, listing.comments, listing.picture_cover, artist.lat, artist.lng, artist.style, artist.avatar"
        query += " FROM listings_listing AS listing"
        query += " JOIN artists_artist AS artist ON artist.id = listing.artist_id"
        query += " JOIN artists_worktime AS worktime ON worktime.artist_id = artist.id"
        query += " LEFT JOIN booking_booking AS booking ON booking.artist_id = artist.id"
        query += " LEFT JOIN artists_busy AS busy ON busy.artist_id = artist.id"
        query += " WHERE listing.id IN ({0})".format(listings_ids)
        query += " AND worktime.{0} <= {1} AND worktime.{2} >= ({1} + listing.duration/1800)".format(
            week_days[0], hour, week_days[1])
        query += " AND (booking.start_time >= listing.duration + {0}".format(
            start_range)
        query += " OR booking.end_time <= {0} OR booking.start_time IS NULL)".format(
            start_range)
        query += " AND (busy.start_time >= listing.duration + {0}".format(
            start_range)
        query += " OR busy.end_time <= {0} OR busy.start_time IS NULL)".format(
            start_range)

        cursor = connection.cursor()
        cursor.execute(query)
        _listings = dictfetchall(cursor)

        listings = []
        listing = {}
        for l in _listings:
            listing = {}
            listing["lat"] = l["lat"]
            listing["lng"] = l["lng"]
            listing["id"] = l["id"]
            listing["picture"] = l["picture_cover"]
            listing["style"] = STYLE_INDEXES[int(l["style"]) - 1][1]
            listing["title"] = l["title"]
            listing["likes"] = l["likes"]
            listing["price"] = int(l["price"])
            listing["comments"] = l["comments"]
            listing["artist_id"] = l["artist_id"]
            listing["avatar"] = MEDIA_ROOT + l["avatar"][7:]
            listings.append(listing)
        print("1")
    elif date:
        work_days = [("mon_start", "mon_end"), ("tues_start", "tues_end"),
                     ("wed_start", "wed_end"), ("thurs_start", "thurs_end"),
                     ("fri_start", "fri_end"), ("sat_start", "sat_end"),
                     ("sun_start", "sun_end")]

        listings_ids = [l.listing_id for l in _listings]
        listings_ids = str(listings_ids)[1:-1]

        week_days = datetime.datetime.fromtimestamp(date).strftime('%w')
        week_days = work_days[int(week_days)]

        query = "SELECT DISTINCT listing.id, listing.title, listing.likes, listing.price, listing.artist_id, listing.comments, listing.picture_cover, artist.lat, artist.lng, artist.style, artist.avatar"
        query += " FROM listings_listing AS listing"
        query += " JOIN artists_artist AS artist ON artist.id = listing.artist_id"
        query += " JOIN artists_worktime AS worktime ON worktime.artist_id = artist.id"
        query += " LEFT JOIN booking_booking AS booking ON booking.artist_id = artist.id"
        query += " LEFT JOIN artists_busy AS busy ON busy.artist_id = artist.id"
        query += " WHERE listing.id IN ({0})".format(listings_ids)
        query += " AND NOT worktime.{0} = -1".format(week_days[0])

        cursor = connection.cursor()
        cursor.execute(query)
        _listings = dictfetchall(cursor)

        listings = []
        listing = {}
        for l in _listings:
            listing = {}
            listing["lat"] = l["lat"]
            listing["lng"] = l["lng"]
            listing["id"] = l["id"]
            listing["picture"] = l["picture_cover"]
            listing["style"] = STYLE_INDEXES[int(l["style"]) - 1][1]
            listing["title"] = l["title"]
            listing["likes"] = l["likes"]
            listing["price"] = int(l["price"])
            listing["comments"] = l["comments"]
            listing["artist_id"] = l["artist_id"]
            listing["avatar"] = MEDIA_ROOT + l["avatar"][7:]
            listings.append(listing)
        print("2")

    else:
        listings = []
        listing = {}
        for l in _listings:
            listing = {}
            listing["lat"] = l.location.x
            listing["lng"] = l.location.y
            listing["id"] = l.listing_id
            listing["picture"] = l.get_picture
            listing["style"] = l.style
            listing["title"] = l.title
            listing["likes"] = l.likes
            listing["price"] = int(l.price)
            listing["comments"] = l.comments
            listing["artist_id"] = l.artist_id
            listing["avatar"] = l.artist_avatar
            listings.append(listing)
        print('3')
    return render(request, 'service/service.html', {
        "listings": listings,
        "price_list": price_list
    })
Exemplo n.º 10
0
def search(request):
    query = request.GET.get("q", None)
    lat = request.GET.get("lat", None)
    lng = request.GET.get("lng", None)
    budget = request.GET.get("budget", None)
    gender = request.GET.get("gender", None)
    rating = request.GET.get("rating", None)
    date = request.GET.get("date", None)
    hour = request.GET.get("hour", None)
    tags = request.GET.get("tags", None)
    sorted_by = request.GET.get("sorted_by", None)
    start_price = 0
    end_price = 15000
    point = Point(23.31326937672202, 42.68336526966131)
    type_of_order = ['listing_id', '-listing_id', '-likes', '-comments']
    if query:
        query = query.split(" ")
        if tags:
            tags = tags.split(',')
            query = query + tags
        sq = SQ()
        for q in query:
            sq.add(SQ(tags__contains=q), SQ.OR)
            sq.add(SQ(title__contains=q), SQ.OR)
            sq.add(SQ(description__contains=q), SQ.OR)
    else:
        sq = SQ()

    if budget:
        start_price = int(budget.split('-')[0])
        end_price = int(budget.split('-')[1])

    if gender:
        gender = int(gender)
        if gender == 0:
            gender = [0]
        elif gender == 2:
            gender = [1]
        else:
            gender = [0,1,2]
    else:
        gender = [0,1,2]


    # if lat and lng:
    #     lat = float(lat)
    #     lng = float(lng)
        # point = Point(lat, lng)
        
    if date and date.isdigit():
        date = int(date)
    else:
        date = None

    if not (hour is  None or hour == "-1"):
        hour = int(hour)
    else:
        hour = -1

    if rating:
        rating = int(rating)
    else:
        rating = 0

    if sorted_by:
        sorted_by = type_of_order[int(sorted_by)]
    else:
        sorted_by = 'listing_id'


    partial_query = SearchQuerySet().models(Listing).filter(sq)
    partial_query = [l.price for l in partial_query]

    if len(partial_query) >= 2:
        price_list = [min(partial_query), max(partial_query)]
    else:
        price_list = [0, 500]
    print(price_list)

    price_list = [min(price_list), max(price_list)]
    _listings = SearchQuerySet().models(Listing).filter(sq).filter(
                                                    gender__in=gender, 
                                                    price__gte=start_price, 
                                                    price__lte=end_price, 
                                                    status=1, 
                                                    rating__gte=rating)
    print("numbers", len(_listings))
    # .dwithin('location', point, D(km=1500000))

    if date and not (hour is not None and hour == -1):
        ''' hour in seconds is the time in utc seconds from the date to the required time '''
        work_days = [("mon_start", "mon_end"), ("tues_start", "tues_end"), ("wed_start", "wed_end"), ("thurs_start", "thurs_end"), ("fri_start", "fri_end"), ("sat_start", "sat_end"), ("sun_start", "sun_end")]
        
        # get listings IDs which was already filtered and make in format (1,1,3,3,5)
        listings_ids = [l.listing_id for l in _listings]
        listings_ids = str(listings_ids)[1:-1]

        # get the index of the day from the week
        week_days = datetime.datetime.fromtimestamp(date).strftime('%w')
        week_days = work_days[int(week_days)]

        # hour in seconds
        hour_in_seconds = 28800 + hour*1800

        # start range is a variable which will be used for the following things: time in seconds from booking start 
        start_range = date + hour_in_seconds

        query = "SELECT DISTINCT listing.id, listing.title, listing.likes, listing.price, listing.artist_id, listing.comments, listing.picture_cover, artist.lat, artist.lng, artist.style, artist.avatar"
        query += " FROM listings_listing AS listing"
        query += " JOIN artists_artist AS artist ON artist.id = listing.artist_id"
        query += " JOIN artists_worktime AS worktime ON worktime.artist_id = artist.id"
        query += " LEFT JOIN booking_booking AS booking ON booking.artist_id = artist.id"
        query += " LEFT JOIN artists_busy AS busy ON busy.artist_id = artist.id"
        query += " WHERE listing.id IN ({0})".format(listings_ids)
        query += " AND worktime.{0} <= {1} AND worktime.{2} >= ({1} + listing.duration/1800)".format(week_days[0], hour, week_days[1])
        query += " AND (booking.start_time >= listing.duration + {0}".format(start_range)
        query += " OR booking.end_time <= {0} OR booking.start_time IS NULL)".format(start_range)
        query += " AND (busy.start_time >= listing.duration + {0}".format(start_range)
        query += " OR busy.end_time <= {0} OR busy.start_time IS NULL)".format(start_range)


        cursor = connection.cursor()
        cursor.execute(query)
        _listings = dictfetchall(cursor)

        listings = []
        listing = {}
        for l in _listings:
            listing = {}
            listing["lat"] = l["lat"]
            listing["lng"] = l["lng"]
            listing["id"] = l["id"]
            listing["picture"] = l["picture_cover"]
            listing["style"] = STYLE_INDEXES[int(l["style"])-1][1]
            listing["title"] = l["title"]
            listing["likes"] = l["likes"]
            listing["price"] = int(l["price"])
            listing["comments"] = l["comments"]
            listing["artist_id"] = l["artist_id"]
            listing["avatar"] = MEDIA_ROOT + l["avatar"][7:]
            listings.append(listing)
        print("1")
    elif date:
        work_days = [("mon_start", "mon_end"), ("tues_start", "tues_end"), ("wed_start", "wed_end"), ("thurs_start", "thurs_end"), ("fri_start", "fri_end"), ("sat_start", "sat_end"), ("sun_start", "sun_end")]
        
        listings_ids = [l.listing_id for l in _listings]
        listings_ids = str(listings_ids)[1:-1]

        week_days = datetime.datetime.fromtimestamp(date).strftime('%w')
        week_days = work_days[int(week_days)]

        query = "SELECT DISTINCT listing.id, listing.title, listing.likes, listing.price, listing.artist_id, listing.comments, listing.picture_cover, artist.lat, artist.lng, artist.style, artist.avatar"
        query += " FROM listings_listing AS listing"
        query += " JOIN artists_artist AS artist ON artist.id = listing.artist_id"
        query += " JOIN artists_worktime AS worktime ON worktime.artist_id = artist.id"
        query += " LEFT JOIN booking_booking AS booking ON booking.artist_id = artist.id"
        query += " LEFT JOIN artists_busy AS busy ON busy.artist_id = artist.id"
        query += " WHERE listing.id IN ({0})".format(listings_ids)
        query += " AND NOT worktime.{0} = -1".format(week_days[0])

        cursor = connection.cursor()
        cursor.execute(query)
        _listings = dictfetchall(cursor)

        listings = []
        listing = {}
        for l in _listings:
            listing = {}
            listing["lat"] = l["lat"]
            listing["lng"] = l["lng"]
            listing["id"] = l["id"]
            listing["picture"] = l["picture_cover"]
            listing["style"] = STYLE_INDEXES[int(l["style"])-1][1]
            listing["title"] = l["title"]
            listing["likes"] = l["likes"]
            listing["price"] = int(l["price"])
            listing["comments"] = l["comments"]
            listing["artist_id"] = l["artist_id"]
            listing["avatar"] = MEDIA_ROOT + l["avatar"][7:]
            listings.append(listing)
        print("2")

    else:
        listings = []
        listing = {}
        for l in _listings:
            listing = {}
            listing["lat"] = l.location.x
            listing["lng"] = l.location.y
            listing["id"] = l.listing_id
            listing["picture"] = l.get_picture
            listing["style"] = l.style
            listing["title"] = l.title
            listing["likes"] = l.likes
            listing["price"] = int(l.price)
            listing["comments"] = l.comments
            listing["artist_id"] = l.artist_id
            listing["avatar"] = l.artist_avatar
            listings.append(listing)
        print('3')
    return render(request, 'service/service.html', {"listings": listings, "price_list": price_list})
Exemplo n.º 11
0
def search(request):
    styles = {
        "hairstyle": "hair",
        "nails-design": "nails",
        "make-up": "make up",
    }
    query = styles.get(request.GET.get("q", None), request.GET.get("q", None))
    lat = request.GET.get("lat", None)
    lng = request.GET.get("lng", None)
    budget = request.GET.get("budget", None)
    gender = request.GET.get("gender", None)
    rating = request.GET.get("rating", None)
    date = request.GET.get("date", None)
    hour = request.GET.get("hour", None)
    tags = request.GET.get("tags", None)
    sorted_by = request.GET.get("sorted_by", None)
    point = None
    start_price = 0
    end_price = 15000
    # point = Point(23.31326937672202, 42.68336526966131)
    type_of_order = ['listing_id', '-listing_id', '-likes', '-comments']
    currencies = dict(CURRENCY)
    if query:
        query = query.split(" ")
        if tags:
            tags = tags.split(',')
            query = query + tags
        sq = SQ()
        for q in query:
            sq.add(SQ(tags__contains=q), SQ.OR)
            sq.add(SQ(title__contains=q), SQ.OR)
            sq.add(SQ(description__contains=q), SQ.OR)
    else:
        sq = SQ()

    if budget:
        start_price = int(budget.split('-')[0])
        end_price = int(budget.split('-')[1])

    if gender:
        gender = int(gender)
        if gender == 0:
            gender = [0]
        elif gender == 2:
            gender = [2]
        else:
            gender = [0, 1, 2]
    else:
        gender = [0, 1, 2]

    if lat and lng and lat != 'undefined' and lng != 'undefined':
        print(lat, lng)
        lat = float(lat)
        lng = float(lng)
        point = Point(lat, lng)

    if date and date.isdigit():
        date = int(date)
    else:
        date = None

    if not (hour is None or hour == "-1"):
        hour = int(hour)
    else:
        hour = -1

    if rating:
        rating = int(rating)
    else:
        rating = 0

    if sorted_by:
        sorted_by = type_of_order[int(sorted_by)]
    else:
        sorted_by = 'listing_id'

    partial_query = SearchQuerySet().models(Listing).filter(sq)
    partial_query = [l.price for l in partial_query]

    if len(partial_query) >= 2:
        price_list = [min(partial_query), max(partial_query)]
    else:
        price_list = [0, 500]

    price_list = [min(price_list), max(price_list)]
    if point:
        _listings = SearchQuerySet().models(Listing).filter(sq).filter(
            gender__in=gender,
            price__gte=start_price,
            price__lte=end_price,
            status=1,
            rating__gte=rating).dwithin('location', point, D(km=1500000))
    else:
        _listings = SearchQuerySet().models(Listing).filter(sq).filter(
            gender__in=gender,
            price__gte=start_price,
            price__lte=end_price,
            status=1,
            rating__gte=rating)

    if date and not (hour is not None and hour == -1):
        ''' hour in seconds is the time in utc seconds from the date to the required time '''
        work_days = [("mon_start", "mon_end"), ("tues_start", "tues_end"),
                     ("wed_start", "wed_end"), ("thurs_start", "thurs_end"),
                     ("fri_start", "fri_end"), ("sat_start", "sat_end"),
                     ("sun_start", "sun_end")]

        # get listings IDs which was already filtered and make in format (1,1,3,3,5)
        listings_ids = [l.listing_id for l in _listings]
        listings_ids = str(listings_ids)[1:-1] if listings_ids else 'NULL'

        # get the index of the day from the week
        week_days = datetime.datetime.fromtimestamp(date).strftime('%w')
        week_days = work_days[int(week_days)]

        # hour in seconds
        hour_in_seconds = 28800 + hour * 1800

        # start range is a variable which will be used for the following things: time in seconds from booking start
        start_range = date + hour_in_seconds

        query = '''SELECT DISTINCT listing.id, listing.title, listing.likes, listing.price, listing.artist_id, listing.comments, listing.currency, listing.picture_cover, artist.lat, artist.lng, artist.style, artist.avatar, artist_user.first_name as artist_name, salon.id as salon_id, salon.avatar as salon_avatar, salon_user.first_name as salon_name
        FROM listings_listing AS listing
        JOIN artists_artist AS artist ON artist.id = listing.artist_id
        JOIN artists_worktime AS worktime ON worktime.artist_id = artist.id
        LEFT JOIN booking_booking AS booking ON booking.artist_id = artist.id
        LEFT JOIN artists_busy AS busy ON busy.artist_id = artist.id
        LEFT JOIN salons_salon AS salon ON artist.salon_id = salon.id
        LEFT JOIN auth_user AS salon_user ON salon.user_id = salon_user.id
        LEFT JOIN auth_user AS artist_user ON artist.user_id = artist_user.id
        WHERE listing.id IN ({listings_ids})
        AND worktime.{first_week_day} <= {hour} AND worktime.{second_week_day} >= ({hour} + listing.duration/1800)
        AND (booking.start_time >= listing.duration + {start_range}
        OR booking.end_time <= {start_range} OR booking.start_time IS NULL)
        AND (busy.start_time >= listing.duration + {start_range}
        OR busy.end_time <= {start_range} OR busy.start_time IS NULL)'''.format(
            listings_ids=listings_ids,
            first_week_day=week_days[0],
            hour=hour,
            second_week_day=week_days[1],
            start_range=start_range)

        cursor = connection.cursor()
        cursor.execute(query)
        _listings = dictfetchall(cursor)

        listings = []
        listing = {}
        for l in _listings:
            listing = {
                "lat":
                l["lat"],
                "lng":
                l["lng"],
                "id":
                l["id"],
                "picture":
                l["picture_cover"],
                "style":
                STYLE_INDEXES[l["style"]][1],
                "title":
                l["title"],
                "likes":
                l["likes"],
                "price":
                l["price"],
                "currency":
                currencies[l["currency"]],
                "comments":
                l["comments"],
                "artist_id":
                l["artist_id"],
                "artist_name":
                l["artist_name"],
                "avatar":
                MEDIA_ROOT + l["avatar"][7:] if l["avatar"] else '',
                "salon_id":
                l["salon_id"],
                "salon_name":
                l["salon_name"],
                "salon_avatar":
                MEDIA_ROOT + l["salon_avatar"][7:] if l["salon_avatar"] else ''
            }
            listings.append(listing)
    elif date:
        work_days = [("mon_start", "mon_end"), ("tues_start", "tues_end"),
                     ("wed_start", "wed_end"), ("thurs_start", "thurs_end"),
                     ("fri_start", "fri_end"), ("sat_start", "sat_end"),
                     ("sun_start", "sun_end")]

        listings_ids = [l.listing_id for l in _listings]
        listings_ids = str(listings_ids)[1:-1] if listings_ids else 'NULL'

        week_days = datetime.datetime.fromtimestamp(date).strftime('%w')
        week_days = work_days[int(week_days)]

        query = '''SELECT DISTINCT listing.id, listing.title, listing.likes, listing.price, listing.artist_id, listing.comments, listing.currency, listing.picture_cover, artist.lat, artist.lng, artist.style, artist.avatar, artist_user.first_name as artist_name, salon.id as salon_id, salon.avatar as salon_avatar, salon_user.first_name as salon_name
            FROM listings_listing AS listing
            JOIN artists_artist AS artist ON artist.id = listing.artist_id
            JOIN artists_worktime AS worktime ON worktime.artist_id = artist.id
            LEFT JOIN booking_booking AS booking ON booking.artist_id = artist.id
            LEFT JOIN artists_busy AS busy ON busy.artist_id = artist.id
            LEFT JOIN salons_salon AS salon ON artist.salon_id = salon.id
            LEFT JOIN auth_user AS salon_user ON salon.user_id = salon_user.id
            LEFT JOIN auth_user AS artist_user ON artist.user_id = artist_user.id
            WHERE listing.id IN ({listings_ids})
            AND NOT worktime.{week_day} = -1'''.format(
            listings_ids=listings_ids, week_day=week_days[0])

        cursor = connection.cursor()
        cursor.execute(query)
        _listings = dictfetchall(cursor)

        listings = []
        listing = {}
        for l in _listings:
            listing = {
                "lat":
                l["lat"],
                "lng":
                l["lng"],
                "id":
                l["id"],
                "picture":
                l["picture_cover"],
                "style":
                STYLE_INDEXES[l["style"]][1],
                "title":
                l["title"],
                "likes":
                l["likes"],
                "price":
                l["price"],
                "currency":
                currencies[l["currency"]],
                "comments":
                l["comments"],
                "artist_id":
                l["artist_id"],
                "artist_name":
                l["artist_name"],
                "avatar":
                MEDIA_ROOT + l["avatar"][7:] if l["avatar"] else '',
                "salon_id":
                l["salon_id"],
                "salon_name":
                l["salon_name"],
                "salon_avatar":
                MEDIA_ROOT + l["salon_avatar"][7:] if l["salon_avatar"] else ''
            }
            listings.append(listing)

    else:
        listings = []
        listing = {}
        for l in _listings:
            listing = {
                "lat": l.location.x,
                "lng": l.location.y,
                "id": l.listing_id,
                "picture": l.get_picture,
                "style": l.style,
                "title": l.title,
                "likes": l.likes,
                "price": l.price,
                "currency": currencies[l.currency],
                "comments": l.comments,
                "artist_id": l.artist_id,
                "artist_name": l.artist_name,
                "avatar": l.artist_avatar,
                "salon_id": l.salon_id,
                "salon_name": l.salon_name,
                "salon_avatar": l.salon_avatar,
            }
            listings.append(listing)

    artists_ids = [l["artist_id"] for l in listings]
    artists_ratings = Review.objects.filter(
        artist_id__in=artists_ids).values("artist_id").annotate(
            average_rating=Avg('rating'))
    final_map = {}
    for e in artists_ratings:
        final_map[e["artist_id"]] = e["average_rating"]

    for l in listings:
        rating = final_map.get(l['artist_id'], None)
        l["artist_rating"] = rating

    return render(request, 'service/service.html', {
        "listings": listings,
        "price_list": price_list
    })
Exemplo n.º 12
0
def search(request):
    styles = {
        "hairstyle": "hair",
        "nails-design": "nails",
        "make-up": "make up",
    }
    query = styles.get(request.GET.get("q", None), request.GET.get("q", None))
    lat = request.GET.get("lat", None)
    lng = request.GET.get("lng", None)
    budget = request.GET.get("budget", None)
    gender = request.GET.get("gender", None)
    rating = request.GET.get("rating", None)
    date = request.GET.get("date", None)
    hour = request.GET.get("hour", None)
    tags = request.GET.get("tags", None)
    sorted_by = request.GET.get("sorted_by", None)
    point = None
    start_price = 0
    end_price = 15000
    # point = Point(23.31326937672202, 42.68336526966131)
    type_of_order = ['listing_id', '-listing_id', '-likes', '-comments']
    currencies = dict(CURRENCY)
    if query:
        query = query.split(" ")
        if tags:
            tags = tags.split(',')
            query = query + tags
        sq = SQ()
        for q in query:
            sq.add(SQ(tags__contains=q), SQ.OR)
            sq.add(SQ(title__contains=q), SQ.OR)
            sq.add(SQ(description__contains=q), SQ.OR)
    else:
        sq = SQ()

    if budget:
        start_price = int(budget.split('-')[0])
        end_price = int(budget.split('-')[1])

    if gender:
        gender = int(gender)
        if gender == 0:
            gender = [0]
        elif gender == 2:
            gender = [2]
        else:
            gender = [0, 1, 2]
    else:
        gender = [0, 1, 2]

    if lat and lng and lat != 'undefined' and lng != 'undefined':
        print(lat, lng)
        lat = float(lat)
        lng = float(lng)
        point = Point(lat, lng)

    if date and date.isdigit():
        date = int(date)
    else:
        date = None

    if not (hour is None or hour == "-1"):
        hour = int(hour)
    else:
        hour = -1

    if rating:
        rating = int(rating)
    else:
        rating = 0

    if sorted_by:
        sorted_by = type_of_order[int(sorted_by)]
    else:
        sorted_by = 'listing_id'

    partial_query = SearchQuerySet().models(Listing).filter(sq)
    partial_query = [l.price for l in partial_query]

    if len(partial_query) >= 2:
        price_list = [min(partial_query), max(partial_query)]
    else:
        price_list = [0, 500]

    price_list = [min(price_list), max(price_list)]
    if point:
        _listings = SearchQuerySet().models(Listing).filter(sq).filter(
            gender__in=gender,
            price__gte=start_price,
            price__lte=end_price,
            status=1,
            rating__gte=rating).dwithin('location', point, D(km=1500000))
    else:
        _listings = SearchQuerySet().models(Listing).filter(sq).filter(
            gender__in=gender,
            price__gte=start_price,
            price__lte=end_price,
            status=1,
            rating__gte=rating)

    if date and not (hour is not None and hour == -1):
        ''' hour in seconds is the time in utc seconds from the date to the required time '''
        work_days = [("mon_start", "mon_end"), ("tues_start", "tues_end"), ("wed_start", "wed_end"), ("thurs_start", "thurs_end"), ("fri_start", "fri_end"), ("sat_start", "sat_end"), ("sun_start", "sun_end")]

        # get listings IDs which was already filtered and make in format (1,1,3,3,5)
        listings_ids = [l.listing_id for l in _listings]
        listings_ids = str(listings_ids)[1:-1] if listings_ids else 'NULL'

        # get the index of the day from the week
        week_days = datetime.datetime.fromtimestamp(date).strftime('%w')
        week_days = work_days[int(week_days)]

        # hour in seconds
        hour_in_seconds = 28800 + hour*1800

        # start range is a variable which will be used for the following things: time in seconds from booking start
        start_range = date + hour_in_seconds

        query = '''SELECT DISTINCT listing.id, listing.title, listing.likes, listing.price, listing.artist_id, listing.comments, listing.currency, listing.picture_cover, artist.lat, artist.lng, artist.style, artist.avatar, artist_user.first_name as artist_name, salon.id as salon_id, salon.avatar as salon_avatar, salon_user.first_name as salon_name
        FROM listings_listing AS listing
        JOIN artists_artist AS artist ON artist.id = listing.artist_id
        JOIN artists_worktime AS worktime ON worktime.artist_id = artist.id
        LEFT JOIN booking_booking AS booking ON booking.artist_id = artist.id
        LEFT JOIN artists_busy AS busy ON busy.artist_id = artist.id
        LEFT JOIN salons_salon AS salon ON artist.salon_id = salon.id
        LEFT JOIN auth_user AS salon_user ON salon.user_id = salon_user.id
        LEFT JOIN auth_user AS artist_user ON artist.user_id = artist_user.id
        WHERE listing.id IN ({listings_ids})
        AND worktime.{first_week_day} <= {hour} AND worktime.{second_week_day} >= ({hour} + listing.duration/1800)
        AND (booking.start_time >= listing.duration + {start_range}
        OR booking.end_time <= {start_range} OR booking.start_time IS NULL)
        AND (busy.start_time >= listing.duration + {start_range}
        OR busy.end_time <= {start_range} OR busy.start_time IS NULL)'''.format(listings_ids=listings_ids, first_week_day=week_days[0], hour=hour, second_week_day=week_days[1], start_range=start_range)

        cursor = connection.cursor()
        cursor.execute(query)
        _listings = dictfetchall(cursor)

        listings = []
        listing = {}
        for l in _listings:
            listing = {
                "lat": l["lat"],
                "lng": l["lng"],
                "id": l["id"],
                "picture": l["picture_cover"],
                "style": STYLE_INDEXES[l["style"]][1],
                "title": l["title"],
                "likes": l["likes"],
                "price": l["price"],
                "currency": currencies[l["currency"]],
                "comments": l["comments"],
                "artist_id": l["artist_id"],
                "artist_name": l["artist_name"],
                "avatar": MEDIA_ROOT + l["avatar"][7:] if l["avatar"] else '',
                "salon_id": l["salon_id"],
                "salon_name": l["salon_name"],
                "salon_avatar": MEDIA_ROOT + l["salon_avatar"][7:] if l["salon_avatar"] else ''
            }
            listings.append(listing)
    elif date:
        work_days = [("mon_start", "mon_end"), ("tues_start", "tues_end"), ("wed_start", "wed_end"), ("thurs_start", "thurs_end"), ("fri_start", "fri_end"), ("sat_start", "sat_end"), ("sun_start", "sun_end")]

        listings_ids = [l.listing_id for l in _listings]
        listings_ids = str(listings_ids)[1:-1] if listings_ids else 'NULL'

        week_days = datetime.datetime.fromtimestamp(date).strftime('%w')
        week_days = work_days[int(week_days)]

        query = '''SELECT DISTINCT listing.id, listing.title, listing.likes, listing.price, listing.artist_id, listing.comments, listing.currency, listing.picture_cover, artist.lat, artist.lng, artist.style, artist.avatar, artist_user.first_name as artist_name, salon.id as salon_id, salon.avatar as salon_avatar, salon_user.first_name as salon_name
            FROM listings_listing AS listing
            JOIN artists_artist AS artist ON artist.id = listing.artist_id
            JOIN artists_worktime AS worktime ON worktime.artist_id = artist.id
            LEFT JOIN booking_booking AS booking ON booking.artist_id = artist.id
            LEFT JOIN artists_busy AS busy ON busy.artist_id = artist.id
            LEFT JOIN salons_salon AS salon ON artist.salon_id = salon.id
            LEFT JOIN auth_user AS salon_user ON salon.user_id = salon_user.id
            LEFT JOIN auth_user AS artist_user ON artist.user_id = artist_user.id
            WHERE listing.id IN ({listings_ids})
            AND NOT worktime.{week_day} = -1'''.format(listings_ids=listings_ids, week_day=week_days[0])

        cursor = connection.cursor()
        cursor.execute(query)
        _listings = dictfetchall(cursor)

        listings = []
        listing = {}
        for l in _listings:
            listing = {
                "lat": l["lat"],
                "lng": l["lng"],
                "id": l["id"],
                "picture": l["picture_cover"],
                "style": STYLE_INDEXES[l["style"]][1],
                "title": l["title"],
                "likes": l["likes"],
                "price": l["price"],
                "currency": currencies[l["currency"]],
                "comments": l["comments"],
                "artist_id": l["artist_id"],
                "artist_name": l["artist_name"],
                "avatar": MEDIA_ROOT + l["avatar"][7:] if l["avatar"] else '',
                "salon_id": l["salon_id"],
                "salon_name": l["salon_name"],
                "salon_avatar": MEDIA_ROOT + l["salon_avatar"][7:] if l["salon_avatar"] else ''
            }
            listings.append(listing)

    else:
        listings = []
        listing = {}
        for l in _listings:
            listing = {
                "lat": l.location.x,
                "lng": l.location.y,
                "id": l.listing_id,
                "picture": l.get_picture,
                "style": l.style,
                "title": l.title,
                "likes": l.likes,
                "price": l.price,
                "currency": currencies[l.currency],
                "comments": l.comments,
                "artist_id": l.artist_id,
                "artist_name": l.artist_name,
                "avatar": l.artist_avatar,
                "salon_id": l.salon_id,
                "salon_name": l.salon_name,
                "salon_avatar": l.salon_avatar,
            }
            listings.append(listing)

    artists_ids = [l["artist_id"] for l in listings]
    artists_ratings = Review.objects.filter(artist_id__in=artists_ids).values("artist_id").annotate(average_rating=Avg('rating'))
    final_map = {}
    for e in artists_ratings:
        final_map[e["artist_id"]] = e["average_rating"]

    for l in listings:
        rating = final_map.get(l['artist_id'], None)
        l["artist_rating"] = rating

    return render(request, 'service/service.html', {"listings": listings, "price_list": price_list})