コード例 #1
0
class Choice(models.Model):
    poll = models.ForeignKey(Poll,
                             on_delete=models.CASCADE,
                             related_name="choices")
    text_de = models.CharField(max_length=255, verbose_name=_("Text (German)"))
    text_en = models.CharField(max_length=255,
                               verbose_name=_("Text (English)"))
    text = translate(en='text_en', de='text_de')
    description_de = models.TextField(default="",
                                      blank=True,
                                      verbose_name=_("Description (German)"))
    description_en = models.TextField(default="",
                                      blank=True,
                                      verbose_name=_("Description (English)"))
    description = translate(en='description_en', de='description_de')
    votes = models.IntegerField(default=0)

    index = models.IntegerField(verbose_name=_("Ordering index"), default=0)

    class Meta:
        ordering = ['index']

    def __str__(self):
        return self.text_en

    def percentage(self):
        participant_count = self.poll.participants.count()
        if participant_count == 0:
            return 0
        return self.votes * 100 / participant_count
コード例 #2
0
ファイル: models.py プロジェクト: gritfessel/1327
class Document(PolymorphicModel):
	def get_hash():
		max_id = Document.objects.aggregate(models.Max('id'))['id__max'] or 0
		return '{}_{}'.format(hashlib.md5(str(datetime.now()).encode()).hexdigest(), int(max_id) + 1)

	created = models.DateTimeField(default=timezone.now)
	title_de = models.CharField(max_length=255, verbose_name=_("Title (German)"))
	title_en = models.CharField(max_length=255, verbose_name=_("Title (English)"))
	title = translate(en='title_en', de='title_de')
	url_title = models.CharField(unique=True, max_length=255, verbose_name='url_title')
	text_de = models.TextField(blank=True, verbose_name=_("Text (German)"))
	text_en = models.TextField(blank=True, verbose_name=_("Text (English)"))
	text = translate(en='text_en', de='text_de')
	hash_value = models.CharField(max_length=40, unique=True, default=get_hash, verbose_name=_("Hash value"))

	DOCUMENT_LINK_REGEX = r'\[(?P<title>[^\[]+)\]\(document:(?P<id>\d+)\)'
	VIEW_PERMISSION_NAME = DOCUMENT_VIEW_PERMISSION_NAME

	class Meta:
		verbose_name = _("Document")
		verbose_name_plural = _("Documents")

	class LinkPattern(InternalLinkPattern):
		def url(self, id):
			document = Document.objects.get(id=id)
			if document:
				return reverse(document.get_view_url_name(), args=[document.url_title])
			return ''

	def __str__(self):
		return f"{self.title_de} | {self.title_en}"

	def save(self, *args, **kwargs):
		# make sure that the url is slugified
		self.url_title = slugify(self.url_title)
		super().save(*args, **kwargs)

	def get_view_url(self):
		raise NotImplementedError()

	def get_edit_url(self):
		raise NotImplementedError()

	def get_view_url_name(self):
		return 'view'

	def get_edit_url_name(self):
		return 'edit'

	def get_attachments_url_name(self):
		return 'attachments'

	def get_permissions_url_name(self):
		return 'permissions'

	def get_versions_url_name(self):
		return 'versions'

	@classmethod
	def get_view_permission(klass):
		content_type = ContentType.objects.get_for_model(klass)
		app_label = content_type.app_label
		permission = "{}.{}".format(app_label, klass.VIEW_PERMISSION_NAME)
		return permission

	def save_formset(self, formset):
		pass

	def can_be_changed_by(self, user):
		raise NotImplementedError

	@property
	def can_be_reverted(self):
		return True

	def authors(self):
		authors = set()
		versions = Version.objects.get_for_object(self)
		for version in versions:
			authors.add(version.revision.user)
		return authors

	@classmethod
	def generate_new_title(cls):
		now = str(datetime.now())
		title_en = "New Page from {}".format(now)
		title_de = "Neue Seite vom {}".format(now)
		return title_en, title_de

	@classmethod
	def generate_default_slug(cls, title):
		slug = slugify(title)
		count = 2

		while Document.objects.filter(url_title=slug).exists():
			# check whether current title already ends with '_\d+' and handle this case accordingly
			temp_suffix_match = re.search(r'_(?P<count>\d+)$', slug)
			if temp_suffix_match:
				slug = slug[:temp_suffix_match.start()]

			slug = "{}_{}".format(slug, count)
			count += 1
		return slug

	@property
	def view_permission_name(self):
		content_type = ContentType.objects.get_for_model(self)
		return "{app}.view_{model}".format(app=content_type.app_label, model=content_type.model)

	@property
	def edit_permission_name(self):
		content_type = ContentType.objects.get_for_model(self)
		return "{app}.change_{model}".format(app=content_type.app_label, model=content_type.model)

	@property
	def add_permission_name(self):
		content_type = ContentType.objects.get_for_model(self)
		return "{app}.add_{model}".format(app=content_type.app_label, model=content_type.model)

	@property
	def delete_permission_name(self):
		content_type = ContentType.objects.get_for_model(self)
		return "{app}.delete_{model}".format(app=content_type.app_label, model=content_type.model)

	def delete_all_permissions(self, user_or_group):
		remove_perm(self.view_permission_name, user_or_group, self)
		remove_perm(self.edit_permission_name, user_or_group, self)
		remove_perm(self.delete_permission_name, user_or_group, self)

	def set_all_permissions(self, user_or_group):
		assign_perm(self.view_permission_name, user_or_group, self)
		assign_perm(self.edit_permission_name, user_or_group, self)
		assign_perm(self.delete_permission_name, user_or_group, self)

	def reset_permissions(self):
		users = get_users_with_perms(self)
		for user in users:
			self.delete_all_permissions(user)
		groups = get_groups_with_perms(self)
		for group in groups:
			self.delete_all_permissions(group)

	@property
	def meta_information_html(self):
		raise NotImplementedError('Please use a subclass of Document')

	@property
	def last_change(self):
		last_revision = Version.objects.get_for_object(self).order_by('revision__date_created').last()
		if last_revision is None:
			return None
		return last_revision.revision.date_created

	@property
	def last_author(self):
		last_revision = Version.objects.get_for_object(self).order_by('revision__date_created').last()
		if last_revision is None:
			return None
		return last_revision.revision.user

	@property
	def is_in_creation(self):
		return not self.has_perms()

	def show_permissions_editor(self):
		return True

	def show_publish_button(self):
		return False

	def has_perms(self):
		group_perms = get_groups_with_perms(self, attach_perms=True)
		content_type = ContentType.objects.get_for_model(self)
		for perms in group_perms.values():
			for perm in perms:
				perm = "{app}.{perm}".format(app=content_type.app_label, perm=perm)
				if perm != self.add_permission_name:
					return True
		return False

	def handle_edit(self, cleaned_data):
		pass
