From fb32c8b86ae768ae06708572f50077ba78307e94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adolfo=20G=C3=B3mez=20Garc=C3=ADa?= Date: Mon, 12 Aug 2019 15:01:24 +0200 Subject: [PATCH] refactoring for name improvements - cache, calendar, calendar_access, calendar_rule models --- server/src/uds/REST/methods/calendarrules.py | 2 +- server/src/uds/REST/methods/op_calendars.py | 2 +- server/src/uds/REST/methods/services_pools.py | 2 +- server/src/uds/core/util/Cache.py | 26 +++++++++---------- server/src/uds/core/util/calendar/__init__.py | 2 +- server/src/uds/models/MetaPool.py | 2 +- server/src/uds/models/ServicesPool.py | 2 +- server/src/uds/models/__init__.py | 10 +++---- server/src/uds/models/{Cache.py => cache.py} | 0 .../uds/models/{Calendar.py => calendar.py} | 4 +-- .../{CalendarAccess.py => calendar_access.py} | 10 ++++--- .../{CalendarAction.py => calendar_action.py} | 14 +++++----- .../{CalendarRule.py => calendar_rule.py} | 2 +- 13 files changed, 40 insertions(+), 38 deletions(-) rename server/src/uds/models/{Cache.py => cache.py} (100%) rename server/src/uds/models/{Calendar.py => calendar.py} (95%) rename server/src/uds/models/{CalendarAccess.py => calendar_access.py} (94%) rename server/src/uds/models/{CalendarAction.py => calendar_action.py} (97%) rename server/src/uds/models/{CalendarRule.py => calendar_rule.py} (99%) diff --git a/server/src/uds/REST/methods/calendarrules.py b/server/src/uds/REST/methods/calendarrules.py index 61d06e15..6e1b9c19 100644 --- a/server/src/uds/REST/methods/calendarrules.py +++ b/server/src/uds/REST/methods/calendarrules.py @@ -37,7 +37,7 @@ from django.utils.translation import ugettext as _ from django.db import IntegrityError -from uds.models.CalendarRule import freqs, CalendarRule +from uds.models.calendar_rule import freqs, CalendarRule from uds.models.Util import getSqlDatetime from uds.core.util import permissions diff --git a/server/src/uds/REST/methods/op_calendars.py b/server/src/uds/REST/methods/op_calendars.py index 40bc4f4b..2efd6eb7 100644 --- a/server/src/uds/REST/methods/op_calendars.py +++ b/server/src/uds/REST/methods/op_calendars.py @@ -36,7 +36,7 @@ import logging from django.utils.translation import ugettext as _ from uds.models import CalendarAction, Calendar -from uds.models.CalendarAction import CALENDAR_ACTION_DICT +from uds.models.calendar_action import CALENDAR_ACTION_DICT from uds.core.util import log, permissions from uds.core.util.model import processUuid diff --git a/server/src/uds/REST/methods/services_pools.py b/server/src/uds/REST/methods/services_pools.py index 0e69b0da..de25bba1 100644 --- a/server/src/uds/REST/methods/services_pools.py +++ b/server/src/uds/REST/methods/services_pools.py @@ -34,7 +34,7 @@ import logging from django.utils.translation import ugettext, ugettext_lazy as _ from uds.models import DeployedService, OSManager, Service, Image, ServicesPoolGroup, Account -from uds.models.CalendarAction import ( +from uds.models.calendar_action import ( CALENDAR_ACTION_INITIAL, CALENDAR_ACTION_MAX, CALENDAR_ACTION_CACHE_L1, diff --git a/server/src/uds/core/util/Cache.py b/server/src/uds/core/util/Cache.py index 41a97a67..c74042c4 100644 --- a/server/src/uds/core/util/Cache.py +++ b/server/src/uds/core/util/Cache.py @@ -37,7 +37,7 @@ from datetime import datetime, timedelta import typing from django.db import transaction -import uds.models.Cache +import uds.models.cache from uds.models.Util import getSqlDatetime from uds.core.util import encoders @@ -71,7 +71,7 @@ class Cache: try: key = self.__getKey(skey) logger.debug('Key: %s', key) - c: uds.models.Cache = uds.models.Cache.objects.get(pk=key) # @UndefinedVariable + c: uds.models.cache = uds.models.cache.objects.get(pk=key) # @UndefinedVariable # If expired if now > c.created + timedelta(seconds=c.validity): return defValue @@ -86,7 +86,7 @@ class Cache: Cache.hits += 1 return val - except uds.models.Cache.DoesNotExist: # @UndefinedVariable + except uds.models.cache.DoesNotExist: # @UndefinedVariable Cache.misses += 1 logger.debug('key not found: %s', skey) return defValue @@ -103,9 +103,9 @@ class Cache: # logger.debug('Removing key "%s" for uService "%s"' % (skey, self._owner)) try: key = self.__getKey(skey) - uds.models.Cache.objects.get(pk=key).delete() # @UndefinedVariable + uds.models.cache.objects.get(pk=key).delete() # @UndefinedVariable return True - except uds.models.Cache.DoesNotExist: # @UndefinedVariable + except uds.models.cache.DoesNotExist: # @UndefinedVariable logger.debug('key not found') return False @@ -120,11 +120,11 @@ class Cache: value = typing.cast(str, encoders.encode(pickle.dumps(value), 'base64', asText=True)) now: datetime = typing.cast(datetime, getSqlDatetime()) try: - uds.models.Cache.objects.create(owner=self._owner, key=key, value=value, created=now, validity=validity) # @UndefinedVariable + uds.models.cache.objects.create(owner=self._owner, key=key, value=value, created=now, validity=validity) # @UndefinedVariable except Exception: try: # Already exists, modify it - c: uds.models.Cache = uds.models.Cache.objects.get(pk=key) # @UndefinedVariable + c: uds.models.cache = uds.models.cache.objects.get(pk=key) # @UndefinedVariable c.owner = self._owner c.key = key c.value = value @@ -138,26 +138,26 @@ class Cache: # logger.debug('Refreshing key "%s" for cache "%s"' % (skey, self._owner,)) try: key = self.__getKey(skey) - c = uds.models.Cache.objects.get(pk=key) # @UndefinedVariable + c = uds.models.cache.objects.get(pk=key) # @UndefinedVariable c.created = getSqlDatetime() c.save() - except uds.models.Cache.DoesNotExist: # @UndefinedVariable + except uds.models.cache.DoesNotExist: # @UndefinedVariable logger.debug('Can\'t refresh cache key %s because it doesn\'t exists', skey) return @staticmethod def purge() -> None: - uds.models.Cache.objects.all().delete() # @UndefinedVariable + uds.models.cache.objects.all().delete() # @UndefinedVariable @staticmethod def cleanUp() -> None: - uds.models.Cache.cleanUp() # @UndefinedVariable + uds.models.cache.cleanUp() # @UndefinedVariable @staticmethod def delete(owner: typing.Optional[str] = None) -> None: # logger.info("Deleting cache items") if owner is None: - objects = uds.models.Cache.objects.all() # @UndefinedVariable + objects = uds.models.cache.objects.all() # @UndefinedVariable else: - objects = uds.models.Cache.objects.filter(owner=owner) # @UndefinedVariable + objects = uds.models.cache.objects.filter(owner=owner) # @UndefinedVariable objects.delete() diff --git a/server/src/uds/core/util/calendar/__init__.py b/server/src/uds/core/util/calendar/__init__.py index b3d1a05f..bd185277 100644 --- a/server/src/uds/core/util/calendar/__init__.py +++ b/server/src/uds/core/util/calendar/__init__.py @@ -39,7 +39,7 @@ import bitarray from uds.models.Util import NEVER from uds.models.Util import getSqlDatetime -from uds.models.Calendar import Calendar +from uds.models.calendar import Calendar from uds.core.util.Cache import Cache diff --git a/server/src/uds/models/MetaPool.py b/server/src/uds/models/MetaPool.py index 32097cdb..884a9d3f 100644 --- a/server/src/uds/models/MetaPool.py +++ b/server/src/uds/models/MetaPool.py @@ -48,7 +48,7 @@ from uds.models.Image import Image from uds.models.ServicesPoolGroup import ServicesPoolGroup from uds.models.ServicesPool import ServicePool from uds.models.Group import Group -from uds.models.Calendar import Calendar +from uds.models.calendar import Calendar if typing.TYPE_CHECKING: import datetime diff --git a/server/src/uds/models/ServicesPool.py b/server/src/uds/models/ServicesPool.py index d7c16474..4373f3f7 100644 --- a/server/src/uds/models/ServicesPool.py +++ b/server/src/uds/models/ServicesPool.py @@ -51,7 +51,7 @@ from uds.models.Transport import Transport from uds.models.Group import Group from uds.models.Image import Image from uds.models.ServicesPoolGroup import ServicesPoolGroup -from uds.models.Calendar import Calendar +from uds.models.calendar import Calendar from uds.models.account import Account from uds.models.Util import NEVER diff --git a/server/src/uds/models/__init__.py b/server/src/uds/models/__init__.py index 179fa34d..72d5dcb5 100644 --- a/server/src/uds/models/__init__.py +++ b/server/src/uds/models/__init__.py @@ -75,7 +75,7 @@ from .StatsEvents import StatsEvents # General utility models, such as a database cache (for caching remote content of slow connections to external services providers for example) # We could use django cache (and maybe we do it in a near future), but we need to clean up things when objecs owning them are deleted -from .Cache import Cache +from .cache import Cache from .Config import Config from .Storage import Storage from .UniqueId import UniqueId @@ -91,11 +91,11 @@ from .Image import Image from .TicketStore import TicketStore # Calendar related -from .Calendar import Calendar -from .CalendarRule import CalendarRule +from .calendar import Calendar +from .calendar_rule import CalendarRule -from .CalendarAccess import CalendarAccess, CalendarAccessMeta -from .CalendarAction import CalendarAction +from .calendar_access import CalendarAccess, CalendarAccessMeta +from .calendar_action import CalendarAction # Accounting from .account import Account diff --git a/server/src/uds/models/Cache.py b/server/src/uds/models/cache.py similarity index 100% rename from server/src/uds/models/Cache.py rename to server/src/uds/models/cache.py diff --git a/server/src/uds/models/Calendar.py b/server/src/uds/models/calendar.py similarity index 95% rename from server/src/uds/models/Calendar.py rename to server/src/uds/models/calendar.py index c74d4f84..adb2ef82 100644 --- a/server/src/uds/models/Calendar.py +++ b/server/src/uds/models/calendar.py @@ -1,7 +1,5 @@ # -*- coding: utf-8 -*- -# Model based on https://github.com/llazzaro/django-scheduler -# # Copyright (c) 2016-2019 Virtual Cable S.L. # All rights reserved. # @@ -41,7 +39,7 @@ from uds.models.Tag import TaggingMixin logger = logging.getLogger(__name__) -class Calendar(UUIDModel, TaggingMixin): # type: ignore +class Calendar(UUIDModel, TaggingMixin): name = models.CharField(max_length=128, default='') comments = models.CharField(max_length=256, default='') diff --git a/server/src/uds/models/CalendarAccess.py b/server/src/uds/models/calendar_access.py similarity index 94% rename from server/src/uds/models/CalendarAccess.py rename to server/src/uds/models/calendar_access.py index 974a646a..d4661add 100644 --- a/server/src/uds/models/CalendarAccess.py +++ b/server/src/uds/models/calendar_access.py @@ -34,11 +34,13 @@ import logging from django.db import models + from uds.core.util import states -from uds.models.UUIDModel import UUIDModel -from uds.models.Calendar import Calendar -from uds.models.ServicesPool import ServicePool -from uds.models.MetaPool import MetaPool + +from .UUIDModel import UUIDModel +from .calendar import Calendar +from .ServicesPool import ServicePool +from .MetaPool import MetaPool logger = logging.getLogger(__name__) diff --git a/server/src/uds/models/CalendarAction.py b/server/src/uds/models/calendar_action.py similarity index 97% rename from server/src/uds/models/CalendarAction.py rename to server/src/uds/models/calendar_action.py index ac4ff913..c189df4d 100644 --- a/server/src/uds/models/CalendarAction.py +++ b/server/src/uds/models/calendar_action.py @@ -39,13 +39,15 @@ import typing from django.utils.translation import ugettext_lazy as _ from django.db import models -from uds.models.Calendar import Calendar -from uds.models.UUIDModel import UUIDModel -from uds.models.Util import getSqlDatetime + from uds.core.util import (calendar, log) -from uds.models.ServicesPool import ServicePool -from uds.models.Transport import Transport -from uds.models.authenticator import Authenticator + +from .calendar import Calendar +from .UUIDModel import UUIDModel +from .Util import getSqlDatetime +from .ServicesPool import ServicePool +from .Transport import Transport +from .authenticator import Authenticator # from django.utils.translation import ugettext_lazy as _, ugettext diff --git a/server/src/uds/models/CalendarRule.py b/server/src/uds/models/calendar_rule.py similarity index 99% rename from server/src/uds/models/CalendarRule.py rename to server/src/uds/models/calendar_rule.py index 1eedb13f..237b27a9 100644 --- a/server/src/uds/models/CalendarRule.py +++ b/server/src/uds/models/calendar_rule.py @@ -40,7 +40,7 @@ from django.utils.translation import ugettext_lazy as _ from dateutil import rrule as rules from .UUIDModel import UUIDModel -from .Calendar import Calendar +from .calendar import Calendar from .Util import getSqlDatetime