Why timezone inside unittest in Django is different from timezone in view? - django

I'm using TestCase.client to test my views. And when I call timezone.now() from test case, i get 2015-11-17 07:48:26.826661+00:00, but when I call
start = timezone.make_aware(datetime.strptime(
date_text + ' ' + time,
'%y/%m/%d %H:%M'
))
from view I get 2015-11-17 07:36:00+02:00.
How to make them use the same timezone?
I'm running using ./manage.py test --settings=www.tests_settings and www/tests_settings.py contains following:
from .settings import *
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
}
}
USE_TZ = True
TIME_ZONE = 'Europe/Kiev'
>>> django.__version__
'1.8.4'
>>> pytz.__version__
'2015.7'
>>> sys.version
'3.4.3 (default, Mar 26 2015, 22:03:40) \n[GCC 4.9.2]'

Oh, I see. make_aware returns time in default time zone, and now - in UTC. And to get local current time, I need to do timezone.localtime(timezone.now()).
But documentation recommends to deal with time and timezones like with text and encodings - store everything in UTC (unicode) and convert only when receiving or giving it to user.

Related

Django, using oracledb error msg that I need Oracle 19

I am confused since the documentation for oracledb clearly states that everything past 12.1 should work fine. Could someone please explain to me where I went wrong? The error was created when I tried to create migrations.
The document I am referencing is: oracledb docs
Here is the error:
django.db.utils.NotSupportedError: Oracle 19 or later is required (found 12.2.0.1.0).
And here is my databases string in my settings.py:
from pathlib import Path
import sys
import oracledb
oracledb.version = "8.3.0"
sys.modules["cx_Oracle"] = oracledb
#the above line was added because of error (django.core.exceptions.ImproperlyConfigured: Error
#loading cx_Oracle module: No module named 'cx_Oracle')
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.oracle',
'NAME': (
'(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=server123)(PORT=1521))'
'(CONNECT_DATA=(SERVICE_NAME=server.domain.com)))'
),
'USER': 'user123',
'PASSWORD': 'password',
'OPTIONS': {
'threaded': True,
},
}
}
It's a Django thing. From docs.djangoproject.com/en/4.1/ref/databases/#oracle-notes:
"Django supports Oracle Database Server versions 19c and higher."
Also see the Django 4.0 release notes
"Dropped support for Oracle 12.2 and 18c".
Try an older version of Django if you can't upgrade the DB

Google analytics and django datetime