コード例 #3
0
 class DummyClass():
     title_de = "deutsch"
     title_en = "english"
     title = translate(en='title_en', de='title_de')
コード例 #4
0
ファイル: models.py プロジェクト: gritfessel/1327
class MenuItem(models.Model):
	MAIN_MENU = 1
	FOOTER = 2
	MENU_TYPES = (
		(MAIN_MENU, _("Main Menu")),
		(FOOTER, _("Footer")),
	)
	title_de = models.CharField(max_length=255, verbose_name=_("Title (German)"))
	title_en = models.CharField(max_length=255, verbose_name=_("Title (English)"))
	title = translate(en='title_en', de='title_de')
	order = models.IntegerField(default=999)

	link = models.CharField(max_length=255, blank=True, null=True, verbose_name=_("Link"))

	document = models.ForeignKey(Document, blank=True, null=True, on_delete=models.PROTECT, verbose_name=_("Document"), related_name='menu_items')

	parent = models.ForeignKey('self', blank=True, null=True, on_delete=models.CASCADE, related_name='children')

	menu_type = models.IntegerField(choices=MENU_TYPES, default=MAIN_MENU)

	VIEW_PERMISSION_NAME = MENUITEM_VIEW_PERMISSION_NAME
	EDIT_PERMISSION_NAME = MENUITEM_EDIT_PERMISSION_NAME
	CHANGE_CHILDREN_PERMISSION_NAME = MENUITEM_CHANGE_CHILDREN_PERMISSION_NAME

	class Meta:
		ordering = ['order']
		permissions = (
			(MENUITEM_CHANGE_CHILDREN_PERMISSION_NAME, 'User/Group is allowed to change children items'),
		)

	def __str__(self):
		return self.title

	def get_url(self):
		if self.link:
			split = self.link.split("?")
			if len(split) == 1:
				return reverse(split[0])
			else:
				key, value = split[1].split('=')
				return reverse(split[0], kwargs={key: value})
		elif self.document:
			return self.document.get_view_url()
		else:
			return "#"

	@property
	def view_permission_name(self):
		content_type = ContentType.objects.get_for_model(self)
		return "{app}.view_{model}".format(app=content_type.app_label, model=content_type.model)

	@property
	def change_children_permission_name(self):
		content_type = ContentType.objects.get_for_model(self)
		return "{app}.changechildren_{model}".format(app=content_type.app_label, model=content_type.model)

	@property
	def edit_permission_name(self):
		content_type = ContentType.objects.get_for_model(self)
		return "{app}.change_{model}".format(app=content_type.app_label, model=content_type.model)

	def can_view(self, user):
		permission_name = self.view_permission_name
		return user.has_perm(permission_name, self) or user.has_perm(permission_name)

	def can_view_in_list(self, user):
		return self.can_edit(user) or user.has_perm(self.change_children_permission_name, self)

	def can_edit(self, user):
		change_children_permission_name = self.change_children_permission_name
		edit_permission_name = self.edit_permission_name
		if user.has_perm(change_children_permission_name, self.parent) or user.has_perm(change_children_permission_name):
			return True
		elif user.has_perm(edit_permission_name, self) or user.has_perm(edit_permission_name):
			return True
		elif self.parent and self.parent.parent:
			return user.has_perm(change_children_permission_name, self.parent.parent)
		return False

	def can_delete(self, user):
		return self.can_edit(user)

	def set_all_permissions(self, user_or_group):
		assign_perm(self.view_permission_name, user_or_group, self)
		assign_perm(self.change_children_permission_name, user_or_group, self)

	@classmethod
	def used_permissions(cls):
		app_label = ContentType.objects.get_for_model(cls).app_label
		return (
			NamedPermission(name="{app}.{codename}".format(app=app_label, codename=cls.VIEW_PERMISSION_NAME), description=_('view')),
			NamedPermission(name="{app}.{codename}".format(app=app_label, codename=cls.CHANGE_CHILDREN_PERMISSION_NAME), description=_('change children')),
			NamedPermission(name="{app}.{codename}".format(app=app_label, codename=cls.EDIT_PERMISSION_NAME), description=_('edit')),
		)