Exemplo n.º 1
0
def likeEvent(request):
    event_id = int(request.POST['event_id'])
    user_id = request.POST['user_id']
    like = int(request.POST['like'])

    response = {}
    event = Event.objects.get(pk=event_id)
    if event:
        try:
            event_like = EventLike.objects.get(user_id=user_id,event=event)
        except Exception as e:
            event_like = None
        if event_like:
            if like == -1:
                event_like.delete()
            else:
                event_like.like = like == 0
                event_like.save()
        elif not like == -1:
            event_like = EventLike(event=event,user_id=user_id,like=like == 0)
            event_like.save()
    
        response['ok'] = 'ok'        
    else:
        response['error'] = 'error: event ' + str(event_id) + ' does not exist.'
    
    return jsonhelper.json_response(response)
Exemplo n.º 2
0
def getEventTypes(request):
    response = {}
    types = []
    for t in EventType.objects.all():
        types.append(t.name)
    response['types'] = types
    
    return jsonhelper.json_response(response)
Exemplo n.º 3
0
def saveComment(request):
    event_id = request.POST['event_id']
    user_id = request.POST['user_id']
    comment = request.POST['comment']
    user_name = request.POST['user_name']
    date = dateutil.parser.parse(request.POST['date'])

    event = Event.objects.get(pk=int(event_id))
    e = EventComment(user_id=user_id,comment=comment,user_name=user_name,event=event,pub_date=date)
    e.save()
    
    return jsonhelper.json_response({'ok':'ok'})
Exemplo n.º 4
0
def getLikeAction(request, event_id, user_id):
    response = {}
    event = Event.objects.get(pk=event_id)
    if event:
        try:
            event_like = EventLike.objects.get(user_id=user_id,event=event)
            like_action = 0
            if event_like.like == False:
                like_action = 1
        except Exception as e:
            like_action = -1

        response['like_action'] = like_action
    else:
        response['error'] = 'event ' + event_id + ' doest not exits in the database.'
    return jsonhelper.json_response(response)
Exemplo n.º 5
0
def getEventsWithin(request, latitude, longitude, distance_meters, max_events, context_data):
    """
    Returns a json with all the events within some distance from some location
    and that doesn't have the primary key in the list discard_pks.
    """
    pnt = GEOSGeometry('POINT('+longitude+' '+latitude+')')
    
    cd_dict = contextDataJSONToDict(context_data)
    has_filter = 'filter_eventtype' in cd_dict

    in_contextdata = generateContextData(context_data)
    if has_filter:
        cd_filter = cd_dict['filter_eventtype']
        eventtype_filter = EventType.objects.filter(name=cd_filter)
        if eventtype_filter:
            events = Event.objects.filter(geo_coord__distance_lt=(pnt,float(distance_meters)),
                                          event_type=eventtype_filter)
            events = sorted([e for e in events], key=lambda event: event.pub_date)[::-1]
        else:
            has_filter = False
    
    if not has_filter:
        events = Event.objects.filter(geo_coord__distance_lt=(pnt,float(distance_meters)))
    
    if not str(in_contextdata.from_supplier).lower() == 'filtro':
        events = sort_events_by_relevance([e for e in events], in_contextdata)
       
    response = {'size': len(events)}
    i = 0

    for e in events:
        if i == int(max_events):
            break
        #sends the name string of event_type and the keywords instead of sending
        #their primary keys.
        event_type = EventType.objects.get(pk=e.event_type.pk).name
        keywords = [EventKeyword.objects.get(pk=key.pk).keyword for key in e.keywords.all()]
        json = eval(jsonhelper.toJSON(e))
        json['fields']['event_type'] = event_type
        json['fields']['keywords'] = keywords
        json['fields']['likes'] = countLikes(e.pk)
        json['fields']['dislikes'] = countDislikes(e.pk)
        response['event_'+str(i)] = json
        i += 1
            
    #returns a json with the number of fetched events and with them as well.
    return jsonhelper.json_response(response)
Exemplo n.º 6
0
def getComments(request, event_id):
    response = {}
    ok = False
    try:
        event = Event.objects.get(pk=event_id)
        ok = True
    except Exception as e:
        response['error'] = 'invalid event_id'        

    if ok:
        response = {}
        i = 0
        for ec in EventComment.objects.filter(event=event):
            response['comment_'+str(i)] = {'user_id': ec.user_id, 'comment': ec.comment, 'user_name': ec.user_name, 'date': re.sub('(\+\d+:\d+)?(\-\d+:\d+)?', '', str(ec.pub_date))}
            i += 1
        response['size'] = i

    return jsonhelper.json_response(response)
Exemplo n.º 7
0
def addNewEvent(request):
    """
    Method that adds an event to the spatial database.
    """
    response = {'response':'ok'}
    try:
        #event_type and event_keywords come as names, not IDs. So we need first
        #to get their primary keys so we can save the Event properly.
        event_type_name = request.POST['event_type'].encode('utf-8').lower().strip()
        event_type, created = EventType.objects.get_or_create(name=event_type_name)
    
        event_keywords = request.POST['event_keywords'].encode('utf-8')
        event_keywords = event_keywords[1:-1].split(',')
        keywords = []
        for keyword in event_keywords:
            k, created = EventKeyword.objects.get_or_create(keyword=keyword.strip())
            keywords.append(k)

        #gets what is interesting about the Event and makes it compatible 
        #with the server's format.
        event_json = eval(request.POST['event'].encode('utf-8'))[0]['fields']

        geo_coord = event_json['geo_coord'].replace(',',' ')
        geo_coord = GEOSGeometry('POINT('+geo_coord+')')

        event = Event(title=event_json['title'],description=event_json['description'],
            pub_date=event_json['pub_date'],geo_coord=geo_coord)
        event.event_type = event_type
        event.save()
        for k in keywords:
            event.keywords.add(k)
        event.save()
        
        contextdata = generateContextData(request.POST['context_data'])
        contextdata.event = event
        contextdata.save()
        
        response['pk'] = event.pk
    except DeserializationError as error:
        response['response'] = str(error)
        
    #returns a json with an error or an ok and the primary key from the added event.
    return jsonhelper.json_response(response)
Exemplo n.º 8
0
def countLikesDislikes(event_id):
    response['likes'] = countLikes(event_id)
    response['dislikes'] = countDislikes(event_id)
    
    return jsonhelper.json_response(response)