class Questionnaire(RandomIDModel): filename = models.CharField(max_length=100) title = models.CharField(max_length=500) id_string = models.CharField(max_length=50) xls_form = S3FileField(upload_to='xls-forms') xml_form = S3FileField(upload_to='xml-forms', default=False) original_file = models.CharField(max_length=200, null=True) project = models.ForeignKey('organization.Project', related_name='questionnaires') version = models.BigIntegerField(default=1) md5_hash = models.CharField(max_length=50, default=False) objects = managers.QuestionnaireManager() history = HistoricalRecords() class Meta: unique_together = ('project', 'id_string', 'version') class TutelaryMeta: perm_type = 'questionnaire' path_fields = ('project', 'pk') actions = ( ('questionnaire.view', {'description': _("View the questionnaire of the project"), 'error_message': messages.QUESTIONNAIRE_VIEW, 'permissions_object': 'project'}), ('questionnaire.add', {'description': _("Add a questionnaire to the project"), 'error_message': messages.QUESTIONNAIRE_ADD, 'permissions_object': 'project'}), ('questionnaire.edit', {'description': _("Edit an existing questionnaire"), 'error_message': messages.QUESTIONNAIRE_EDIT, 'permissions_object': 'project'}), )
class Questionnaire(RandomIDModel): filename = models.CharField(max_length=100) title = models.CharField(max_length=500) id_string = models.CharField(max_length=50) default_language = models.CharField(max_length=3, null=True) xls_form = S3FileField(upload_to='xls-forms') xml_form = S3FileField(upload_to='xml-forms', default=False) original_file = models.CharField(max_length=200, null=True) project = models.ForeignKey('organization.Project', related_name='questionnaires') version = models.BigIntegerField() md5_hash = models.CharField(max_length=50, default=False) objects = managers.QuestionnaireManager() history = HistoricalRecords() class Meta: unique_together = ('project', 'id_string', 'version') class TutelaryMeta: perm_type = 'questionnaire' path_fields = ('project', 'pk') actions = ( ('questionnaire.view', { 'description': _("View the questionnaire of the project"), 'error_message': messages.QUESTIONNAIRE_VIEW, 'permissions_object': 'project' }), ('questionnaire.add', { 'description': _("Add a questionnaire to the project"), 'error_message': messages.QUESTIONNAIRE_ADD, 'permissions_object': 'project' }), ('questionnaire.edit', { 'description': _("Edit an existing questionnaire"), 'error_message': messages.QUESTIONNAIRE_EDIT, 'permissions_object': 'project' }), ) def __repr__(self): repr_string = ('<Questionnaire id={obj.id} title={obj.title}' ' project={obj.project.slug}>') return repr_string.format(obj=self) def save(self, *args, **kwargs): if not self.id and not self.version: self.version = int( datetime.utcnow().strftime('%Y%m%d%H%M%S%f')[:-4]) if not self.id and not self.md5_hash: string = (str(self.filename) + str(self.id_string) + str(self.version)) self.md5_hash = hashlib.md5(string.encode()).hexdigest() return super().save(*args, **kwargs)
class User(auth_base.AbstractBaseUser, auth.PermissionsMixin): username = abstract_user_field('username') full_name = models.CharField(_('full name'), max_length=130, blank=True) email = models.EmailField( _('email address'), blank=True, null=True, default=None, unique=True ) phone = models.CharField( _('phone number'), max_length=16, null=True, blank=True, default=None, unique=True ) is_staff = abstract_user_field('is_staff') is_active = abstract_user_field('is_active') date_joined = abstract_user_field('date_joined') email_verified = models.BooleanField(default=False) phone_verified = models.BooleanField(default=False) change_pw = models.BooleanField(default=True) language = models.CharField(max_length=10, choices=settings.LANGUAGES, default=settings.LANGUAGE_CODE) measurement = models.CharField(max_length=20, choices=settings.MEASUREMENTS, default=settings.MEASUREMENT_DEFAULT) avatar = S3FileField(upload_to='avatars', accepted_types=settings.ACCEPTED_AVATAR_TYPES, blank=True) objects = UserManager() history = HistoricalRecords() USERNAME_FIELD = 'username' REQUIRED_FIELDS = ['email', 'full_name'] class Meta: ordering = ('username',) verbose_name = _('user') verbose_name_plural = _('users') objects = UserManager() class TutelaryMeta: perm_type = 'user' path_fields = ('username',) actions = [('user.list', {'permissions_object': None, 'error_message': _("You don't have permission to view user details")}), ('user.update', {'error_message': _("You don't have permission to update user details")})] def __repr__(self): repr_string = ('<User username={obj.username}' ' full_name={obj.full_name}' ' email={obj.email}' ' email_verified={obj.email_verified}' ' phone={obj.phone}' ' phone_verified={obj.phone_verified}>') return repr_string.format(obj=self) def get_display_name(self): """ Returns the display name. If full name is present then return full name as display name else return username. """ if self.full_name != '': return self.full_name else: return self.username @property def avatar_url(self): return self.avatar.url or settings.DEFAULT_AVATAR
class Resource(RandomIDModel): name = models.CharField(max_length=200) description = models.TextField(null=True, blank=True) file = S3FileField(upload_to='resources', accepted_types=ACCEPTED_TYPES) original_file = models.CharField(max_length=200) file_versions = JSONField(null=True, blank=True) mime_type = models.CharField(max_length=100, validators=[validate_file_type]) archived = models.BooleanField(default=False) last_updated = models.DateTimeField(auto_now=True) contributor = models.ForeignKey('accounts.User') project = models.ForeignKey('organization.Project') objects = ResourceManager() history = HistoricalRecords() class Meta: ordering = ('name',) class TutelaryMeta: perm_type = 'resource' path_fields = ('project', 'pk') actions = ( ('resource.list', {'description': _("List resources"), 'permissions_object': 'project', 'error_message': messages.RESOURCE_LIST}), ('resource.add', {'description': _("Add resources"), 'permissions_object': 'project', 'error_message': messages.RESOURCE_ADD}), ('resource.view', {'description': _("View resource"), 'error_message': messages.RESOURCE_VIEW}), ('resource.edit', {'description': _("Edit resource"), 'error_message': messages.RESOURCE_EDIT}), ('resource.archive', {'description': _("Archive resource"), 'error_message': messages.RESOURCE_ARCHIVE}), ('resource.unarchive', {'description': _("Unarchive resource"), 'error_message': messages.RESOURCE_UNARCHIVE}), ) def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self._original_url = self.file.url def __repr__(self): repr_string = ('<Resource id={obj.id} name={obj.name}' ' file={obj.file.url} project={obj.project.slug}>') return repr_string.format(obj=self) @property def file_name(self): if not hasattr(self, '_file_name'): self._file_name = self.file.url.split('/')[-1] return self._file_name @property def file_type(self): return self.file_name.split('.')[-1] @property def thumbnail(self): if not hasattr(self, '_thumbnail'): icon = settings.ICON_LOOKUPS.get(self.mime_type, None) if 'image' in self.mime_type and 'tif' not in self.mime_type: ext = self.file_name.split('.')[-1] base_url = self.file.url[:self.file.url.rfind('.')] self._thumbnail = base_url + '-128x128.' + ext elif icon: self._thumbnail = settings.ICON_URL.format(icon) else: self._thumbnail = '' return self._thumbnail @property def num_entities(self): if not hasattr(self, '_num_entities'): self._num_entities = ContentObject.objects.filter( resource=self).count() return self._num_entities def save(self, *args, **kwargs): create_thumbnails(self, (not self.id)) super().save(*args, **kwargs) @property def ui_class_name(self): return _("Resource") def get_absolute_url(self): return iri_to_uri(reverse( 'resources:project_detail', kwargs={ 'organization': self.project.organization.slug, 'project': self.project.slug, 'resource': self.id, }, ))
class User(auth_base.AbstractBaseUser, auth.PermissionsMixin): username = abstract_user_field('username') full_name = models.CharField(_('full name'), max_length=130, blank=True) email = abstract_user_field('email') is_staff = abstract_user_field('is_staff') is_active = abstract_user_field('is_active') date_joined = abstract_user_field('date_joined') email_verified = models.BooleanField(default=False) verify_email_by = models.DateTimeField(default=now_plus_48_hours) change_pw = models.BooleanField(default=True) language = models.CharField(max_length=10, choices=settings.LANGUAGES, default=settings.LANGUAGE_CODE) measurement = models.CharField(max_length=20, choices=settings.MEASUREMENTS, default=settings.MEASUREMENT_DEFAULT) avatar = S3FileField(upload_to='avatars', accepted_types=settings.ACCEPTED_AVATAR_TYPES, blank=True) objects = UserManager() # Audit history created_date = models.DateTimeField(auto_now_add=True) last_updated = models.DateTimeField(auto_now=True) history = HistoricalRecords() _dict_languages = dict(settings.LANGUAGES) _dict_measurements = dict(settings.MEASUREMENTS) USERNAME_FIELD = 'username' REQUIRED_FIELDS = ['email', 'full_name'] class Meta: ordering = ('username', ) verbose_name = _('user') verbose_name_plural = _('users') objects = UserManager() class TutelaryMeta: perm_type = 'user' path_fields = ('username', ) actions = [('user.list', { 'permissions_object': None, 'error_message': _("You don't have permission to view user details") }), ('user.update', { 'error_message': _("You don't have permission to update user details") })] def __repr__(self): repr_string = ('<User username={obj.username}' ' full_name={obj.full_name}' ' email={obj.email}' ' email_verified={obj.email_verified}' ' verify_email_by={obj.verify_email_by}>') return repr_string.format(obj=self) def get_display_name(self): """ Returns the display name. If full name is present then return full name as display name else return username. """ if self.full_name != '': return self.full_name else: return self.username @property def avatar_url(self): if not self.avatar or not self.avatar.url: return settings.DEFAULT_AVATAR return self.avatar.url @property def language_verbose(self): language_code = self.language.split('-')[0] return self._dict_languages[language_code] @property def measurement_verbose(self): return self._dict_measurements[self.measurement]