Exemple #1
0
def activity_api(request, username):
    from bson.objectid import ObjectId

    def stringify_unknowns(obj):
        if isinstance(obj, ObjectId):
            return str(obj)
        if isinstance(obj, datetime):
            return obj.strftime(DATETIME_FORMAT)
        return None
    try:
        query_args = {
            'username': username,
            'query': json.loads(request.GET.get('query'))
            if request.GET.get('query') else {},
            'fields': json.loads(request.GET.get('fields'))
            if request.GET.get('fields') else [],
            'sort': json.loads(request.GET.get('sort'))
            if request.GET.get('sort') else {}
        }
        if 'start' in request.GET:
            query_args["start"] = int(request.GET.get('start'))
        if 'limit' in request.GET:
            query_args["limit"] = int(request.GET.get('limit'))
        if 'count' in request.GET:
            query_args["count"] = True \
                if int(request.GET.get('count')) > 0 else False
        cursor = AuditLog.query_mongo(**query_args)
    except ValueError as e:
        return HttpResponseBadRequest(e.__str__())
    records = list(record for record in cursor)
    response_text = json.dumps(records, default=stringify_unknowns)
    if 'callback' in request.GET and request.GET.get('callback') != '':
        callback = request.GET.get('callback')
        response_text = ("%s(%s)" % (callback, response_text))
    return HttpResponse(response_text, mimetype='application/json')
Exemple #2
0
def activity_api(request, username):
    from bson.objectid import ObjectId

    def stringify_unknowns(obj):
        if isinstance(obj, ObjectId):
            return str(obj)
        if isinstance(obj, datetime):
            return obj.strftime(DATETIME_FORMAT)
        # raise TypeError
        return None

    try:
        query_args = {
            "username": username,
            "query": json.loads(request.GET.get("query")) if request.GET.get("query") else {},
            "fields": json.loads(request.GET.get("fields")) if request.GET.get("fields") else [],
            "sort": json.loads(request.GET.get("sort")) if request.GET.get("sort") else {},
        }
        if "start" in request.GET:
            query_args["start"] = int(request.GET.get("start"))
        if "limit" in request.GET:
            query_args["limit"] = int(request.GET.get("limit"))
        if "count" in request.GET:
            query_args["count"] = True if int(request.GET.get("count")) > 0 else False
        cursor = AuditLog.query_mongo(**query_args)
    except ValueError, e:
        return HttpResponseBadRequest(e.__str__())
Exemple #3
0
def activity_api(request, username):
    from bson.objectid import ObjectId

    def stringify_unknowns(obj):
        if isinstance(obj, ObjectId):
            return str(obj)
        if isinstance(obj, datetime):
            return obj.strftime(DATETIME_FORMAT)
        return None
    try:
        query_args = {
            'username': username,
            'query': json.loads(request.GET.get('query'))
            if request.GET.get('query') else {},
            'fields': json.loads(request.GET.get('fields'))
            if request.GET.get('fields') else [],
            'sort': json.loads(request.GET.get('sort'))
            if request.GET.get('sort') else {}
        }
        if 'start' in request.GET:
            query_args["start"] = int(request.GET.get('start'))
        if 'limit' in request.GET:
            query_args["limit"] = int(request.GET.get('limit'))
        if 'count' in request.GET:
            query_args["count"] = True \
                if int(request.GET.get('count')) > 0 else False
        cursor = AuditLog.query_mongo(**query_args)
    except ValueError, e:
        return HttpResponseBadRequest(e.__str__())
Exemple #4
0
    def test_audit_log_call(self):
        account_user = User(username="******")
        request_user = User(username="******")
        request = RequestFactory().get("/")
        # create a log
        audit = {}
        audit_log(Actions.FORM_PUBLISHED, request_user, account_user,
                  "Form published", audit, request)
        # function should just run without exception so we are good at this
        # point query for this log entry
        sort = {"created_on": -1}
        cursor = AuditLog.query_mongo(
            account_user.username, None, None, sort, 0, 1)
        result = AuditLog.query_mongo(account_user.username, count=True)

        self.assertTrue(result[0]['count'] > 0)
        record = next(cursor)
        self.assertEqual(record['account'], "alice")
        self.assertEqual(record['user'], "bob")
        self.assertEqual(record['action'], Actions.FORM_PUBLISHED)
 def test_audit_log_call(self):
     account_user = User(username="******")
     request_user = User(username="******")
     request = RequestFactory().get("/")
     # create a log
     audit = {}
     audit_log(Actions.FORM_PUBLISHED, request_user, account_user,
               "Form published", audit, request)
     # function should just run without exception so we are good at this
     # point query for this log entry
     sort = {"created_on": -1}
     cursor = AuditLog.query_mongo(
         account_user.username, None, None, sort, 0, 1)
     self.assertTrue(cursor.count() > 0)
     record = cursor.next()
     self.assertEqual(record['account'], "alice")
     self.assertEqual(record['user'], "bob")
     self.assertEqual(record['action'], Actions.FORM_PUBLISHED)
 def test_audit_log_call(self):
     account_user = User(username="******")
     request_user = User(username="******")
     request = RequestFactory().get("/")
     # create a log
     audit = {}
     audit_log(Actions.FORM_PUBLISHED, request_user, account_user,
               "Form published", audit, request)
     # function should just run without exception so we are good at this
     # point query for this log entry
     sort = '-created_on'
     cursor = AuditLog.query_data(account_user.username, None, None, sort,
                                  0, 1)
     records = [rec for rec in cursor]
     self.assertTrue(len(records) > 0)
     record = records[0]
     self.assertEqual(record['account'], "alice")
     self.assertEqual(record['user'], "bob")
     self.assertEqual(record['action'], Actions.FORM_PUBLISHED)