summaryrefslogtreecommitdiffstats
path: root/devel/management/commands/rematch_developers.py
blob: ab2f0f4b20a0717e56fcde3bfaa310ab974bedbd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# -*- coding: utf-8 -*-
"""
rematch_developers command

Match all packages with a packager_str but NULL packager_id to a packager if we
can find one.

Also, match all flag requests with a NULL user_id that have a user_email
matching up to a developer if we can find one.

Usage: ./manage.py rematch_developers
"""

from django.core.management.base import NoArgsCommand
from django.db import transaction

import sys
import logging

from devel.utils import UserFinder
from main.models import Package
from packages.models import FlagRequest

logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s -> %(levelname)s: %(message)s',
    datefmt='%Y-%m-%d %H:%M:%S',
    stream=sys.stderr)
logger = logging.getLogger()

class Command(NoArgsCommand):
    help = "Match and map objects in database to developer emails"

    def handle_noargs(self, **options):
        v = int(options.get('verbosity', None))
        if v == 0:
            logger.level = logging.ERROR
        elif v == 1:
            logger.level = logging.INFO
        elif v == 2:
            logger.level = logging.DEBUG

        finder = UserFinder()
        match_packager(finder)
        match_flagrequest(finder)

@transaction.commit_on_success
def match_packager(finder):
    logger.info("getting all unmatched packager strings")
    package_count = matched_count = 0
    mapping = {}

    unmatched = Package.objects.filter(packager__isnull=True).values_list(
            'packager_str', flat=True).order_by().distinct()

    for packager in unmatched:
        logger.debug("packager string %s", packager)
        user = finder.find(packager)
        if user:
            mapping[packager] = user
            logger.debug("  found user %s" % user.username)
            matched_count += 1

    for packager_str, user in mapping.items():
        package_count += Package.objects.filter(packager__isnull=True,
                packager_str=packager_str).update(packager=user)

    logger.info("%d packages updated, %d packager strings matched",
            package_count, matched_count)


@transaction.commit_on_success
def match_flagrequest(finder):
    logger.info("getting all flag requests emails from unknown users")
    req_count = matched_count = 0
    mapping = {}

    unmatched = FlagRequest.objects.filter(user__isnull=True).values_list(
            'user_email', flat=True).order_by().distinct()
    
    for user_email in unmatched:
        logger.debug("email %s", user_email)
        user = finder.find_by_email(user_email)
        if user:
            mapping[user_email] = user
            logger.debug("  found user %s" % user.username)
            matched_count += 1

    for user_email, user in mapping.items():
        req_count += FlagRequest.objects.filter(user__isnull=True,
                user_email=user_email).update(user=user)

    logger.info("%d request emails updated, %d emails matched",
            req_count, matched_count)

# vim: set ts=4 sw=4 et: