Coverage for apis_core/history/views.py: 48%

31 statements  

« prev     ^ index     » next       coverage.py v7.6.4, created at 2024-11-22 07:51 +0000

1from django.contrib.contenttypes.models import ContentType 

2from django.shortcuts import redirect 

3from django.urls import reverse 

4from django.utils import timezone 

5from django.views.generic.detail import DetailView 

6from django_tables2 import SingleTableMixin 

7from django_tables2.tables import table_factory 

8 

9from apis_core.generic.helpers import first_member_match, module_paths 

10from apis_core.generic.views import GenericModelMixin 

11 

12from .tables import HistoryGenericTable 

13 

14 

15class HistoryView(GenericModelMixin, SingleTableMixin, DetailView): 

16 template_name = "history/history.html" 

17 

18 def get_table_class(self): 

19 table_modules = module_paths(self.model, path="tables", suffix="HistoryTable") 

20 table_class = first_member_match(table_modules, HistoryGenericTable) 

21 return table_factory(self.model, table_class) 

22 

23 def get_table_data(self): 

24 return self.get_object().get_history_data() 

25 

26 

27def create_new_version(request, contenttype, pk): 

28 """Gets the version of the history instance and creates a new version.""" 

29 model = contenttype.model_class() 

30 instance = model.objects.get(id=pk) 

31 history_latest = instance.history.latest() 

32 latest_version_list = [ 

33 int(x.replace("v", "")) 

34 for x in instance.history.filter(version_tag__isnull=False).values_list( 

35 "version_tag", flat=True 

36 ) 

37 ] 

38 history_latest.history_id = None 

39 history_latest.history_date = timezone.now() 

40 history_latest.save() 

41 if latest_version_list: 

42 latest_version = max(latest_version_list) 

43 else: 

44 latest_version = 0 

45 history_latest.set_version_tag(f"v{latest_version + 1}") 

46 return redirect( 

47 reverse( 

48 "apis_core:generic:detail", 

49 args=[ 

50 ContentType.objects.get_for_model(history_latest.__class__), 

51 history_latest.history_id, 

52 ], 

53 ) 

54 )