aboutsummaryrefslogtreecommitdiff
path: root/license_protected_downloads/models.py
blob: 3e98175e3817309e048ac577d6b2c6a9b0985336 (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import datetime
import logging
import uuid

from django.conf import settings
from django.db import models
from django.db.models import Count


def ip_field(required=True):
    # we are on an old version of django missing an ipv6 friendly field
    # so just use a charfield to keep it simple
    if required:
        return models.CharField(max_length=40)
    return models.CharField(max_length=40, blank=True, null=True)


def get_ip(request):
    # fields taken from:
    #  https://github.com/un33k/django-ipware/blob/master/ipware/defaults.py
    ip_meta_vals = (
        'HTTP_X_FORWARDED_FOR',
        'HTTP_CLIENT_IP',
        'HTTP_X_REAL_IP',
        'HTTP_X_FORWARDED',
        'HTTP_X_CLUSTER_CLIENT_IP',
        'HTTP_FORWARDED_FOR',
        'HTTP_FORWARDED',
        'HTTP_VIA',
        'X_FORWARDED_FOR',
        'REMOTE_ADDR',
    )
    for field in ip_meta_vals:
        ip = request.META.get(field)
        if ip:
            return ip.split(',')[0]
    return 'unkwnown'


class LicenseManager(models.Manager):
    """
    Model manager for License model.

    Provides additional convenience method
    """

    def all_with_hashes(self, hash_list):
        """
        Produce a list of licenses that match the specified list of hashes.
        The main use case is to convert license digest to something the user
        can relate to.
        """
        return self.all().filter(digest__in=hash_list)


class License(models.Model):
    digest = models.CharField(max_length=40)
    text = models.TextField()
    theme = models.CharField(max_length=60)

    objects = LicenseManager()

    def __unicode__(self):
        return self.digest


class APIKeyStore(models.Model):
    key = models.CharField(max_length=80)
    public = models.BooleanField()

    description = models.CharField(max_length=256, default='')
    last_used = models.DateTimeField(auto_now=True, blank=True, null=True)

    def __unicode__(self):
        return '%s: %s' % (self.description, self.public)


class APIToken(models.Model):
    '''Represents an API token that will be valid under certain restrictions'''
    token = models.CharField(primary_key=True, max_length=40)
    key = models.ForeignKey(APIKeyStore)
    expires = models.DateTimeField(
        blank=True, null=True, help_text='Limit the duration of this token')
    not_valid_til = models.DateTimeField(
        blank=True, null=True,
        help_text='Prevent this token from being immediately available')
    ip = ip_field(required=False)

    def save(self, *args, **kwargs):
        if not self.token:
            self.token = str(uuid.uuid4())
        return super(APIToken, self).save(*args, **kwargs)

    def valid_request(self, request):
        if self.expires and self.expires < datetime.datetime.now():
            return False
        if self.not_valid_til and self.not_valid_til > datetime.datetime.now():
            return False
        if self.ip and get_ip(request) != self.ip:
            return False

        return True


class APILog(models.Model):
    timestamp = models.DateTimeField(auto_now_add=True)
    ip = ip_field()
    key = models.ForeignKey(APIKeyStore, blank=True, null=True)
    label = models.CharField(max_length=32)
    path = models.CharField(max_length=256)

    @staticmethod
    def mark(request, label, key=None):
        ip = get_ip(request)
        APILog.objects.create(label=label, ip=ip, path=request.path, key=key)
        if key:
            key.save()  # bump the last_used timestamp


class Download(models.Model):
    timestamp = models.DateTimeField(auto_now_add=True)
    ip = ip_field()
    name = models.CharField(max_length=256)
    link = models.BooleanField(
        help_text='Was this a real path or a link like "latest"')

    country = models.CharField(max_length=256, blank=True, null=True)
    region_isp = models.CharField(max_length=256, blank=True, null=True)

    @staticmethod
    def mark(request, artifact):
        try:
            if not settings.TRACK_DOWNLOAD_STATS:
                return

            # Don't keep track of bot downloads.
            agent = request.META.get('HTTP_USER_AGENT', '')
            for bot in settings.BOT_USER_AGENTS:
                if bot in agent:
                    return

            ip = get_ip(request)
            name = artifact.get_real_name()
            link = name != artifact.url()
            Download.objects.create(ip=ip, name=name, link=link)
        except:
            logging.exception('unable to mark download')

    @staticmethod
    def next_month(ts):
        if ts.month < 12:
            return ts.replace(month=ts.month + 1)
        return ts.replace(year=ts.year + 1, month=1)

    @staticmethod
    def month_queryset(year_month):
        start = datetime.datetime.strptime(year_month, '%Y.%m')
        end = Download.next_month(start)
        return Download.objects.filter(
            timestamp__gte=start, timestamp__lte=end).exclude(country=None)

    @staticmethod
    def report(year_month, column_name, **extra_filters):
        qs = Download.month_queryset(
            year_month
        )
        if extra_filters:
            qs = qs.filter(**extra_filters)

        return qs.values(
            column_name,
        ).annotate(
            count=Count(column_name)
        ).order_by(
            '-count'
        )