Exemple #1
0
def logstuff(reqlog, template, rendargs, nolog=None, listing=None):
    """
    Logs the response in the database if logging is enabled
    """
    if logEnabled():
        if nolog is None:
            nolog = []
        reslog = ResponseLog.create(request=reqlog, template=template)
        reslog.save()

        args = []
        for key in rendargs:
            if key not in nolog and not isinstance(rendargs[key],(forms.Form,forms.formsets.BaseFormSet)):
                try:
                    # check if an object is a list or tuple (but not string)
                    # http://stackoverflow.com/questions/1835018/python-check-if-an-object-is-a-list-or-tuple-but-not-string
                    assert not isinstance(rendargs.get(key), basestring)
                    args = args + [ResponseArgument(response=reslog, name=key, value=handleFunnyCharacters(v)[:1024])
                                   for v in rendargs.get(key) if not isinstance(v,(forms.Form,forms.formsets.BaseFormSet))]
                except (TypeError, AssertionError):
                    # not iterable
                    v = str(rendargs.get(key))
                    args = args + [ResponseArgument(response=reslog, name=key, value=handleFunnyCharacters(v)[:1024])]

        ResponseArgument.objects.bulk_create(args)
        if listing:
            recordList(reslog, listing)
Exemple #2
0
def recordRequest(request):
    """
    Logs the request in the database
    """
    if logEnabled():
        data = getDataFromRequest(request)
        reqlog = RequestLog.create(request)
        reqlog.save()
        args = []
        for a in data.keys():
            args = args + [RequestArgument(request=reqlog, name=a, value=handleFunnyCharacters(v)) for v in data.getlist(a) if v.strip() != '']

        RequestArgument.objects.bulk_create(args)

        return reqlog
    else:
        return None
Exemple #3
0
def recordList(reslog, results):
    """
    Logs a ranked list of results
    """
    if logEnabled():
        if results:
            ranks = range(1, min(201, len(results)))
            ranks.extend([(2 ** p) for p in range(8, 1 + int(floor(log(len(results), 2))))])
            ranks.append(len(results))
            items = [ResponseList(response=reslog,
                                  rank=r,
#                                  fclass=str(getListItemProperty(results[r - 1], '__class__')),
                                  fclass=goodLookingModelName(concrete_model(results[r - 1])),
#                                  fid=getFid(results[r - 1],  pk(getListItemProperty(results[r - 1], '__class__')))
                                  fid=pkValue(results[r - 1])
                                  )
                     for r in ranks]
            try:
                ResponseList.objects.bulk_create(items)
            except TypeError as e:
#                print('dlogging.py is confused by {0}'.format(getListItemProperty(results[r - 1], '__class__')))
                print('dlogging.py is confused by {0} instance'.format(goodLookingModelName(concrete_model(results[r - 1]))))
            except ValueError as e:
                print(e)
Exemple #4
0
        return targets


## taken from http://stackoverflow.com/questions/4581789/how-do-i-get-user-ip-address-in-django
def get_client_ip(request):
    """
        Attempt to extract ip address from request object
        """
    x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
    if x_forwarded_for:
        ip = x_forwarded_for.split(',')[0]
    else:
        ip = request.META.get('REMOTE_ADDR')
    return ip

if logEnabled():
    from django.http import QueryDict
#    from django.utils.datastructures import MergeDict

    class RequestLog(models.Model):
        timestampSeconds = models.DateTimeField(verbose_name="Time", blank=False)
        path = models.CharField(max_length=256, blank=False)
        ipaddress = models.CharField(max_length=256, blank=False)
        #user = models.CharField(max_length=64, blank=False)  # probably can be a foreign key
        user = models.ForeignKey(User, null=True, blank=True)
        session = models.CharField(max_length=64, null=True, blank=True)
        referer = models.CharField(max_length=256, null=True, blank=True)
        user_agent = models.CharField(max_length=256, null=True, blank=True)

        def get_absolute_url(self):
            return reverse('xgds_data_replayRequest', args=[self.pk])