Improve client version-related logic
This commit is contained in:
		
							parent
							
								
									6b09780eac
								
							
						
					
					
						commit
						ef140fe11d
					
				
					 4 changed files with 38 additions and 2 deletions
				
			
		| 
						 | 
					@ -1,3 +1,7 @@
 | 
				
			||||||
 | 
					class UnknownClientVersionError(Exception):
 | 
				
			||||||
 | 
					    pass
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class UnknownConnectionTypeError(Exception):
 | 
					class UnknownConnectionTypeError(Exception):
 | 
				
			||||||
    pass
 | 
					    pass
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,4 +1,4 @@
 | 
				
			||||||
from core.Constants import Constants
 | 
					from core.Errors import UnknownClientVersionError
 | 
				
			||||||
from core.controllers.ApplicationController import ApplicationController
 | 
					from core.controllers.ApplicationController import ApplicationController
 | 
				
			||||||
from core.controllers.ApplicationVersionController import ApplicationVersionController
 | 
					from core.controllers.ApplicationVersionController import ApplicationVersionController
 | 
				
			||||||
from core.controllers.ClientVersionController import ClientVersionController
 | 
					from core.controllers.ClientVersionController import ClientVersionController
 | 
				
			||||||
| 
						 | 
					@ -8,6 +8,7 @@ from core.controllers.SubscriptionPlanController import SubscriptionPlanControll
 | 
				
			||||||
from core.observers import ClientObserver
 | 
					from core.observers import ClientObserver
 | 
				
			||||||
from core.observers.ConnectionObserver import ConnectionObserver
 | 
					from core.observers.ConnectionObserver import ConnectionObserver
 | 
				
			||||||
from typing import Optional
 | 
					from typing import Optional
 | 
				
			||||||
 | 
					import os
 | 
				
			||||||
import pathlib
 | 
					import pathlib
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -17,6 +18,26 @@ class ClientController:
 | 
				
			||||||
    def get_working_directory():
 | 
					    def get_working_directory():
 | 
				
			||||||
        return str(pathlib.Path(__file__).resolve().parent.parent.parent)
 | 
					        return str(pathlib.Path(__file__).resolve().parent.parent.parent)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @staticmethod
 | 
				
			||||||
 | 
					    def get_version():
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        version_number = os.environ.get('HV_CLIENT_VERSION')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if version_number is None:
 | 
				
			||||||
 | 
					            raise UnknownClientVersionError('The client version could not be determined.')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return ClientVersionController.get_or_new(version_number)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @staticmethod
 | 
				
			||||||
 | 
					    def can_be_updated():
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        try:
 | 
				
			||||||
 | 
					            version = ClientController.get_version()
 | 
				
			||||||
 | 
					        except UnknownClientVersionError:
 | 
				
			||||||
 | 
					            return False
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return not ClientVersionController.is_latest(version)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @staticmethod
 | 
					    @staticmethod
 | 
				
			||||||
    def sync(client_observer: ClientObserver = None, connection_observer: ConnectionObserver = None):
 | 
					    def sync(client_observer: ClientObserver = None, connection_observer: ConnectionObserver = None):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -17,6 +17,16 @@ class ClientVersionController:
 | 
				
			||||||
    def get(version_number: str):
 | 
					    def get(version_number: str):
 | 
				
			||||||
        return ClientVersion.find(version_number)
 | 
					        return ClientVersion.find(version_number)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @staticmethod
 | 
				
			||||||
 | 
					    def get_or_new(version_number: str):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        client_version = ClientVersionController.get(version_number)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if client_version is None:
 | 
				
			||||||
 | 
					            return ClientVersion(version_number)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return client_version
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @staticmethod
 | 
					    @staticmethod
 | 
				
			||||||
    def get_all():
 | 
					    def get_all():
 | 
				
			||||||
        return ClientVersion.all()
 | 
					        return ClientVersion.all()
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -20,7 +20,8 @@ _table_definition: str = """
 | 
				
			||||||
@dataclass
 | 
					@dataclass
 | 
				
			||||||
class ClientVersion(Model):
 | 
					class ClientVersion(Model):
 | 
				
			||||||
    version_number: str
 | 
					    version_number: str
 | 
				
			||||||
    released_at: datetime = field(
 | 
					    released_at: Optional[datetime] = field(
 | 
				
			||||||
 | 
					        default=None,
 | 
				
			||||||
        metadata=config(
 | 
					        metadata=config(
 | 
				
			||||||
            encoder=datetime.isoformat,
 | 
					            encoder=datetime.isoformat,
 | 
				
			||||||
            decoder=datetime.fromisoformat,
 | 
					            decoder=datetime.fromisoformat,
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in a new issue