def __init__(self, **kwargs): super(CreateNewsBlogArticleForm, self).__init__(**kwargs) # If there's only 1 (or zero) app_configs, don't bother show the # app_config choice field, we'll choose the option for the user. get_published_app_configs() userprofiles = self.user.userprofile_set.all() if self.user.is_superuser: userprofiles = UserProfile.objects.all() app_config_choices = [] for profile in userprofiles: app_config_choices.append( (profile.app_config.pk, profile.app_config.get_app_title())) self.fields['app_config'] = forms.ChoiceField( label=_('Section'), required=True, choices=app_config_choices)
def __init__(self, **kwargs): super(CreateNewsBlogArticleForm, self).__init__(**kwargs) # If there's only 1 (or zero) app_configs, don't bother show the # app_config choice field, we'll choose the option for the user. get_published_app_configs() gsoc_year = GsocYear.objects.first() student_role = {i[1]: i[0] for i in UserProfile.ROLES}['Student'] userprofiles = self.user.userprofile_set.filter(gsoc_year=gsoc_year, role=student_role) if self.user.is_superuser: userprofiles = UserProfile.objects.all() app_config_choices = [] for profile in userprofiles: app_config_choices.append( (profile.app_config.pk, profile.app_config.get_app_title())) self.fields["app_config"] = forms.ChoiceField( label=_("Section"), required=True, choices=app_config_choices)
def user_has_add_permission(self, user, **kwargs): """ Return True if the current user has permission to add an article. :param user: The current user :param kwargs: Ignored here :return: True if user has add permission, else False """ # No one can create an Article, if there is no app_config yet. num_configs = get_published_app_configs() if not num_configs: return False # Ensure user has permission to create articles. if user.is_superuser or user.student_profile() is not None: return True # By default, no permission. return False