Example #1
0
def tournament_preference_form_builder(instance, preferences=[], **kwargs):
    if kwargs.get('section') in [str(s) for s in global_preferences_registry.sections()]:
        # Check for global preferences
        return preference_form_builder(GlobalPreferenceForm, preferences, **kwargs)

    return preference_form_builder(
        TournamentPreferenceForm, preferences, model={'instance': instance}, **kwargs)
Example #2
0
 def user_get_form_class(self, *args, **kwargs):
     section = self.kwargs.get("section", None)
     return preference_form_builder(
         UserPreferencesForm,
         instance=self.request.user,
         section=section,
         **kwargs,
     )
Example #3
0
 def get_form_class(self, *args, **kwargs):
     print('section', self.section)
     if 'site' in self.kwargs:
         self.site = get_object_or_404(Site, pk=self.kwargs['site'])
     else:
         self.site = self.request.site
     if hasattr(self.site, 'pk'):
         return preference_form_builder(SitePreferenceForm,
                                        instance=self.site,
                                        section=self.section.name)
     return SitePreferenceForm
Example #4
0
def tournament_preference_form_builder(instance, preferences=[], **kwargs):
    return preference_form_builder(TournamentPreferenceForm,
                                   preferences,
                                   model={'instance': instance},
                                   **kwargs)
Example #5
0
 def get_preference_form(self):
     form_class = preference_form_builder(EventTypePreferenceForm,
                                          instance=self.object)
     return form_class(self.request.POST or None)
Example #6
0
def preferences(
    request: HttpRequest,
    registry_name: str = "person",
    pk: Optional[int] = None,
    section: Optional[str] = None,
) -> HttpResponse:
    """View for changing preferences."""
    context = {}

    # Decide which registry to use and check preferences
    if registry_name == "site":
        registry = site_preferences_registry
        instance = request.site
        form_class = SitePreferenceForm

        if not request.user.has_perm("core.change_site_preferences_rule", instance):
            raise PermissionDenied()
    elif registry_name == "person":
        registry = person_preferences_registry
        instance = objectgetter_optional(Person, "request.user.person", True)(request, pk)
        form_class = PersonPreferenceForm

        if not request.user.has_perm("core.change_person_preferences_rule", instance):
            raise PermissionDenied()
    elif registry_name == "group":
        registry = group_preferences_registry
        instance = objectgetter_optional(Group, None, False)(request, pk)
        form_class = GroupPreferenceForm

        if not request.user.has_perm("core.change_group_preferences_rule", instance):
            raise PermissionDenied()
    else:
        # Invalid registry name passed from URL
        return HttpResponseNotFound()

    if not section and len(registry.sections()) > 0:
        default_section = list(registry.sections())[0]
        return redirect(f"preferences_{registry_name}", default_section)

    # Build final form from dynamic-preferences
    form_class = preference_form_builder(form_class, instance=instance, section=section)

    # Get layout
    form_class.layout = PreferenceLayout(form_class, section=section)

    if request.method == "POST":
        form = form_class(request.POST, request.FILES or None)
        if form.is_valid():
            form.update_preferences()
            messages.success(request, _("The preferences have been saved successfully."))
    else:
        form = form_class()

    context["registry"] = registry
    context["registry_name"] = registry_name
    context["section"] = section
    context["registry_url"] = "preferences_" + registry_name
    context["form"] = form
    context["instance"] = instance

    return render(request, "dynamic_preferences/form.html", context)
Example #7
0
def tournament_preference_form_builder(instance, preferences=[], **kwargs):
    return preference_form_builder(
        TournamentPreferenceForm, preferences, model={'instance': instance}, **kwargs)
Example #8
0
 def get_form_class(self, *args, **kwargs):
     form_class = preference_form_builder(UserPreferenceForm,
                                          instance=self.request.user,
                                          section=self.kwargs.get(
                                              'section', None))
     return form_class