Ejemplo n.º 1
0
def host_list(request):
    """
    Retrieve all NGHost.
    """
    hosts = NGHost.objects.all()
    serializer = NGHostSerializer(hosts, many=True)
    return JSONResponse(serializer.data)
Ejemplo n.º 2
0
def host_last(request, host):
    """
    Retrieve all last NGNews.
    """
    try:
        host = NGHost.objects.get(host=host)
    except NGHost.DoesNotExist:
        return HttpResponse(status=400)

    limit = request.GET.get('limit', '1000')
    if limit == '' or not limit.isdigit():
        return HttpResponse(status=400)
    limit = int(limit)
    if not limit > 0:
        return HttpResponse(status=400)

    groups = NGGroup.objects.filter(host=host)

    n_list = NGNews.objects\
                 .filter(groups__in=groups)\
                 .order_by('-date')[:limit]

    names = request.GET.get('names', 'false')
    names = True if names == '' else (True if names == 'true' else False)

    if names:
        serializer = NGNewsSerializerWithNames(n_list, many=True)
    else:
        serializer = NGNewsSerializer(n_list, many=True)

    return JSONResponse(serializer.data)
Ejemplo n.º 3
0
def group_list(request, host):
    """
    Retrieve all NGGroup.
    """
    hosts = NGGroup.objects.filter(host__host=host)\
        .order_by('name')
    serializer = NGGroupSerializer(hosts, many=True)
    return JSONResponse(serializer.data)
Ejemplo n.º 4
0
def host_detail(request, host_url):
    """
    Retrieve a NGHost.
    """
    try:
        host = NGHost.objects.get(host=host_url)
    except NGHost.DoesNotExist:
        return HttpResponse(status=400)

    serializer = NGHostSerializer(host)
    return JSONResponse(serializer.data)
Ejemplo n.º 5
0
def news_list_refresh(request, host, group):
    """
    Retrieve all NGNews.
    """
    try:
        host = NGHost.objects.get(host=host)
    except NGHost.DoesNotExist:
        return HttpResponse(status=400)

    end_date = request.GET.get('end_date', '')

    if end_date != '':
        try:
            e_date = datetime.strptime(end_date, '%Y-%m-%dT%H:%M:%S%z')
            e_date = e_date.replace(tzinfo=None)
        except ValueError as e:
            return HttpResponse(status=400)
    else:
        e_date = datetime(1970, 1, 1, 00, 00)

    limit = request.GET.get('limit', '1000')
    if limit == '' or not limit.isdigit():
        return HttpResponse(status=400)
    limit = int(limit)
    if not limit > 0:
        return HttpResponse(status=400)

    try:
        group = NGGroup.objects.get(host=host, name=group)
    except NGGroup.DoesNotExist:
        return HttpResponse(status=400)

    n_list = NGNews.objects\
                 .filter(groups__in=[group], date__gt=e_date, father='')\
                 .order_by('-date')[:limit]

    names = request.GET.get('names', 'false')
    names = True if names == '' else (True if names == 'true' else False)

    if names:
        serializer = NGNewsSerializerWithNames(n_list, many=True)
    else:
        serializer = NGNewsSerializer(n_list, many=True)

    return JSONResponse(serializer.data)
Ejemplo n.º 6
0
def news_list(request, host, group):
    """
    Retrieve all NGNews.
    """
    try:
        host = NGHost.objects.get(host=host)
    except NGHost.DoesNotExist:
        return HttpResponse(status=400)

    start_date = request.GET.get('start_date', '')

    if start_date != '':
        try:
            s_date = datetime.strptime(start_date, '%Y-%m-%dT%H:%M:%S%z')
            s_date = s_date.replace(tzinfo=None)
        except ValueError:
            return HttpResponse(status=400)
    else:
        s_date = datetime.now()

    limit = int(request.GET.get('limit', '1000'))
    if limit < 1:
        return HttpResponse(status=400)

    try:
        group = NGGroup.objects.get(host=host, name=group)
    except NGGroup.DoesNotExist:
        return HttpResponse(status=400)

    n_list = NGNews.objects\
                 .filter(groups__in=[group], date__lt=s_date, father='')\
                 .order_by('-date')[:limit]

    names = request.GET.get('names', 'false')
    names = True if names == '' else (True if names == 'true' else False)

    if names:
        serializer = NGNewsSerializerWithNames(n_list, many=True)
    else:
        serializer = NGNewsSerializer(n_list, many=True)

    return JSONResponse(serializer.data)
Ejemplo n.º 7
0
def news_detail(request, news_id):
    """
    Retrieve a news details with all answers
    """
    news = None
    while True:
        try:
            news = NGNews.objects.get(id=news_id)
        except NGGroup.DoesNotExist:
            return HttpResponse(status=400)
        if news.father == '':
            break
        else:
            try:
                father = NGNews.objects.get(message_id=news.father)
                news_id = father.id
            except:
                return HttpResponse(status=500)

    serializer = NGNewsDetailSerializer(news)
    return JSONResponse(serializer.to_representation(news))
