forked from theinterned/batucada
/
models.py
150 lines (122 loc) · 5.64 KB
/
models.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
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
import logging
from django.db import models
from django.template.defaultfilters import slugify
from django.db.models import Q
from django.conf import settings
from drumbeat.models import ModelBase
from drumbeat.utils import get_partition_id, safe_filename
from drumbeat import storage
from richtext.models import RichTextField
from users.models import UserProfile
from badges.models import Badge
log = logging.getLogger(__name__)
def schools_determine_image_upload_path(instance, filename):
return "images/schools/%(partition)d/%(filename)s" % {
'partition': get_partition_id(instance.pk),
'filename': safe_filename(filename),
}
class School(ModelBase):
"""Placeholder model for schools."""
name = models.CharField(max_length=100)
slug = models.SlugField(unique=True, blank=True)
short_name = models.CharField(max_length=20)
description = RichTextField(config_name='rich')
organizers = models.ManyToManyField('users.UserProfile',
null=True, blank=True)
featured = models.ManyToManyField('projects.Project',
related_name='school_featured', null=True, blank=True)
logo = models.ImageField(upload_to=schools_determine_image_upload_path, null=True,
storage=storage.ImageStorage(), blank=True)
groups_icon = models.ImageField(upload_to=schools_determine_image_upload_path,
null=True, storage=storage.ImageStorage(), blank=True)
background = models.ImageField(upload_to=schools_determine_image_upload_path,
null=True, storage=storage.ImageStorage(), blank=True)
site_logo = models.ImageField(upload_to=schools_determine_image_upload_path,
null=True, storage=storage.ImageStorage(), blank=True)
headers_color = models.CharField(max_length=7, default='#5a6579')
headers_color_light = models.CharField(max_length=7, default='#f08c00')
background_color = models.CharField(max_length=7, default='#ffffff')
menu_color = models.CharField(max_length=7, default='#36cdc4')
menu_color_light = models.CharField(max_length=7, default='#4bd2c9')
sidebar_width = models.CharField(max_length=5, default='245px')
show_school_organizers = models.BooleanField(default=True)
extra_styles = models.TextField(blank=True)
# The term names are used to import school courses from the old site.
OLD_TERM_NAME_CHOICES = YEAR_IN_SCHOOL_CHOICES = (
('Math Future', 'School of the Mathematical Future'),
('SoSI', 'School of Social Innovation'),
('Webcraft', 'School of Webcraft'),
)
old_term_name = models.CharField(max_length=15, blank=True,
null=True, choices=OLD_TERM_NAME_CHOICES)
mentor_form_url = models.URLField(blank=True, null=True)
mentee_form_url = models.URLField(blank=True, null=True)
def __unicode__(self):
return self.name
@models.permalink
def get_absolute_url(self):
return ('school_home', (), {
'slug': self.slug,
})
def save(self):
"""Make sure each school has a unique slug."""
count = 1
if not self.slug:
slug = slugify(self.name)
self.slug = slug
while True:
existing = School.objects.filter(slug=self.slug)
if len(existing) == 0:
break
self.slug = "%s-%s" % (slug, count + 1)
count += 1
super(School, self).save()
def projectsets_determine_image_upload_path(instance, filename):
return "images/projectsets/%(partition)d/%(filename)s" % {
'partition': get_partition_id(instance.pk),
'filename': safe_filename(filename),
}
class ProjectSet(ModelBase):
"Model for the project sets"
name = models.CharField(max_length=100)
slug = models.SlugField(unique=True, blank=True)
description = RichTextField(config_name='rich')
short_description = models.CharField(max_length=150)
school = models.ForeignKey(School, blank=True, null=True,
related_name="project_sets")
projects = models.ManyToManyField('projects.Project', blank=True, null=True,
related_name="projectsets")
first_project = models.ForeignKey('projects.Project', blank=True, null=True,
related_name="starting_project_sets")
featured = models.BooleanField(default=False)
image = models.ImageField(upload_to=projectsets_determine_image_upload_path,
null=True, storage=storage.ImageStorage(), blank=True)
def __unicode__(self):
return self.name
@models.permalink
def get_absolute_url(self):
return ('school_projectset', (), {
'slug': self.school.slug,
'set_slug': self.slug,
})
def get_image_url(self):
# TODO: using project's default image until a default badge
# image is added.
missing = settings.MEDIA_URL + 'images/missing-challenge-set.png'
image_path = self.image.url if self.image else missing
return image_path
def _distinct_participants(self):
return UserProfile.objects.filter(participations__project__projectsets=self,
deleted=False,
participations__left_on__isnull=True).distinct()
def total_participants(self):
return self._distinct_participants().filter(
participations__adopter = False,
participations__organizing = False).count()
def total_adopters(self):
return self._distinct_participants().filter(
Q(participations__adopter=True)
| Q(participations__organizing=True)).count()
def total_badges(self):
return Badge.objects.filter(
groups__projectsets=self).distinct().count()