0

Hi I have below setup.

Django 1.9, mongoDB, pymongo 2.7, mongoengine 0.9

I have written an API to store logs at backend server userwise. Below is sample of table

user  subject  registered  changed_on
abc    eng      Y          "2018-04-18T00:00:00Z"
abc    maths    N          "2018-04-18T00:10:00Z"
xyz    eng      Y          "2018-04-18T00:10:00Z"

I also have read API for this in which we give user name and timestamp for filter like below:

{
"user" : "abc",
"from_date" : "2018-04-18T00:00:00Z"
}

The line in serializers.py which is applying filter is

Logs.objects.filter(user__iexact='abc',changed_on__gte=from_date)

Now Sometimes when I add new log and retrieve it from postman, it is not working. I have to restart django server and then it gives me newly added row.

I dont understand why this is happening.

EDIT1 : Full Serializer.py

from rest_framework import serializers

class GetUserLogs(serializers.Serializer):
    user = serializers.CharField(label=_("USER"))
    token = serializers.CharField(label=_("Token"))
    from_date = serializers.CharField(label=_('From date'), default="")
    till_date = serializers.CharField(label=_('Till date'), default=datetime.datetime.now().isoformat().split(".")[0]+'Z')
    def validate(self, attrs):
        user = attrs.get('user')
        token = attrs.get('token')
        from_date = attrs.get('from_date')          
        if user:
            tokendetails = validate_token(token)
            if not tokendetails:
                msg = _('Invalid token.')
                raise serializers.ValidationError(msg)
            else:
                userdetails = tokendetails.user
                if userdetails.check_user(user):
                    rows = Logs.objects.all().filter(user__iexact=user,changed_on__gte=from_date, changed_on__lte = till_date)
                    print(len(rows))
                else:
                    msg = _('Invalid USER)
                    raise serializers.ValidationError(msg)
        else:
            msg = _('Must include "USER".')
            raise serializers.ValidationError(msg)

        attrs['rows'] = rows
        return attrs
Pratibha
  • 1,730
  • 7
  • 27
  • 46

1 Answers1

0

So After a lot of debugging for almost a day, I found that the default value of till_date that we are setting to current timestamp of system is not increasing as time increase. So I changed default value to empty string and added below code after reading value of till_date.

if till_date == "":
    till_date = datetime.datetime.now().isoformat().split(".")[0]+'Z'

It will reset the till_date value to current timestamp every time its value is missing.

Hope it will help someone someday.

Pratibha
  • 1,730
  • 7
  • 27
  • 46