def group_aware_url_name(view_name, group_or_group_slug, portal_id=None): """ Modifies a URL name that points to a URL within a CosinnusGroup so that the URL points to the correct sub-url of the type of the CosinnusGroup Model for the given group slug. @return: A modified URL view name """ if not group_or_group_slug: return '' if not isinstance(group_or_group_slug, six.string_types) and \ (type(group_or_group_slug) is get_cosinnus_group_model() or issubclass(group_or_group_slug.__class__, get_cosinnus_group_model())): group_type = group_or_group_slug.type else: # retrieve group type cached group_type = cache.get( CosinnusGroupManager._GROUP_SLUG_TYPE_CACHE_KEY % (CosinnusPortal.get_current().id, group_or_group_slug)) if group_type is None: group_type = get_cosinnus_group_model().objects.get( slug=group_or_group_slug, portal_id=portal_id).type cache.set(CosinnusGroupManager._GROUP_SLUG_TYPE_CACHE_KEY % (CosinnusPortal.get_current().id, group_or_group_slug), group_type, 31536000) # 1 year cache # retrieve that type's prefix and add to URL viewname prefix = group_model_registry.get_url_name_prefix_by_type(group_type, 0) if ":" in view_name: view_name = (":%s" % prefix).join(view_name.rsplit(":", 1)) else: view_name = prefix + view_name return view_name
class CosinnusProject(get_cosinnus_group_model()): class Meta(object): """ For some reason, the Meta isn't inherited automatically from CosinnusGroup here """ proxy = True app_label = 'cosinnus' ordering = ('name', ) verbose_name = _('Cosinnus project') verbose_name_plural = _('Cosinnus projects') GROUP_MODEL_TYPE = CosinnusGroup.TYPE_PROJECT MEMBERSHIP_MODE_CHOICES = [ CosinnusGroup.MEMBERSHIP_MODE_CHOICES[mode] for mode in settings.COSINNUS_GROUP_MEMBERSHIP_MODE_CHOICES[GROUP_MODEL_TYPE] ] objects = CosinnusProjectManager() def save(self, allow_type_change=False, *args, **kwargs): if not allow_type_change: self.type = CosinnusGroup.TYPE_PROJECT super(CosinnusProject, self).save(*args, **kwargs) def __str__(self): # FIXME: better caching for .portal.name return '%s (%s)' % (self.name, self.portal.name) @classmethod def get_trans(cls): """ Added this for IDEs and parsers to not mark the unknown `get_trans()` property everywhere the subtypes are called using this directly. """ return super(CosinnusProject, cls).get_trans()
def ensure_group_type(group): """ If the given group is a CosinnusGroup model instance, returns it as either a CosinnusProject or CosinnusSociety, depending on group type """ if group.__class__ == get_cosinnus_group_model(): klass = CosinnusProject if group.type == group.TYPE_PROJECT else CosinnusSociety group = klass.objects.get_cached(pks=group.id) return group
def is_member_in_forum(user): """ Template filter to check if a user is in the default forum group. """ forum_slug = getattr(settings, 'NEWW_FORUM_GROUP_SLUG', None) if forum_slug: forum_group = get_object_or_None(get_cosinnus_group_model(), slug=forum_slug, portal=CosinnusPortal.get_current()) if forum_group: return is_group_member(user, forum_group) return False
def ensure_group_type(group): """ If the given group is a CosinnusGroup model instance, returns it as either a CosinnusProject or CosinnusSociety, depending on group type """ if group.__class__ == get_cosinnus_group_model(): klass_map = { group.TYPE_PROJECT: CosinnusProject, group.TYPE_SOCIETY: CosinnusSociety, group.TYPE_CONFERENCE: CosinnusConference, } klass = klass_map.get(group.type) group = get_object_or_None(klass, id=group.id) return group
class CosinnusSociety(get_cosinnus_group_model()): class Meta(object): """ For some reason, the Meta isn't inherited automatically from CosinnusGroup here """ proxy = True app_label = 'cosinnus' ordering = ('name', ) verbose_name = _('Cosinnus group') verbose_name_plural = _('Cosinnus groups') GROUP_MODEL_TYPE = CosinnusGroup.TYPE_SOCIETY objects = CosinnusSocietyManager() def save(self, allow_type_change=False, *args, **kwargs): if not allow_type_change: self.type = CosinnusGroup.TYPE_SOCIETY super(CosinnusSociety, self).save(*args, **kwargs) def __str__(self): # FIXME: better caching for .portal.name return '%s (%s)' % (self.name, self.portal.name)
def render(self, context): if not hasattr(self, 'base_view_name'): self.base_view_name = copy(self.view_name) else: self.view_name = copy(self.base_view_name) view_name = self.view_name.resolve(context) ignoreErrors = 'ignoreErrors' in self.kwargs and self.kwargs.pop( 'ignoreErrors').resolve(context) or False group_arg = self.kwargs["group"].resolve(context) group_slug = "" foreign_portal = None portal_id = getattr(self, '_portal_id', None) force_local_domain = getattr(self, '_force_local_domain', False) try: # the portal id if given to the tag can override the group's portal self._portal_id = self.kwargs["portal_id"].resolve(context) portal_id = self._portal_id del self.kwargs["portal_id"] except KeyError: pass try: # this will retain the local domain. useful for avoiding POSTs to cross-portal domains and CSRF-failing self._force_local_domain = bool( self.kwargs["force_local_domain"].resolve(context)) force_local_domain = self._force_local_domain del self.kwargs["force_local_domain"] except KeyError: pass patched_group_slug_arg = None # we accept a group object or a group slug if issubclass(group_arg.__class__, get_cosinnus_group_model()): # determine the portal from the group group_slug = group_arg.slug # if not explicitly given, learn the portal id from the group if not portal_id: portal_id = group_arg.portal_id if not portal_id == CosinnusPortal.get_current().id: foreign_portal = group_arg.portal # we patch the variable given to the tag here, to restore the regular slug-passed-url-resolver functionality patched_group_slug_arg = deepcopy(self.kwargs['group']) patched_group_slug_arg.token += '.slug' patched_group_slug_arg.var.var += '.slug' patched_group_slug_arg.var.lookups = list( self.kwargs['group'].var.lookups) + ['slug'] elif not isinstance(group_arg, six.string_types): raise TemplateSyntaxError( "'group_url' tag requires a group kwarg that is a group or a slug! Have you passed one? (You passed: 'group=%s')" % group_arg) else: group_slug = group_arg # make sure we have the foreign portal. we might not have yet retrieved it if we had a portal id explicitly set if portal_id and not portal_id == CosinnusPortal.get_current( ).id and not foreign_portal: foreign_portal = CosinnusPortal.objects.get(id=portal_id) try: try: view_name = group_aware_url_name(view_name, group_slug, portal_id) except CosinnusGroup.DoesNotExist: # ignore errors if the group doesn't exist if it is inactive (return empty link) if ignoreErrors or (not group_arg.is_active): return '' logger.error( u'Cosinnus__group_url_tag: Could not find group for: group_arg: %s, view_name: %s, group_slug: %s, portal_id: %s' % (str(group_arg), view_name, group_slug, portal_id)) raise self.view_name.var = view_name self.view_name.token = "'%s'" % view_name # to retain django core code for rendering, we patch this node to look like a proper url node, # with a slug argument. # and then restore it later, so that the node object can be reused for other group arguments # if we didn't do that, this group node's group argument would have been replaced already, and # lost to other elements that use the group_url tag in a for-loop, for example # (we cannot store anything on the object itself, down that road madness lies) if patched_group_slug_arg: self.kwargs[ 'group'], patched_group_slug_arg = patched_group_slug_arg, self.kwargs[ 'group'] ret_url = super(GroupURLNode, self).render(context) # swap back the patched arg for the original if patched_group_slug_arg: self.kwargs['group'] = patched_group_slug_arg if foreign_portal and not force_local_domain: domain = get_domain_for_portal(foreign_portal) # attach to either output or given "as" variable if self.asvar: context[self.asvar] = domain + context[self.asvar] else: ret_url = domain + ret_url return ret_url except: if ignoreErrors: return '' else: raise
app_label = 'cosinnus' ordering = ('name', ) verbose_name = _('Cosinnus group') verbose_name_plural = _('Cosinnus groups') GROUP_MODEL_TYPE = CosinnusGroup.TYPE_SOCIETY objects = CosinnusSocietyManager() def save(self, allow_type_change=False, *args, **kwargs): if not allow_type_change: self.type = CosinnusGroup.TYPE_SOCIETY super(CosinnusSociety, self).save(*args, **kwargs) def __str__(self): # FIXME: better caching for .portal.name return '%s (%s)' % (self.name, self.portal.name) CosinnusGroup = get_cosinnus_group_model() def ensure_group_type(group): """ If the given group is a CosinnusGroup model instance, returns it as either a CosinnusProject or CosinnusSociety, depending on group type """ if group.__class__ == get_cosinnus_group_model(): klass = CosinnusProject if group.type == group.TYPE_PROJECT else CosinnusSociety group = klass.objects.get_cached(pks=group.id) return group