~dylanmccall/harvest/gsoc-client-stuff

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
from django.db.models import Count
from harvest.common.url_tools import current_url_with_parameters

class PackageWrapper(object):
    """
    Describes a visible source package, for specific use in a
    template.
    """
    
    def __init__(self, request, package, visible_opportunities = None, expanded = False):
        self.request = request
        self.package = package
        self.visible_opportunities = visible_opportunities
        self.expanded = expanded
    
    def real(self):
        return self.package
    
    def get_expand_toggle_url(self):
        parameter = {'expand_pkg' : self.package.id}
        url = current_url_with_parameters(self.request, parameter)
        return url
    
    #FIXME: get_visible_opportunities and get_hidden_opportunities feel
    #       wasteful. Could we do exclude and filter in a single
    #       operation? Does it affect performance?
    def get_visible_opportunities(self):
        """
        Returns opportunities that belong to the given package and are
        in opportunities_list.
        """
        #also check if valid?
        return self.visible_opportunities
    
    def get_hidden_opportunities(self):
        """
        Returns opportunities that belong to the given package but have
        been hidden from view
        """
        opps_visible = self.get_visible_opportunities()
        return self.package.opportunity_set.exclude(id__in=opps_visible)

class PackageListWrapper(object):
    """
    Object describing a list of source packages and opportunities, to
    be used by a template. It contains UI-specific variables and simple
    helper functions for doing final queries to access these lists.
    """
    
    def __init__(self, request, packages_list, opportunities_list):
        expand_list = list() #list of packages to show in detail
        if 'expand_pkg' in request.GET:
            expand_list = request.GET['expand_pkg'].split(',')
            expand_list = [int(i) for i in expand_list if i.isdigit()]
        
        related_packages = set(opportunities_list.values_list('sourcepackage', flat=True))
        
        self.visible_packages_list = list()
        self.hidden_packages_list = list() 
        
        #Create a PackageWrapper around every source package.
        #Includes a less detailed wrapper for hidden packages.
        for package in packages_list:
            if package.pk in related_packages:
                opps = None
                expand = False
                
                expand = (package.id in expand_list)
                opps = opportunities_list.filter(sourcepackage=package)
                
                package_wrapper = PackageWrapper(request, package,
                                                 visible_opportunities = opps,
                                                 expanded = expand)
                self.visible_packages_list.append(package_wrapper)
            
            else:
                package_wrapper = PackageWrapper(request, package)
                self.hidden_packages_list.append(package_wrapper)
    
    def get_visible_packages(self):
        """
        Returns list of packages that are are visible.
        These are any packages that contain opportunities.
        """
        return self.visible_packages_list
    
    def get_hidden_packages(self):
        """
        Returns list of packages that have been hidden from view.
        """
        return self.hidden_packages_list