Ejemplo n.º 8
0
def get_subscribed_groups(request):

    service = request.POST.get('service', '')
    registration_id = request.POST.get('registration_id', '')
    host = request.POST.get('host', '')

    if service == '' or registration_id == '' or host == '':
        return HttpResponse(status=404)

    if not (service == 'android' or service == 'ios'):
        return JsonResponse(
            {
                'error': 0,
                'message': "The service should be either 'android' or 'ios'"
            },
            safe=False,
            status=403)

    host_obj = get_object_or_404(NGHost, host=host)

    # Try to find if an anonymous user already exists for this device
    try:
        if service == 'android':
            device = GCMDevice.objects.get(registration_id=registration_id)
            session = DeviceSession.objects.get(gcm_device=device)
        else:
            device = APNSDevice.objects.get(registration_id=registration_id)
            session = DeviceSession.objects.get(apns_device=device)
    except ObjectDoesNotExist:
        return HttpResponse(status=404)

    user = session.get_user()

    data = {
        'subscribed_groups':
        [g.name for g in NGGroup.objects.filter(followers__in=[user])]
    }
    return JSONResponse(data)
Ejemplo n.º 9
0
def search(request, host=None, group=None):

    term = request.GET.get('term', '')
    if len(term) < 2:
        return HttpResponse(status=400)

    limit = request.GET.get('limit', '1000')
    if limit == '' or not limit.isdigit():
        return HttpResponse(status=400)
    limit = int(limit)
    if not limit > 0:
        return HttpResponse(status=400)

    start_date = request.GET.get('start_date', '')
    if start_date != '':
        try:
            s_date = datetime.strptime(start_date, '%Y-%m-%dT%H:%M:%S%z')
            s_date = s_date.replace(tzinfo=None)
        except ValueError:
            return HttpResponse(status=400)
    else:
        s_date = datetime(2099, 1, 1, 00, 00)

    author = request.GET.get('author', 'false')
    author = True if author == '' else (True if author == 'true' else False)
    title = request.GET.get('title', 'false')
    title = True if title == '' else (True if title == 'true' else False)
    message = request.GET.get('message', 'false')
    message = True if message == '' else (True if message == 'true' else False)

    if not author and not title and not message:
        author = True
        title = True
        message = True

    if host and group:
        try:
            host = NGHost.objects.get(host=host)
            groups = [NGGroup.objects.get(host=host, name=group)]
        except:
            return HttpResponse(status=404)
    elif host:
        try:
            host = NGHost.objects.get(host=host)
            groups = NGGroup.objects.filter(host=host)
        except:
            return HttpResponse(status=404)
    else:
        groups = NGGroup.objects.all()

    case = request.GET.get('case', 'false')
    case = True if (case == '') else (True if case == 'true' else False)

    n_list = []
    if author:
        if case:
            a_list = NGNews.objects.filter(groups__in=groups,
                                           date__lt=s_date,
                                           email_from__regex=r'^.*' + term +
                                           '.*$')
        else:
            a_list = NGNews.objects.filter(groups__in=groups,
                                           date__lt=s_date,
                                           email_from__iregex=r'^.*' + term +
                                           '.*$')
        n_list = list(set(n_list) | set(a_list))
    if title:
        if case:
            a_list = NGNews.objects.filter(groups__in=groups,
                                           date__lt=s_date,
                                           subject__regex=r'^.*' + term +
                                           '.*$')
        else:
            a_list = NGNews.objects.filter(groups__in=groups,
                                           date__lt=s_date,
                                           subject__iregex=r'^.*' + term +
                                           '.*$')
        n_list = list(set(n_list) | set(a_list))
    if message:
        if case:
            a_list = NGNews.objects.filter(groups__in=groups,
                                           date__lt=s_date,
                                           contents__regex=r'^.*' + term +
                                           '.*$')
        else:
            a_list = NGNews.objects.filter(groups__in=groups,
                                           date__lt=s_date,
                                           contents__iregex=r'^.*' + term +
                                           '.*$')
        n_list = list(set(n_list) | set(a_list))

    n_list.sort(key=lambda x: x.date, reverse=True)

    n_list = n_list[:limit]

    names = request.GET.get('names', 'false')
    names = True if names == '' else (True if names == 'true' else False)

    if names:
        if host:
            serializer = NGNewsSerializerWithNames(n_list, many=True)
        else:
            serializer = NGNewsSerializerWithNamesAndHost(n_list, many=True)
    else:
        serializer = NGNewsSerializer(n_list, many=True)

    web_render = request.GET.get('web_render', False) != False
    if web_render:
        data = [serializable_object(n, light=True) for n in n_list]
        return JsonResponse(data, safe=False)
    else:
        return JSONResponse(serializer.data)