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"""Command to import tags data from an old PTS dump."""
12import sys
14from django.core.exceptions import ValidationError
15from django.core.management.base import BaseCommand
16from django.db import transaction
18from distro_tracker.core.models import (
19 EmailSettings,
20 Keyword,
21 Subscription,
22 UserEmail
23)
26class Command(BaseCommand):
27 """
28 Import the old PTS user default keywords and package-specific keywords.
29 The expected input is the output of the ``bin/dump-tags.pl`` file on stdin.
30 """
31 stdin = sys.stdin
33 def write(self, message):
34 if self.verbose:
35 self.stdout.write(message)
37 @transaction.atomic
38 def handle(self, *args, **kwargs):
39 self.verbose = int(kwargs.get('verbosity', 1)) > 1
41 # Each line can have one of the two possible formats:
42 # <email>: <tag1>,<tag2>,...
43 # or
44 # <email>#<package-name>: <tag1>,<tag2>
46 for line in self.stdin:
47 email, tags = line.rsplit(':', 1)
48 tags = tags.strip().split(',')
49 tags = [tag.strip() for tag in tags]
50 # Map the keyword names that have been changed to their new values
51 legacy_mapping = {
52 'katie-other': 'archive',
53 'buildd': 'build',
54 'ddtp': 'translation',
55 'cvs': 'vcs',
56 }
57 keywords = [
58 legacy_mapping.get(tag, tag)
59 for tag in tags
60 ]
61 keywords = Keyword.objects.filter(name__in=keywords)
62 email = email.strip()
63 if '#' in email:
64 # A subscription specific set of keywords
65 email, package = email.split('#', 1)
66 try:
67 subscription = Subscription.objects.get(
68 package__name=package,
69 email_settings__user_email__email=email)
70 except Subscription.DoesNotExist:
71 self.write('Skip non-existing subscription '
72 '({email} to {package}).\n'.format(
73 email=email, package=package))
74 continue
75 subscription.keywords.clear()
76 for keyword in keywords:
77 subscription.keywords.add(keyword)
78 else:
79 # User default keywords
80 try:
81 user_email, _ = UserEmail.objects.get_or_create(email=email)
82 except ValidationError:
83 self.write('Skip invalid email {email}.\n'.format(
84 email=email))
85 continue
86 email_settings, _ = \
87 EmailSettings.objects.get_or_create(user_email=user_email)
88 email_settings.default_keywords.set(keywords)