sponsor_metrics.py 5.95 KB
Newer Older
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
# -*- coding: utf-8 -*-
#
#   sponsor_metrics.py — rudimentary model for sponsor metrics
#
#   This file is part of debexpo - http://debexpo.workaround.org
#
#   Copyright © 2011 Arno Töll <debian@toell.net>
#
#   Permission is hereby granted, free of charge, to any person
#   obtaining a copy of this software and associated documentation
#   files (the "Software"), to deal in the Software without
#   restriction, including without limitation the rights to use,
#   copy, modify, merge, publish, distribute, sublicense, and/or sell
#   copies of the Software, and to permit persons to whom the
#   Software is furnished to do so, subject to the following
#   conditions:
#
#   The above copyright notice and this permission notice shall be
#   included in all copies or substantial portions of the Software.
#
#   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
#   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
#   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
#   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
#   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
#   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
#   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
#   OTHER DEALINGS IN THE SOFTWARE.

"""
Holds Sponsor Metrics Model
"""

__author__ = 'Arno Töll'
__copyright__ = 'Copyright © 2011 Arno Töll'
__license__ = 'MIT'

import os
import datetime

import sqlalchemy as sa
from sqlalchemy import orm
43
from sqlalchemy.ext.associationproxy import association_proxy
44
45
46
47
48
49
50
51
52
53
54
55
56
57

from debexpo.model import meta, OrmObject
from debexpo.model.users import User
from debexpo.lib import constants
import debexpo.lib.utils

t_sponsor_metrics = sa.Table(
    'sponsor_metrics', meta.metadata,
    sa.Column('user_id', sa.types.Integer, sa.ForeignKey('users.id'), primary_key=True),
    sa.Column('availability', sa.types.Integer, nullable=False),
    sa.Column('contact', sa.types.Integer, nullable=False),
    sa.Column('types', sa.types.Text, nullable=True),
    sa.Column('guidelines', sa.types.Integer, nullable=True),
    sa.Column('guidelines_text', sa.types.Text, nullable=True),
58
    sa.Column('social_requirements', sa.types.Text, nullable=True),
59
60
    )

61
62
63
64
65
66
67
68
69
70
t_sponsor_tags = sa.Table(
    'sponsor_tags', meta.metadata,
    sa.Column('tag_type', sa.types.Integer, nullable=False),
    sa.Column('tag', sa.types.Text, primary_key=True),
    sa.Column('label', sa.types.Text, nullable=False),
    sa.Column('long_description', sa.types.Text, nullable=False),
)

t_sponsor_metrics_tags = sa.Table(
    'sponsor_metrics_tags', meta.metadata,
71
    sa.Column('tag', sa.Text, sa.ForeignKey('sponsor_tags.tag'), primary_key=True),
72
    sa.Column('user_id', sa.Integer, sa.ForeignKey('sponsor_metrics.user_id'), primary_key=True),
73
    sa.Column('weight', sa.Integer),
74
75
)

76
77
class SponsorMetrics(OrmObject):
    foreign = ['user']
78
79
80
81
82
83
84
85

    def get_all_tags_weighted(self, weight = 0):
        if weight > 0:
            return [x.tag for x in self.tags if x.weight > 0]
        elif weight < 0:
            return [x.tag for x in self.tags if x.weight < 0]
        else:
            return [x.tag for x in self.tags]
86

87
    def get_all_tags(self):
88
        return get_all_tags_weighted(0)
89

90
91
    def get_technical_tags(self):
        return [x.tag for x in self.get_technical_tags_full()]
92

93
94
    def get_social_tags(self):
        return [x.tag for x in self.get_social_tags_full()]
95

96
    def get_technical_tags_full(self):
97
        return [x for x in self.tags if x.full_tag.tag_type == constants.SPONSOR_METRICS_TYPE_TECHNICAL]
98

99
    def get_social_tags_full(self):
100
101
102
103
104
105
106
        return [x for x in self.tags if x.full_tag.tag_type == constants.SPONSOR_METRICS_TYPE_SOCIAL]

    def get_tag_weight(self, tag):
        for t in self.tags:
            if t.tag == tag:
                return t.weight
        return 0.0
107
108
109
110
111
112

    def get_guidelines(self):
        """
        Return a formatted and sanitized string of the guidelines the sponsor
        configured
        """
113
        return self.guidelines_text
114
115
116
117
118
119
120
121

    def get_types(self):
        """
        Return a formatted and sanitized string of the packages the sponsor
        is interested in
        """

        if self.types:
122
            return self.types
123
        return ""
124

125
126
127
128
129
130
131
    def get_social_requirements(self):
        """
        Return a formatted and sanitized string of the social requirements the sponsor
        configured
        """

        if self.social_requirements:
132
            return self.social_requirements
133
        else:
134
            return ""
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149

    def allowed(self, flag):
        """
        Returns true if the user associated with this object allowed to display a
        contact address. This method also honors the SPONSOR_METRICS_RESTRICTED flag

        ```flag``` A SPONSOR_CONTACT_METHOD_* flag which should be checked
        """
        if self.availability == constants.SPONSOR_METRICS_RESTRICTED and self.contact == flag:
            return True
        elif self.availability == constants.SPONSOR_METRICS_PUBLIC:
            return True
        return False


150
151
class SponsorTags(OrmObject):
    pass
152
    #keywords = association_proxy('metrics', 'metric')
153

154
class SponsorMetricsTags(OrmObject):
155
    foreign = ['user', 'tags']
156

157
orm.mapper(SponsorMetrics, t_sponsor_metrics, properties={
158
159
    'tags' : orm.relationship(SponsorMetricsTags),
    'user'  : orm.relationship(User),
160
})
161
162
163
164


orm.mapper(SponsorTags, t_sponsor_tags)

165
166
167
168
169
orm.mapper(SponsorMetricsTags, t_sponsor_metrics_tags, properties={
    'full_tag': orm.relationship(SponsorTags)
})


170
def create_tags():
171
    import debexpo.model.data.tags
172
173
    import logging
    for metrics_type in [constants.SPONSOR_METRICS_TYPE_TECHNICAL, constants.SPONSOR_METRICS_TYPE_SOCIAL]:
174
        for (description, tag, long_description) in debexpo.model.data.tags.TAGS[metrics_type]:
175
176
177
178
            st = SponsorTags(tag_type=metrics_type, tag=tag, label=description, long_description=long_description)
            logging.info("Adding tag %s" % (tag))
            meta.session.merge(st)
    meta.session.commit()