Skip to content

Instantly share code, notes, and snippets.

@ruchej
Last active February 3, 2022 17:01
Show Gist options
  • Save ruchej/6f7c1c1330ca1a090a08d98b7709cf75 to your computer and use it in GitHub Desktop.
Save ruchej/6f7c1c1330ca1a090a08d98b7709cf75 to your computer and use it in GitHub Desktop.
class MenuManager(models.QuerySet):
"""docstring for MenuManager"""
def get_menu(self, attr):
return self.filter(title=attr, parent_id__isnull=True).first()
def get_submenu(self, user):
submenu = self.filter(Q(seen_seeker=True))
def get_supermenu(self, request):
return self.filter(parent_id__isnull=True)
def __getattr__(self, attr):
attr, splitter, menu = attr.partition('get_menu_')
if splitter:
return self.get_menu(menu)
return super().__getattr__(attr)
class Menu(Core):
class Meta:
ordering = ('parent_id', 'sort', 'title')
verbose_name = _('Элемент меню')
verbose_name_plural = _('Меню сайта')
url = models.CharField(_('Линк'), max_length=256, blank=False, null=False, default='index')
css_class = models.CharField(_('CSS-Класс блока меню'), max_length=30, null=False, blank=True, default='')
seen_gasts = models.BooleanField(_('Элемент меню виден незарегистрированным пользователям'), default=True)
seen_seeker = models.BooleanField(_('Элемент меню виден соискателям'), default=False)
seen_employ = models.BooleanField(_('Элемент меню виден работодателям'), default=False)
parent = models.ForeignKey('self', verbose_name=_('Суперкласс Меню'), null=True, blank=True,
related_name='submenus', on_delete=models.CASCADE)
objects = MenuManager.as_manager()
def __str__(self):
self.title = self.title or self.css_class
return super().__str__()
def get_title(self):
return super().__str__()
def set_css_active(self):
self.css_class = f'active {self.css_class}'
return self
def get_url(self):
url = self.url
return reverse(url) if ':' in url else url
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment