How to set Flask server timezone to GMT - flask

We have our flask server up and running. However requirement has come up to set server timzone as GMT instead of UTC. I could not find anything related to timezone in flask documentation. Is this possible? I know it is possible in django.

you can make use of the python datetime.now() instead of the datetime.utcnow(), or you can create a function that will automatically do the timezone conversions on all date objects for you on the sever.
I hope that will help you

This is another option in flask
from datetime import datetime
import pytz
UTC = pytz.utc
IST = pytz.timezone('Asia/Kolkata')
datetime_ist = datetime.now(IST)
print(datetime_ist.strftime('%Y-%m-%d %H:%M:%S'))

Related

Problem to run a check in my database in django

I have created a model called Certs, the models is registered and it is showing in the admin page, however I am trying to call it to run a check and try to make a task, but I am getting the error: No module named 'ships',
This is the structure of my project (note I am writing the code in reminders.py):
And here my code:
from django.core.mail import send_mail
from ships.models import Certs
from datetime import datetime, timedelta
b = Certs.objects.count()
def notifications():
for dates in range(b):
if Certs.exp_date[b] <= datetime.datetime.now() - timedelta(days=30):
send_mail(subject='Test',message='This is a reminder where you will be reminded about expiring certifications', recipient_list=['info#intermaritime.org'])
Thank you very much
Add the ships module to your INSTALLED section of settings.py

Get Timezone to take effect

I am going through the Django tutorial.
I thought the TIME_ZONE in settings.py was of form 'UTC-5', but it isn't.
I replaced it with 'America/Chicago'
However, when I do:
python manage.py shell
from django.utils import timezone
timezone.now()
I get 'UTC'
How do I get the timezone to take effect?
It can be confusing. If you run the code below, you will see that it is set, but it won't output as you expect:
from django.utils import timezone
timezone.get_current_timezone() # Should be 'America/Chicago'
timezone.now() # should show UTC
If you want it to output in the shell with your set timezone, use timezone.localtime()
from django.utils import timezone
timezone.localtime()

Date time and Date format localization with Django Rest Framework

I am creating API with DRF and Django 1.8 using python 2.7.x and Postgres 9.4. API is consumed by front end using AngularJS. I need to do time conversion to different time zones as per user set timezone. I tried creating a middleware as shown below -
import pytz
from django.utils import timezone
from django.conf import settings
class TimezoneMiddleware(object):
def process_request(self, request):
request.session["user_pref_timezone"] = "Europe/Paris"
tzname = request.session.get('user_pref_timezone')
settings.DATE_FORMAT = request.session.get('date')
settings.TIME_FORMAT = request.session.get('time')
if tzname:
timezone.activate(pytz.timezone(tzname))
else:
timezone.deactivate()
And added it to settings.py under MIDDLEWARE_CLASSES as
'core.middleware.timezone.TimezoneMiddleware',
in settings.py I have added ,
USE_TZ = True
import pytz
TIMEZONES = ( (tz, tz) for tz in pytz.common_timezones )
still I can not see my time converted from UTC to specific country timezone. Am I missing something?

Start django shell with a temporary database

I want to fire up the django shell with a temporary database (like what's done when doing django tests)
Is there any command like:
python manage.py testshell
where I can create a bunch of bogus models without polluting my database?
Nevermind, this blog post explains it
>>> from django import test
>>> test.utils.setup_test_environment() # Setup the environment
>>> from django.db import connection
>>> db = connection.creation.create_test_db() # Create the test db
You could just turn autocommit off:
from django.db import transaction
transaction.set_autocommit(False)

Not naive datetime (tzinfo is already set)

I use django celery in my django app and I'm experiencing this error:
ValueError: Not naive datetime (tzinfo is already set) in djcelery.utils in make_aware
How to fix this?
I think it's a bug from django-celery when they upgrade their version. But by the way, put this in your init.py where your settings.py located.
# Patch the djcelery/snapshot cause it's broken
import datetime
from django.utils.timezone import is_aware
import djcelery.snapshot
orig_maybe_make_aware = djcelery.snapshot.maybe_make_aware
def new_maybe_make_aware(value):
if isinstance(value, datetime.datetime) and is_aware(value):
return value
return orig_maybe_make_aware(value)
djcelery.snapshot.maybe_make_aware = new_maybe_make_aware