I am trying to club together the data from google analytics and django application.
The datetime of django in database are in UTC. Google analytics data has timezone as IST.
I am getting data from google analytics for a period based on IST and getting data from django for same period in IST. There are lot of variations .
Is there any way this can be solved ?
IST (Indian Standard Time) is 5:30 ahead of UTC (http://www.timeanddate.com/time/zones/ist). When getting data from Django, make sure that you use localized datetime objects:
import pytz
from datetime import datetime
tz_india = pytz.timezone('Asia/Kolkata')
start = tz_india.localize(datetime(#Here create you datetime object from IST))
end = tz_india.localize(datetime(#Here create you datetime object from IST))
Your best option here could be to change the default timezone from your Django App to IST.
First of all you should check Django settings: TIME_ZONE
You will understand better how to fix it if you understand how it works.
To change the default timezone to IST you need to add in your settings.py:
TIME_ZONE = 'Asia/Kolkata'
If Asia/Kolkata doesn't fit your needs, you can check all the available time zones in the next URL:
List of Time Zones
If you prefer to manage the datetime objects and convert from UTC to IST or viceversa you can do it like this:
Convert data from UTC to IST
from datetime import datetime
from pytz import timezone
format = "%Y-%m-%d %H:%M:%S %Z%z"
# Current time in UTC
now_utc = datetime.now(timezone('UTC'))
print now_utc.strftime(format)
Output: 2015-05-18 10:02:47 UTC+0000
# Convert to Asia/Kolkata time zone
now_asia = now_utc.astimezone(timezone('Asia/Kolkata'))
print now_asia.strftime(format)
Output: 2015-05-18 15:32:47 IST+0530
Convert data from IST to UTC
from datetime import datetime
from pytz import timezone
format = "%Y-%m-%d %H:%M:%S %Z%z"
# Current time in Asia/Kolkata
now_asia = datetime.now(timezone('Asia/Kolkata'))
print now_asia.strftime(format)
Output: 2015-05-18 15:32:47 IST+0530
# Convert to UTC time zone
now_utc = now_utc.astimezone(timezone('UTC'))
print now_utc.strftime(format)
Output: 2015-05-18 10:02:47 UTC+0000

Memcached Compare-And-Set pattern yields wrong result

I'm trying to implement memcached compare-and-set pattern, following the instructions of Guido at:
http://neopythonic.blogspot.nl/2011/08/compare-and-set-in-memcache.html
However, I don't seem to get it right and I have no idea what's wrong. The files below use Django (1.4.5 Final) and python-memcache (1.48).
settings.py
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
'LOCATION': '127.0.0.1:11211',
}
}
djangocache.py
#!/usr/bin/env python
from django.core.cache import cache
import multiprocessing.dummy
django_key = "TEST"
cached_key = cache.make_key(django_key).encode("UTF-8")
def add_to_cache(item):
client = cache._cache
#client = cache._lib.Client(cache._servers)
while True:
items = client.gets(cached_key)
if client.cas(cached_key, items+(item,)):
break
if __name__ == "__main__":
cache.set(django_key, ())
p = multiprocessing.dummy.Pool(2)
p.map(add_to_cache, range(10))
print(len(cache.get(django_key)))
Running it:
mzialla#Q330 ~/test $ DJANGO_SETTINGS_MODULE=settings python djangocache.py
5
It occasionally outputs 6, 7, etc. like you would expect when dealing with race conditions. I've tried multiple client instantiations (see comment).
Help?
python-memcached disables cas by default. Enable it by adding
client.cache_cas = True
to your code.
Credits to Nate Thelen, who's comment I discovered right after asking this question.

Importing CSV to Django and settings not recognised

So i'm getting to grips with Django, or trying to. I have some code that isn't dependent on being called by the webpage - it's designed to populate the database with information. Eventually it will be set up as a cron job to run overnight. This is the first crack at it, which is to do an initial population (once I have that working, I'll move to an add structure, where only new records are pushed.) I'm using Python 2.7, Django 1.5 and Sqlite3. When I run this code, I get
Requested setting DATABASES, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
That seems fairly obvious, but I've spent a couple of hours now trying to work out how to adjust that setting. How do I call / open a connection / whatever the right terminology is here? I have a number of functions like this that will be scheduled jobs, and this has been frustrating me all afternoon.
import urllib2
import csv
import requests
from django.db import models
from gmbl.models import Match
master_data_file = urllib2.urlopen("http://www.football-data.co.uk/mmz4281/1213/E0.csv", "GET")
data = list(tuple(rec) for rec in csv.reader(master_data_file, delimiter=','))
for row in data:
current_match = Match(matchdate=row[1],
hometeam=row[2],
awayteam = row [3],
homegoals = row [4],
awaygoals = row[5],
homeshots = row[10],
awayshots = row[11],
homeshotsontarget = row[12],
awayshotsontarget = row[13],
homecorners = row[16],
awaycorners = row[17])
current_match.save()
I had originally started out with http://django-csv-importer.readthedocs.org/en/latest/ but I had the same error, and the documentation doesn't make much sense trying to debug it. When I tried calling settings.configure in the function, it said it didn't exist; presumably I had to import it, but couldn't make that work.
Make sure Django, and your project are in PYTHONPATH then you can do:
import urllib2
import csv
import requests
from django.core.management import setup_environ
from django.db import models
from yoursite import settings
setup_environ(settings)
from gmbl.models import Match
master_data_file = urllib2.urlopen("http://www.football-data.co.uk/mmz4281/1213/E0.csv", "GET")
data = list(tuple(rec) for rec in csv.reader(master_data_file, delimiter=','))
# ... your code ...
Reference: http://www.b-list.org/weblog/2007/sep/22/standalone-django-scripts/
Hope it helps!

date_format Django 1.4 with correct timezone

Okay simple question (I think).
I have a DateTime field (auto_add_now) and when output to a template
{{ edited|date:"DATETIME_FORMAT" }}
I get the expected result of "Sept. 16, 2012, 12:01 p.m."
But unfortunately things are slightly more complicated since I am using Backbone.js and need to pass the datetime with JSON, and since it is only used for display purposes I decided to pass it as a nice locale formatted string. So I dug into the code and found what the template tag uses and this is what I setup.
from django.utils.formats import date_format
return {
'created': date_format(self.created, 'DATETIME_FORMAT'),
}
But that ends up with this "Sept. 16, 2012, 5:01 p.m."
I have a feeling it has to do with the following on the template tag
#register.filter(expects_localtime=True, is_safe=False)
I also tried this but ended up with the same results
from django.utils import timezone
tz = timezone.get_current_timezone()
logger.info(tz)
logger.info(self.edited)
logger.info(format(self.edited, 'DATETIME_FORMAT'))
logger.info(self.edited.replace(tzinfo=tz))
logger.info(format(self.edited.replace(tzinfo=tz), 'DATETIME_FORMAT'))
Which gave me this
INFO: America/Chicago
INFO: 2012-09-16 17:01:52.921276+00:00
INFO: Sept. 16, 2012, 5:01 p.m.
INFO: 2012-09-16 17:01:52.921276-06:00
INFO: Sept. 16, 2012, 5:01 p.m.
So yeah, I must be missing something, and I have been up and down the django documentation and cannot find anything that could point me to what I am doing wrong. Thanks for any help.
I figured it out. And sadly it was in the Django Timezones documentation that I thought I had exhausted. Localize Usage timezone.localtime()
from django.utils.formats import date_format
from django.utils import timezone
date_format(timezone.localtime(page.created), 'DATETIME_FORMAT')
Maybe the following will help you.
>>> obj = MyModel.objects.get(...)
>>> data = {"date_format": obj.edited}
>>> from django.core.serializers.json import DjangoJSONEncoder
>>> data = json.dumps(data, cls=DjangoJSONEncoder)
>>> data
'{"date_format": "2012-09-16T21:45:46Z"}'
Send the json formatted data from your view:
E.g return HttpResponse(data, mimetype='application/json').
And then at your client side code you can convert the date_format to the local timezone with:
(Assuming response is the JSON parsed object)
var d = new Date(Date.parse(response.date_format));
// Sun Sep 16 2012 22:45:46 GMT+0100 (BST)