summaryrefslogtreecommitdiff
path: root/main/models.py
blob: 525a4cebb51002b5b3153d45580674a9a0ae1dd5 (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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
from django.db import models
from django.contrib.auth.models import User
import re

###########################
### Model help classes  ###
###########################
class Container(dict):
    def __init__(self, dict_entries=None, **entries):
        if dict_entries:
            self.update(dict_entries)
        if entries:
            self.update(entries)

    def __iter__(self):
        rev_items = [(v, k) for k, v in self.items()]
        rev_items.sort()
        items = [k for v, k in rev_items]
        return items.__iter__()

    def itertransp(self):
        rev_items = [(v, k) for k, v in self.items()]
        rev_items.sort()
        return rev_items.__iter__()

    def __getattr__(self,name):
        return self[name]

    def __setattr__(self,name,val):
        self[name] = val

    
###########################
### User Profile Class ####
###########################
class UserProfile(models.Model):
    id = models.AutoField(primary_key=True) # not technically needed
    notify = models.BooleanField("Send notifications", default=True, help_text="When enabled, user will recieve 'flag out of date' notifications")
    alias = models.CharField(core=True, maxlength=50, help_text="Required field")
    public_email = models.CharField(core=True, maxlength=50, help_text="Required field")
    other_contact = models.CharField(maxlength=100, null=True, blank=True)
    website = models.URLField(null=True, blank=True)
    yob = models.IntegerField(null=True, blank=True)
    location = models.CharField(maxlength=50, null=True, blank=True)
    languages = models.CharField(maxlength=50, null=True, blank=True)
    interests = models.CharField(maxlength=255, null=True, blank=True)
    occupation = models.CharField(maxlength=50, null=True, blank=True)
    roles = models.CharField(maxlength=255, null=True, blank=True)
    favorite_distros = models.CharField(maxlength=255, null=True, blank=True)
    picture = models.FileField(upload_to='devs', default='devs/silhouette.png')
    user = models.ForeignKey(User, related_name='userprofile_user',  edit_inline=models.STACKED, num_in_admin=1, min_num_in_admin=1, max_num_in_admin=1, num_extra_on_change=0, unique=True)
    class Meta:
        db_table = 'user_profiles'
        verbose_name = 'Additional Profile Data'
        verbose_name_plural = 'Additional Profile Data'


#######################
### Manager Classes ###
#######################
class TodolistManager(models.Manager):
    def get_incomplete(self):
        results = []
        for l in self.all().order_by('-date_added'):
            if TodolistPkg.objects.filter(list=l.id).filter(complete=False).count() > 0:
                results.append(l)
        return results

class PackageManager(models.Manager):
    def get_flag_stats(self):
        results = []
        # first the orphans
        noflag = self.filter(maintainer=0).count()
        flagged = self.filter(maintainer=0).filter(needupdate=True).count()
        flagnotest = self.filter(maintainer=0).filter(
                        needupdate=True).exclude(
                            repo=Package.REPOS['testing']).count()
        results.append(
            (User(id=0,first_name='Orphans'), noflag, flagged, flagnotest))
        # now the rest
        for maint in User.objects.all().order_by('first_name'):
            noflag = self.filter(maintainer=maint.id).count()
            flagged = self.filter(maintainer=maint.id).filter(
                    needupdate=True).count()
            flagnotest = self.filter(maintainer=maint.id).filter(
                    needupdate=True).exclude(
                        repo=Package.REPOS['testing']).count()
            results.append((maint, noflag, flagged, flagnotest))
        return results


#############################
### General Model Classes ###
#############################
class Mirror(models.Model):
    id = models.AutoField(primary_key=True)
    domain = models.CharField(maxlength=255)
    country = models.CharField(maxlength=255)
    url = models.CharField(maxlength=255)
    protocol_list = models.CharField(maxlength=255, null=True, blank=True)
    admin_email = models.CharField(maxlength=255, null=True, blank=True)
    def __str__(self):
        return self.domain
    class Meta:
        db_table = 'mirrors'
    class Admin:
        list_display = ('domain', 'country')
        list_filter = ('country',)
        ordering = ['domain']
        search_fields = ('domain')
        pass

class Press(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(maxlength=255)
    url = models.CharField(maxlength=255)
    def __str__(self):
        return self.name
    class Meta:
        db_table = 'press'
        verbose_name_plural = 'press'
    class Admin:
        ordering = ['name']
        search_fields = ('name')
        pass

class AltForum(models.Model):
    id = models.AutoField(primary_key=True)
    language = models.CharField(maxlength=255)
    url = models.CharField(maxlength=255)
    name = models.CharField(maxlength=255)
    def __str__(self):
        return self.name
    class Meta:
        db_table = 'alt_forums'
        verbose_name = 'AltForum'
    class Admin:
        list_display = ('language', 'name')
        list_filter = ('language',)
        ordering = ['name']
        search_fields = ('name')
        pass

class Donor(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(maxlength=255)
    def __str__(self):
        return self.name
    class Meta:
        db_table = 'donors'
    class Admin:
        ordering = ['name']
        search_fields = ('name')
        pass

class News(models.Model):
    id = models.AutoField(primary_key=True)
    author = models.ForeignKey(User, related_name='news_author')
    postdate = models.DateField(auto_now_add=True)
    title = models.CharField(maxlength=255)
    content = models.TextField()
    class Meta:
        db_table = 'news'
        verbose_name_plural = 'news'
        get_latest_by = 'postdate'
        ordering = ['-postdate', '-id']

    def get_absolute_url(self):
        return '/news/%i/' % self.id

class Package(models.Model):
    ## note: purposefully inlining arch and repo.
    ## they don't change very often (rarely), and it should help compact
    ## the general model
    # architectures
    ARCHES = Container(i686=1, x86_64=2)
    # repositories
    REPOS = Container(core=1, extra=2, testing=3, unstable=4)

    id = models.AutoField(primary_key=True)
    ## note: the arch and repo elements might need to be indexed. not sure.
    repo = models.IntegerField(choices=REPOS.itertransp())
    arch = models.IntegerField(choices=ARCHES.itertransp())
    maintainer = models.ForeignKey(User, related_name='package_maintainer')
    needupdate = models.BooleanField(default=False)
    pkgname = models.CharField(maxlength=255)
    pkgver = models.CharField(maxlength=255)
    pkgrel = models.CharField(maxlength=255)
    pkgdesc = models.CharField(maxlength=255)
    url = models.URLField()
    last_update = models.DateTimeField(null=True, blank=True)
    objects = PackageManager()
    class Meta:
        db_table = 'packages'
        get_latest_by = 'last_update'

    def get_absolute_url(self):
        return '/packages/%i/' % self.id

    def required_by_urlize(self):
        urls = []
        requiredby = PackageDepend.objects.filter(
            depname=self.pkgname).order_by('depname')
        for req in requiredby:
            urls.append(
                '<li><a href="/packages/%d/">%s</a></li>' % \
                (req.pkg.id,req.pkg.pkgname))
        return ''.join(urls)

    def depends_urlize(self):
        urls = []
        for dep in self.packagedepend_set.order_by('depname'):
            try:
                # we only need depend on same-arch-packages
                p = Package.objects.get(
                    pkgname=dep.depname,
                    arch=self.arch)
            except Package.DoesNotExist, IndexError:
                # couldn't find a package in the DB
                # it might be a virtual depend
                urls.append('<li>%s</li>' % dep.depname)
                continue
            urls.append(
                '<li><a href="/packages/%d/">%s</a>%s</li>' % \
                (p.id,dep.depname,dep.depvcmp))
        return ''.join(urls)

class PackageFile(models.Model):
    id = models.AutoField(primary_key=True)
    pkg = models.ForeignKey('Package')
    path = models.CharField(maxlength=255)
    class Meta:
        db_table = 'package_files'

class PackageDepend(models.Model):
    id = models.AutoField(primary_key=True)
    pkg = models.ForeignKey('Package')
    depname = models.CharField(db_index=True, maxlength=255)
    depvcmp = models.CharField(maxlength=255)
    class Meta:
        db_table = 'package_depends'

class Todolist(models.Model):
    id = models.AutoField(primary_key=True)
    creator = models.ForeignKey(User, related_name='todolist_creator')
    name = models.CharField(maxlength=255)
    description = models.TextField()
    date_added = models.DateField(auto_now_add=True)
    objects = TodolistManager()
    class Meta:
        db_table = 'todolists'

class TodolistPkg(models.Model):
    id = models.AutoField(primary_key=True)
    list = models.ForeignKey('Todolist')
    pkg = models.ForeignKey('Package')
    complete = models.BooleanField(default=False)
    class Meta:
        db_table = 'todolists_pkgs'
        unique_together = (('list','pkg'),)

class Wikipage(models.Model):
    """Wiki page storage"""
    title = models.CharField(maxlength=255)
    content = models.TextField()
    last_author = models.ForeignKey(User, related_name='wikipage_last_author')
    class Meta:
        db_table = 'wikipages'

    def editurl(self):
        return "/wiki/edit/" + self.title + "/"

    def __repr__(self):
        return self.title

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