1# Copyright 2013 The Distro Tracker Developers 

2# See the COPYRIGHT file at the top-level directory of this distribution and 

3# at https://deb.li/DTAuthors 

4# 

5# This file is part of Distro Tracker. It is subject to the license terms 

6# in the LICENSE file found in the top-level directory of this 

7# distribution and at https://deb.li/DTLicense. No part of Distro Tracker, 

8# including this file, may be copied, modified, propagated, or distributed 

9# except according to the terms contained in the LICENSE file. 

10""" 

11The Distro-Tracker-specific tasks for :mod:`distro_tracker.auto_news` app. 

12""" 

13from distro_tracker.core.models import News, SourcePackageName 

14from distro_tracker.core.tasks import BaseTask 

15from distro_tracker.core.tasks.mixins import ProcessRepositoryUpdates 

16from distro_tracker.core.tasks.schedulers import IntervalScheduler 

17from distro_tracker.core.utils.http import get_resource_content 

18 

19 

20class GenerateNewsFromRepositoryUpdates(BaseTask, ProcessRepositoryUpdates): 

21 

22 class Scheduler(IntervalScheduler): 

23 interval = 3600 

24 

25 def generate_accepted_news_content(self, srcpkg): 

26 """ 

27 Generates the content for a news item created when a package version is 

28 first created. 

29 

30 :type srcpkg: :class:`~distro_tracker.core.models.SourcePackage` 

31 """ 

32 entry = srcpkg.repository_entries.all()[0] 

33 

34 # Add dsc file 

35 content = get_resource_content(entry.dsc_file_url) 

36 if content: 

37 content = content.decode('utf-8') 

38 else: 

39 content = '' 

40 

41 # Add changelog entries since last update... 

42 changelog_content = srcpkg.get_changelog_entry() 

43 if changelog_content: 

44 content += '\n<span id="changes">Changes:</span>\n' 

45 content += changelog_content 

46 

47 return content 

48 

49 def add_accepted_news(self, entry): 

50 title = "{pkg} {ver} has been added to {repo}".format( 

51 pkg=entry.source_package.name, 

52 ver=entry.source_package.version, 

53 repo=entry.repository.name, 

54 ) 

55 content = self.generate_accepted_news_content(entry.source_package) 

56 News.objects.create( 

57 package=entry.source_package.source_package_name, 

58 title=title, 

59 _db_content=content 

60 ) 

61 

62 def add_migrated_news(self, entry): 

63 title = "{pkg} {ver} migrated to {repo}".format( 

64 pkg=entry.source_package.name, 

65 ver=entry.source_package.version, 

66 repo=entry.repository.name, 

67 ) 

68 News.objects.create( 

69 package=entry.source_package.source_package_name, 

70 title=title 

71 ) 

72 

73 def add_removed_news(self, package, repository): 

74 title = "{pkg} has been removed from {repo}".format( 

75 pkg=package, 

76 repo=repository.name, 

77 ) 

78 pkgname = SourcePackageName.objects.get(name=package) 

79 pkgname.news_set.create(title=title) 

80 

81 def execute_main(self): 

82 for entry in self.items_to_process(): 

83 if entry.repository.get_flags()['hidden']: 

84 self.item_mark_processed(entry) 

85 continue 

86 if self.is_new_source_package(entry.source_package): 

87 self.add_accepted_news(entry) 

88 else: 

89 self.add_migrated_news(entry) 

90 self.item_mark_processed(entry) 

91 

92 for package, repository in self.iter_removals_by_repository(): 

93 if repository.get_flags()['hidden']: 

94 continue 

95 self.add_removed_news(package, repository)