-
-
Notifications
You must be signed in to change notification settings - Fork 964
/
agreement.py
58 lines (47 loc) · 1.99 KB
/
agreement.py
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
# -*- coding: utf-8 -*-
#
# Copyright © 2012 - 2020 Michal Čihař <michal@cihar.com>
#
# This file is part of Weblate <https://weblate.org/>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
from django.conf import settings
from django.db import models
class ContributorAgreementManager(models.Manager):
def has_agreed(self, user, component):
cache_key = ('cla', user.pk, component.pk)
if cache_key not in user.perm_cache:
user.perm_cache[cache_key] = self.filter(
component=component, user=user
).exists()
return user.perm_cache[cache_key]
def create(self, user, component, **kwargs):
user.perm_cache[('cla', user.pk, component.pk)] = True
return super(ContributorAgreementManager, self).create(
user=user, component=component, **kwargs
)
def order(self):
return self.order_by('component__project__name', 'component__name')
class ContributorAgreement(models.Model):
user = models.ForeignKey(
settings.AUTH_USER_MODEL, on_delete=models.deletion.CASCADE
)
component = models.ForeignKey('Component', on_delete=models.deletion.CASCADE)
timestamp = models.DateTimeField(auto_now=True)
objects = ContributorAgreementManager()
class Meta(object):
unique_together = [('user', 'component')]
def __str__(self):
return '{0}:{1}'.format(self.user.username, self.component)