Coverage for portality/models/__init__.py: 90%

52 statements  

« prev     ^ index     » next       coverage.py v6.4.2, created at 2022-08-04 15:38 +0100

1from typing import Any 

2# import the versioned objects, so that the current version is the default one 

3from portality.models.v2 import shared_structs 

4from portality.models.v2.bibjson import JournalLikeBibJSON 

5from portality.models.v2.journal import Journal, JournalQuery, IssnQuery, PublisherQuery, TitleQuery, ContinuationException 

6from portality.models.v2.application import Application, SuggestionQuery, OwnerStatusQuery, DraftApplication, AllPublisherApplications 

7 

8from portality.models.v2.application import Application as Suggestion 

9 

10# non versioned imports for convenience and consistency 

11from portality.models.lcc import LCC 

12from portality.models.account import Account 

13from portality.models.editors import EditorGroup, EditorGroupMemberQuery, EditorGroupQuery 

14from portality.models.uploads import FileUpload, ExistsFileQuery, OwnerFileQuery, ValidFileQuery 

15from portality.models.lock import Lock 

16from portality.models.history import ArticleHistory, JournalHistory 

17from portality.models.article import Article, ArticleBibJSON, ArticleQuery, ArticleVolumesQuery, DuplicateArticleQuery, NoJournalException 

18from portality.models.oaipmh import OAIPMHRecord, OAIPMHJournal, OAIPMHArticle 

19from portality.models.atom import AtomRecord 

20from portality.models.search import JournalArticle, JournalStatsQuery, ArticleStatsQuery 

21from portality.models.cache import Cache 

22from portality.models.openurl import OpenURLRequest 

23from portality.models.provenance import Provenance 

24from portality.models.background import BackgroundJob 

25from portality.models.preservation import PreservationState 

26from portality.models.news import News 

27from portality.models.harvester import HarvestState 

28from portality.models.event import Event 

29from portality.models.notifications import Notification 

30 

31import sys 

32 

33 

34def lookup_model(name='', capitalize=True, split_on="_"): 

35 parts = name.split(split_on) 

36 if capitalize: 

37 parts = [p.capitalize() for p in parts] 

38 name = "".join(parts) 

39 try: 

40 return getattr(sys.modules[__name__], name) 

41 except: 

42 return None 

43 

44 

45def lookup_models_by_type(name: str, parent: Any): 

46 """ Inspect this (models) module, retrieve model for given type """ 

47 import inspect 

48 current_module = sys.modules[__name__] 

49 for classname, obj in inspect.getmembers(current_module): 

50 if not inspect.isclass(obj): 

51 continue 

52 if not issubclass(obj, parent): 

53 continue 

54 if obj.__type__ == name: 

55 return obj 

56 

57 

58############################################################################ 

59# Generic/Utility classes and functions 

60############################################################################ 

61class ObjectDict(object): 

62 """WTForms requires an object in order to work properly, not a dict or an unpacked dict.""" 

63 def __init__(self, d): 

64 super(ObjectDict, self).__setattr__('data', d) 

65 

66 def __getattr__(self, item): 

67 return self.data[item] 

68 

69 def __setattr__(self, key, value): 

70 self.data[key] = value