Esempio n. 1
0
def get_log(request, application_id, log_id):
    log = LogModel.get_by_id(long(log_id))
    if log is None:
        raise Http404
    if request.user.permission != UserPermission.root and request.user.key().id() not in log.application.member_ids:
        # no permission for this application
        raise Http403
    return JsonResponse(log)
Esempio n. 2
0
def update_log(request, application_id, log_id):
    form = LogForm(**json.loads(request.body))
    if not form.validate():
        raise Http400
    log = LogModel.get_by_id(long(log_id))
    if log is None:
        raise Http404
    if request.user.permission != UserPermission.root and request.user.key().id() not in log.application.member_ids:
        # no permission for this application
        raise Http403
    log.is_close = form.is_close.data
    log.put()
    return JsonResponse(log)
Esempio n. 3
0
def get_logs(request, application_id):
    application_id = long(application_id)
    form = SearchForm(**request.GET.dict())

    if application_id == 0:
        # fetch the first application
        if request.user.permission == UserPermission.root:
            applications = ApplicationModel.all().order("title").fetch(1)
        else:
            applications = ApplicationModel.gql(
                "where member_ids in :1 order by title", [request.user.key().id()]
            ).fetch(1)
        if len(applications):
            application = applications[0]
        else:
            # no applications
            return JsonResponse(PageList(0, 20, 0, []))
    else:
        application = ApplicationModel.get_by_id(application_id)
        if application is None:
            raise Http404
        if request.user.permission != UserPermission.root and request.user.key().id() not in application.member_ids:
            # no permission for this application
            raise Http403

    if form.keyword.data:
        source = [x for x in form.keyword.data.split()]
        plus = [x for x in source if not x.startswith("-")]
        minus = [x[1:] for x in source if x.startswith("-")]

        query_string = ""
        if len(plus) > 0:
            keyword = " ".join(plus)
            query_string += "(users:{1}) OR (title:{1}) OR (document:{1}) OR (user_agent:{1}) OR (ip:{1})".replace(
                "{1}", keyword
            )
        if len(minus) > 0:
            keyword = " ".join(minus)
            query_string += "NOT ((users:{1}) OR (title:{1}) OR (document:{1}) OR (user_agent:{1}) OR (ip:{1}))".replace(
                "{1}", keyword
            )
        update_time_desc = search.SortExpression(
            expression="update_time", direction=search.SortExpression.DESCENDING, default_value=0
        )
        options = search.QueryOptions(
            offset=utils.default_page_size * form.index.data,
            limit=utils.default_page_size,
            sort_options=search.SortOptions(expressions=[update_time_desc], limit=1000),
            returned_fields=["doc_id"],
        )
        query = search.Query(query_string=query_string, options=options)
        search_result = search.Index(namespace="Logs", name=str(application.key().id())).search(query)
        total = search_result.number_found
        logs = LogModel.get_by_id([long(x.doc_id) for x in search_result])
        logs = [x for x in logs if not x is None]
    else:
        query = LogModel.all().filter("application =", application.key()).order("-update_time")
        total = query.count()
        logs = query.fetch(utils.default_page_size, form.index.data * utils.default_page_size)
    result = PageList(form.index.data, utils.default_page_size, total, logs).dict()
    result["application"] = {"id": application.key().id(), "title": application.title}
    return JsonResponse(result)