def test_low_zoom(self, test_tile: Dict[str, dict]): # clear cache cache.clear() cache_key: str = "low-zoom" async_generate_tile( year=test_tile["data"]["year"], month=test_tile["data"]["month"], day=test_tile["data"]["day"], style_xml_template=get_style_xml( generate_style_xml=False, carto_style_path=env("CARTO_STYLE_PATH")), zoom=0, x_pixel=test_tile["data"]["x_pixel"], y_pixel=test_tile["data"]["y_pixel"], osm_cato_path=env("CARTO_STYLE_PATH"), cache_key=cache_key, ) # get tile cache tile_cache: Optional[dict] = cache.get(cache_key) if tile_cache is None: raise AssertionError if tile_cache["process_id"] is not None: raise AssertionError if tile_cache["tile_hash"] is None: raise AssertionError if not isinstance(tile_cache["tile_hash"], str): raise AssertionError
def generate_tile_reload_style(request, year: int, month: int, day: int, zoom: int, x_pixel: float, y_pixel: float) -> HttpResponse: """ reload style.xml & than generate a new mapnik tile :param request: django request :param year: request year as INT :param month: request month as INT :param day: request day as INT :param zoom: mapnik zoom level :param x_pixel: mapnik x coordinate :param y_pixel: mapnik y coordinate :return: """ # generate time sensitive tile and reload style.xml tile_gen: TileGenerator = TileGenerator( request_date=date(year=int(year), month=int(month), day=int(day)), style_xml_template=get_style_xml( generate_style_xml=False, carto_sytle_path=env("CARTO_STYLE_PATH")), zoom=int(zoom), x_pixel=float(x_pixel), y_pixel=float(y_pixel), osm_cato_path=env("CARTO_STYLE_PATH"), ) return HttpResponse(tile_gen.render_tile(), content_type="image/jpeg")
class AccountFactory(DjangoModelFactory): class Meta: model = "trading_bot.Account" django_get_or_create = ["api_key"] exchange = Exchanges.BINANCE user = SubFactory(UserFactory) api_key = env("BINANCE_SANDBOX_API_KEY") secret = env("BINANCE_SANDBOX_SECRET_KEY")
def test_task_already_in_queue(self, test_tile: Dict[str, dict]): # clear cache cache.clear() tile_process: AsyncResult = async_generate_tile.delay( year=test_tile["data"]["year"], month=test_tile["data"]["month"], day=test_tile["data"]["day"], style_xml_template=get_style_xml( generate_style_xml=False, carto_style_path=env("CARTO_STYLE_PATH") ), zoom=test_tile["data"]["zoom"], x_pixel=test_tile["data"]["x_pixel"], y_pixel=test_tile["data"]["y_pixel"], osm_cato_path=env("CARTO_STYLE_PATH"), cache_key=test_tile["cache"]["cache_key"], ) # set cache with running process cache.set( test_tile["cache"]["cache_key"], {"process_id": tile_process.id, "tile_hash": None}, ) request: WSGIRequest = RequestFactory().get( self.get_path(kwargs=test_tile["data"]) ) response: HttpResponse = generate_tile( request=request, year=test_tile["data"]["year"], month=test_tile["data"]["month"], day=test_tile["data"]["day"], zoom=test_tile["data"]["zoom"], x_pixel=test_tile["data"]["x_pixel"], y_pixel=test_tile["data"]["y_pixel"], ) tile_cache: Optional[dict] = cache.get(test_tile["cache"]["cache_key"]) # check if the cache was setup right if tile_cache is None: raise AssertionError if hashlib.md5(response.content).hexdigest() != tile_process.get(): raise AssertionError if tile_cache["process_id"] is not None: raise AssertionError if not isinstance(response.content, bytes): raise AssertionError if response.status_code != 200: raise AssertionError if response["content-type"] != "image/jpeg": raise AssertionError
def verify_waiver_upload_token(token): """ Verify the JSON web token to allow a user to upload the waiver to their Appointment. """ try: application_id = jwt.decode(token, env('DJANGO_SECRET_KEY'), algorithms=['HS256'])['upload_waiver'] except: return return Appointment.objects.get(id=application_id)
def get_waiver_upload_token(self, expires_in=1209600): """ Create a JSON web token to send with the initial email to a client. This token will ollow the user to upload the wavier to their Appointment simply by clicking the link. """ return jwt.encode( {'upload_waiver': self.id, 'exp': time() + expires_in}, env('DJANGO_SECRET_KEY'), algorithm='HS256').decode('utf-8')
def set_indexes(osm_cato_path: str = env("CARTO_STYLE_PATH")): """ Set SQL indexes, to speedup rendering """ with open("{}/indexes.sql".format(osm_cato_path)) as index: index_sql: str = index.read() with connection.cursor() as cursor: cursor.execute(index_sql)
def generate_osm_tile(request, zoom: int, x_pixel: float, y_pixel: float) -> HttpResponse: """ get a default mapnik tile, without check the valid date :param request: :param zoom: :param x_pixel: :param y_pixel: :return: """ # generate normal osm tile tile_gen: TileGenerator = TileGenerator( request_date=date(year=2000, month=1, day=1), style_xml_template=get_style_xml( generate_style_xml=False, carto_sytle_path=env("CARTO_STYLE_PATH_DEBUG")), zoom=int(zoom), x_pixel=float(x_pixel), y_pixel=float(y_pixel), osm_cato_path=env("CARTO_STYLE_PATH_DEBUG"), ) return HttpResponse(tile_gen.render_tile(), content_type="image/jpeg")
"""Production settings""" from config.settings.base import PRODUCTION_APPS, PRODUCTION_MIDDLEWARE, env from config.settings.components.paths import ( DEV_DATABASE_FILE, TEST_DATABASE_FILE, ) ALLOWED_HOSTS = [ env("SITE_DOMAIN"), ] INSTALLED_APPS = [ *PRODUCTION_APPS, ] MIDDLEWARE = [ *PRODUCTION_MIDDLEWARE, ] if env("USE_SQLITE", default=False): DATABASES = { "default": { "ENGINE": "django.db.backends.sqlite3", "NAME": DEV_DATABASE_FILE, "TEST": { "NAME": TEST_DATABASE_FILE, }, }, } else:
from config.settings.base import env from .paths import KEYS_DIR REST_FRAMEWORK = { "DEFAULT_PERMISSION_CLASSES": ( "rest_framework.permissions.IsAuthenticated", ), "DEFAULT_AUTHENTICATION_CLASSES": ( "rest_framework_simplejwt.authentication.JWTAuthentication", ), } CORS_ORIGIN_ALLOW_ALL = True JWT_ALGORITHM = env("JWT_ALGORITHM", default="HS256") SIMPLE_JWT = { "ACCESS_TOKEN_LIFETIME": timedelta(minutes=5), "REFRESH_TOKEN_LIFETIME": timedelta(days=1), "ROTATE_REFRESH_TOKENS": True, "BLACKLIST_AFTER_ROTATION": True, "ALGORITHM": JWT_ALGORITHM, "AUTH_COOKIE": "auth_token", "AUTH_COOKIE_SECURE": False, "AUTH_REFRESH_COOKIE_PATH": reverse_lazy("api:v1:auth:token_refresh"), } if JWT_ALGORITHM in ["RS256", "RS384", "RS512"]: PUBLIC_KEY_NAME = env("JWT_PUBLIC_FILE", default="jwt.crt") PRIVATE_KEY_NAME = env("JWT_PRIVATE_FILE", default="jwt.key")
""" With these settings, tests run faster. """ from config.settings.base import * # noqa from config.settings.base import env # GENERAL # ------------------------------------------------------------------------------ # https://docs.djangoproject.com/en/dev/ref/settings/#debug DEBUG = False # https://docs.djangoproject.com/en/dev/ref/settings/#secret-key SECRET_KEY = env( "DJANGO_SECRET_KEY", default="dI4Mozs8dzXeiOr1unuwWbN2aqSf1gEVix7plcwtIq1AoxSEegsvIOjsCmNFrOC3", ) # https://docs.djangoproject.com/en/dev/ref/settings/#test-runner TEST_RUNNER = "django.test.runner.DiscoverRunner" # CACHES # ------------------------------------------------------------------------------ # https://docs.djangoproject.com/en/dev/ref/settings/#caches CACHES = { "default": { "BACKEND": "django.core.cache.backends.locmem.LocMemCache", "LOCATION": "", } } # PASSWORDS # ------------------------------------------------------------------------------
from config.settings.base import * # noqa: F403,F401 from config.settings.base import env # GENERAL # ------------------------------------------------------------------------------ # https://docs.djangoproject.com/en/dev/ref/settings/#debug DEBUG = False SECRET_KEY = env('DJANGO_SECRET_KEY', default='g*j^js4pj!$m2f-&eh2r7lv)&sbzskb=506ks0s&&x+9qo$(2x')
#!/usr/bin/env python import os import sys from config.settings.base import env if __name__ == "__main__": os.environ.setdefault("DJANGO_SETTINGS_MODULE", env("DJANGO_SETTINGS_MODULE")) try: from django.core.management import execute_from_command_line except ImportError as exc: try: import django # noqa except ImportError: raise ImportError( "Couldn't import Django. Are you sure it's installed and " "available on your PYTHONPATH environment variable? Did you " "forget to activate a virtual environment?") raise # This allows easy placement of apps within the interior # covue directory. current_path = os.path.dirname(os.path.abspath(__file__)) sys.path.append(os.path.join(current_path, "covue")) execute_from_command_line(sys.argv)
from config.settings.base import * # noqa from config.settings.base import env # GENERAL # ------------------------------------------------------------------------------ # https://docs.djangoproject.com/en/dev/ref/settings/#debug DEBUG = True # https://docs.djangoproject.com/en/dev/ref/settings/#secret-key SECRET_KEY = env( "DJANGO_SECRET_KEY", default="AcU63XPBP0WLMlX4yMcO6FmHO1hrpEYF8nsMcjM9vm4d5w2nyGh1HLV1q6MXALJ9", ) # https://docs.djangoproject.com/en/dev/ref/settings/#allowed-hosts ALLOWED_HOSTS = [ "localhost", "0.0.0.0", "127.0.0.1", ".compute-1.amazonaws.com", ] # CACHES # ------------------------------------------------------------------------------ # https://docs.djangoproject.com/en/dev/ref/settings/#caches CACHES = { "default": { "BACKEND": "django.core.cache.backends.locmem.LocMemCache", "LOCATION": "", } } # EMAIL
from config.settings.base import * # noqa: F403,F401 from config.settings.base import env # GENERAL # ------------------------------------------------------------------------------ # https://docs.djangoproject.com/en/dev/ref/settings/#debug DEBUG = True SECRET_KEY = env('DJANGO_SECRET_KEY', default='gedqrgbjf6xf^bl^7%riw)euc3_13!2*f6=d=i(kw=zcdbfbxi') ALLOWED_HOSTS = ["localhost", "0.0.0.0", "127.0.0.1", "3.19.113.236"] # https://django-extensions.readthedocs.io/en/latest/installation_instructions.html#configuration INSTALLED_APPS += ['django_extensions', 'autofixture'] # noqa F405
from config.settings.base import * from config.settings.base import env DEBUG = False SECRET_KEY = env('SECRET_KEY', default='c#-!x^di-(n7@h@_2p@j(%^ce#8-@m=ager%x_zfqq%034qfdb') ALLOWED_HOSTS = env.list('DJANGO_ALLOWED_HOSTS', default=['localhost', '127.0.0.1', '.niangular.com']) # Database #DATABASES = {} #DATABASES['default'] = env.db('DATABASE_URL') DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', 'NAME': env('DB_NAME'), 'USER': env('DB_USER'), 'PASSWORD': env('DB_PASSWORD'), 'HOST': '127.0.0.1', 'PORT': '5432', 'ATOMIC_REQUESTS': True, 'CONN_MAX_AGE': env.int('CONN_MAX_AGE', default=60) } }
from config.settings.base import * # noqa from config.settings.base import env # GENERAL SECRET_KEY = env('SECRET_KEY') ALLOWED_HOSTS = env.list('DJANGO_ALLOWED_HOSTS', default=['.travel2change.org']) INSTALLED_APPS += ('gunicorn', ) # noqa F405 DEBUG = False # DATABASES DATABASES['default'] = env.db('DATABASE_URL') # noqa F405 DATABASES['default']['ATOMIC_REQUESTS'] = True # noqa F405 # CACHES CACHES = { 'default': { 'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache', 'LOCATION': 'unix:~/memcached.sock', } } # SECURITY # ================================================================================== # https://docs.djangoproject.com/en/dev/ref/settings/#secure-proxy-ssl-header SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https') # https://docs.djangoproject.com/en/dev/ref/settings/#secure-ssl-redirect SECURE_SSL_REDIRECT = env.bool('SECURE_SSL_REDIRECT', default=True) # https://docs.djangoproject.com/en/dev/ref/settings/#session-cookie-secure
def prerender(zoom_level: int, test_mode: bool = False): # get the lowest valid_since for each geometry point_valid_since: Optional[date] = PlanetOsmPoint.objects.all().order_by( "valid_since" )[0].valid_since line_valid_since: Optional[date] = PlanetOsmLine.objects.all().order_by( "valid_since" )[0].valid_since polygon_valid_since: Optional[date] = PlanetOsmPolygon.objects.all().order_by( "valid_since" )[0].valid_since # get the highest valid_until for each geometry point_valid_until: Optional[date] = PlanetOsmPoint.objects.all().order_by( "-valid_until" )[0].valid_until line_valid_until: Optional[date] = PlanetOsmLine.objects.all().order_by( "-valid_until" )[0].valid_until polygon_valid_until: Optional[date] = PlanetOsmPolygon.objects.all().order_by( "-valid_until" )[0].valid_until # get the lowest valid_since for all geometries valid_since: date if point_valid_since: valid_since = point_valid_since if line_valid_since: if not valid_since: valid_since = line_valid_since if line_valid_since < valid_since: valid_since = line_valid_since if polygon_valid_since: if not valid_since: valid_since = polygon_valid_since if polygon_valid_since < valid_since: valid_since = polygon_valid_since # get the highest valid_until for all geometries valid_until: date if point_valid_until: valid_until = point_valid_until if line_valid_until: if not valid_until: valid_until = line_valid_until if line_valid_until < valid_until: valid_until = line_valid_until if polygon_valid_until: if not valid_until: valid_until = polygon_valid_until if polygon_valid_until < valid_until: valid_until = polygon_valid_until print("Start prerender") delta = timedelta(days=1) while valid_since <= valid_until: for zoom in range(0, zoom_level + 1): for x in range(0, zoom * zoom + 1): for y in range(0, zoom * zoom + 1): tile_cache_key: str = "{}-{}-{}-{}-{}-{}".format( valid_since.year, valid_since.month, valid_since.day, zoom, x, y, ) print("rendering: {}".format(tile_cache_key)) if not test_mode: async_generate_tile( year=valid_since.year, month=valid_since.month, day=valid_since.day, style_xml_template=OSM_CARTO_STYLE_XML, zoom=zoom, x_pixel=x, y_pixel=y, osm_cato_path=env("CARTO_STYLE_PATH"), cache_key=tile_cache_key, ) valid_since += delta print("prerending is done!")
from config.settings.base import * # noqa from config.settings.base import env # GENERAL # ------------------------------------------------------------------------------ # https://docs.djangoproject.com/en/dev/ref/settings/#secret-key SECRET_KEY = env("DJANGO_SECRET_KEY") # https://docs.djangoproject.com/en/dev/ref/settings/#allowed-hosts ALLOWED_HOSTS = env.list("DJANGO_ALLOWED_HOSTS", default=["example.com"]) # DATABASES # ------------------------------------------------------------------------------ DATABASES["default"] = env.db("DATABASE_URL") # noqa F405 DATABASES["default"]["ATOMIC_REQUESTS"] = True # noqa F405 DATABASES["default"]["CONN_MAX_AGE"] = env.int("CONN_MAX_AGE", default=60) # noqa F405 # CACHES # ------------------------------------------------------------------------------ CACHES = { "default": { "BACKEND": "django_redis.cache.RedisCache", "LOCATION": env("REDIS_URL"), "OPTIONS": { "CLIENT_CLASS": "django_redis.client.DefaultClient", # Mimicing memcache behavior. # http://niwinz.github.io/django-redis/latest/#_memcached_exceptions_behavior "IGNORE_EXCEPTIONS": True, }, } }
def __init__( self, zoom: int, x_pixel: float, y_pixel: float, request_date: Optional[date] = None, style_xml_template: str = OSM_CARTO_STYLE_XML, osm_cato_path: str = env("CARTO_STYLE_PATH"), width: int = 256, height: int = 256, use_cache: bool = False, ): """Tile generator class from ohdm_django_mapnik.ohdm.tile import TileGenerator tile_generator: TileGenerator = TileGenerator( zoom=0, x_pixel=0, y_pixel=0, request_date=datetime(2020, 1, 1), use_cache=False ) tile_png: bytes = tile_generator.render_tile() Arguments: zoom {int} -- zoom level between 0 to 20 x_pixel {float} -- x coordinate between 0 and 2^zoom y_pixel {float} -- y coordinate between 0 and 2^zoom Keyword Arguments: request_date {Optional[date]} -- request date (default: {None}) style_xml_template {str} -- path of default style.xml (default: {OSM_CARTO_STYLE_XML}) osm_cato_path {str} -- path of openstreetmap-carto (default: {env("CARTO_STYLE_PATH")}) width {int} -- tile png width (default: {256}) height {int} -- tile png height (default: {256}) use_cache {bool} -- cache style.xml (default: {False}) Raises: ZoomOutOfRange: raise when zoom level is out of range CoordinateOutOfRange: raise when x & y coordinate is out of range """ # check if zoom level is valid if zoom < 0 or zoom > 19: raise ZoomOutOfRange( "Zoom level has to be between 0 and 19! zoom = {}".format( zoom)) # check if x_pixel and y_pixel is valid max_pixel: float = pow(2, zoom) if x_pixel < 0.0 or x_pixel > max_pixel or y_pixel < 0.0 or y_pixel > max_pixel: raise CoordinateOutOfRange(""" X or Y coordinate is out of range! Coordinate has to be between 0 and 2^zoom. zoom = {} x = {} y = {} """.format(zoom, x_pixel, y_pixel)) self.request_date: Optional[date] = request_date self.style_xml_template: str = style_xml_template self.zoom: int = zoom self.x_pixel: float = x_pixel self.y_pixel: float = y_pixel self.width: int = width self.height: int = height self.osm_cato_path: str = osm_cato_path self.use_cache = use_cache
# flake8: noqa from config.settings.base import env from .common import * if DEBUG: SECRET_KEY = env( "SECRET_KEY", default="-qf)o7hs$jk@b8o)zidroo9wskuf^95m2$@k)5^@hl-=)349-7", ) from .development import * else: SECRET_KEY = env("SECRET_KEY") from .production import *
from config.settings.base import DEVELOPER_APPS, DEVELOPER_MIDDLEWARE, env from config.settings.components.paths import ( DEV_DATABASE_FILE, TEST_DATABASE_FILE, ) INTERNAL_IPS = [ "127.0.0.1", ] ALLOWED_HOSTS = ["*"] INSTALLED_APPS = [*DEVELOPER_APPS] MIDDLEWARE = [*DEVELOPER_MIDDLEWARE] if env("USE_SQLITE", default=True): DATABASES = { "default": { "ENGINE": "django.db.backends.sqlite3", "NAME": DEV_DATABASE_FILE, "TEST": { "NAME": TEST_DATABASE_FILE, }, }, } else: DATABASES = { "default": env.db(), }
from oauthlib.oauth2 import BackendApplicationClient from requests_oauthlib import OAuth2Session from requests.auth import HTTPBasicAuth from config.settings.base import env import datetime as dt import os client_id = env('SPOTIFY_CLIENT_ID') client_secret = env('SPOTIFY_CLIENT_SECRET') redirect_uri = env('SPOTIFY_REDIRECT_URI') class SpotifyClientCredentials(object): TOKEN_URL = 'https://accounts.spotify.com/api/token' def __init__(self, client_id=client_id, client_secret=client_secret, token={}): self.client_id = client_id self.client_secret = client_secret self.session = self.create_oauth_session(token=token) def get_new_token(self): auth = self.create_auth() client = self.create_client() self.token = self.session.fetch_token(token_url=self.TOKEN_URL, auth=auth, client=client) return self.token
def generate_tile(request, year: int, month: int, day: int, zoom: int, x_pixel: float, y_pixel: float) -> HttpResponse: """ get a mapnik tile, get it from cache if exist else it will be generated as a celery task :param request: django request :param year: request year as INT :param month: request month as INT :param day: request day as INT :param zoom: mapnik zoom level :param x_pixel: mapnik x coordinate :param y_pixel: mapnik y coordinate :return: """ # set tile cache key, where the celery task id & tile cache id is stored tile_cache_key: str = "{}-{}-{}-{}-{}-{}".format( int(year), int(month), int(day), int(zoom), int(x_pixel), int(y_pixel), ) # tile static typing tile: Optional[bytes] tile_process: AsyncResult # get tile cache tile_cache: Optional[dict] = cache.get(tile_cache_key, { "process_id": None, "tile_hash": None }) # check if process is running and wait for end if tile_cache: if tile_cache["process_id"]: tile_process = AsyncResult(tile_cache["process_id"]) for _ in range(0, env.int("TILE_GENERATOR_HARD_TIMEOUT", 360) * 2): sleep(0.5) tile_cache = cache.get(tile_cache_key, { "process_id": None, "tile_hash": None }) if tile_cache: if tile_cache["tile_hash"]: break # try get tile png & return it if tile_cache: if tile_cache["tile_hash"]: tile = cache.get(tile_cache["tile_hash"]) if tile: return HttpResponse(tile, content_type="image/jpeg") # if there is no tile process & no tile in cache, create one tile_process = async_generate_tile.delay( year=int(year), month=int(month), day=int(day), style_xml_template=OSM_CARTO_STYLE_XML, zoom=int(zoom), x_pixel=float(x_pixel), y_pixel=float(y_pixel), osm_cato_path=env("CARTO_STYLE_PATH"), cache_key=tile_cache_key, ) if not tile_cache: tile_cache = {"process_id": None, "tile_hash": None} tile_cache["process_id"] = tile_process.id # update cache if zoom <= env.int("ZOOM_LEVEL", 13): cache.set(tile_cache_key, tile_cache, None) else: cache.set(tile_cache_key, tile_cache, env.int("TILE_CACHE_TIME", 2592000)) try: tile_process.wait(timeout=env.int("TILE_GENERATOR_HARD_TIMEOUT", 360)) except exceptions.TimeoutError: return HttpResponse("Timeout when creating tile", status=500) except CoordinateOutOfRange as e: return HttpResponse(e, status=405) tile_cache["tile_hash"] = tile_process.get() tile = cache.get(tile_cache["tile_hash"]) if tile: return HttpResponse(tile, content_type="image/jpeg") return HttpResponse("Caching Error", status=500)
from config.settings.base import env from .models import Security from config import celery_app from django.utils.timezone import now from django.core.mail import send_mail DEFAULT_FROM_EMAIL = env("DJANGO_SERVER_EMAIL", default="bonds <*****@*****.**>") @celery_app.task(bind=True, name='moex.refresh_security_from_rshb', default_retry_delay=30 * 60, max_retries=6) def refresh_security_from_rshb(self): today = now().date() securities = Security.objects.\ filter(last_update__lt=today).\ filter(security_type='pif_rshb') result = dict() for security in securities: result[security.name] = security.refresh_price() if securities.count(): if securities.count() > len( [i for i in result if 'ok' == result[i][0]]): raise self.retry() return result @celery_app.task(bind=True,
from config.settings.base import * from config.settings.base import env # Production specific settings # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = env("SECRET_KEY") # SECURITY WARNING: don't run with debug turned on in production! DEBUG = False ALLOWED_HOSTS = env.str("DJANGO_ALLOWED_HOSTS").split(" ") # ADMINS = [ # ( # env.str("WEBMASTER_NAME", default="Webmaster"), # env.str("WEBMASTER_EMAIL", default="*****@*****.**") # ), # ( # env.str("ADMINISTRATOR_NAME", default="Administrator"), # env.str("ADMINISTRATOR_EMAIL", default="*****@*****.**") # ) # ] # MANAGERS = ADMINS # Add INSTALLED_APPS on top INSTALLED_APPS = [] + INSTALLED_APPS # Add INSTALLED_APPS at bottom
""" With these settings, tests run faster. """ from config.settings.base import * # noqa from config.settings.base import env # noqa # GENERAL # ------------------------------------------------------------------------------ # https://docs.djangoproject.com/en/dev/ref/settings/#secret-key SECRET_KEY = env( "DJANGO_SECRET_KEY", default="Jys1TxGh1BHi1llwoXGIoJ5wleJBoWSFb6dBgJdSk8zBtVO2edMla7WBdx7ewsF0", ) # https://docs.djangoproject.com/en/dev/ref/settings/#test-runner TEST_RUNNER = "django.test.runner.DiscoverRunner" # CACHES # ------------------------------------------------------------------------------ # https://docs.djangoproject.com/en/dev/ref/settings/#caches CACHES = { "default": { "BACKEND": "django.core.cache.backends.locmem.LocMemCache", "LOCATION": "", } } # PASSWORDS # ------------------------------------------------------------------------------ # https://docs.djangoproject.com/en/dev/ref/settings/#password-hashers PASSWORD_HASHERS = ["django.contrib.auth.hashers.MD5PasswordHasher"]
def generate_tile(request, year: int, month: int, day: int, zoom: int, x_pixel: float, y_pixel: float) -> HttpResponse: """ get a mapnik tile, get it from cache if exist else it will be generated as a celery task :param request: django request :param year: request year as INT :param month: request month as INT :param day: request day as INT :param zoom: mapnik zoom level :param x_pixel: mapnik x coordinate :param y_pixel: mapnik y coordinate :return: """ request_date: date = date(year=int(year), month=int(month), day=int(day)) tile_cache: Optional[TileCache] = TileCache.objects.filter( zoom=zoom, x_pixel=x_pixel, y_pixel=y_pixel, valid_since__lte=request_date, valid_until__gte=request_date, ).last() if tile_cache: tile: Optional[bytes] = tile_cache.get_tile_from_cache_or_delete() if tile_cache and tile: return HttpResponse(tile, content_type="image/jpeg") else: tile_cache = TileCache.objects.create( zoom=zoom, x_pixel=x_pixel, y_pixel=y_pixel, valid_since=request_date, valid_until=request_date, ) tile_process: AsyncResult = async_generate_tile.delay( year=int(year), month=int(month), day=int(day), style_xml_template=OSM_CARTO_STYLE_XML, zoom=int(zoom), x_pixel=float(x_pixel), y_pixel=float(y_pixel), osm_cato_path=env("CARTO_STYLE_PATH"), cache_key=tile_cache.get_cache_key(), ) tile_cache.celery_task_id = tile_process.id tile_cache.save() tile_cache.set_valid_date() while tile_process.ready() is False: pass tile_cache.celery_task_done = True tile_cache.save() return HttpResponse(cache.get(tile_process.get()), content_type="image/jpeg")
from config.settings.base import * # noqa from config.settings.base import env # GENERAL # ------------------------------------------------------------------------------ # https://docs.djangoproject.com/en/dev/ref/settings/#debug DEBUG = False # https://docs.djangoproject.com/en/dev/ref/settings/#secret-key SECRET_KEY = env('SECRET_KEY', default='275r(#c+_uh4k&nbdmt*)mq)_v^689lkq&mypgpk*8e11wboo6') # https://docs.djangoproject.com/en/dev/ref/settings/#test-runner TEST_RUNNER = 'django.test.runner.DiscoverRunner' # CACHES # ------------------------------------------------------------------------------ # https://docs.djangoproject.com/en/dev/ref/settings/#caches CACHES = { 'default': { 'BACKEND': 'django.core.cache.backends.locmem.LocMemCache', 'LOCATION': '' } } # PASSWORDS # ------------------------------------------------------------------------------ # https://docs.djangoproject.com/en/dev/ref/settings/#password-hashers PASSWORD_HASHERS = [ 'django.contrib.auth.hashers.MD5PasswordHasher', ] # TEMPLATES
def handle(self, *args, **options): create_style_xml(carto_style_path=env("CARTO_STYLE_PATH"))