We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Unable to add questions
The following link doesn't do anything: `https://[Domain]/orga/event/[CFP_name]/cfp/questions/new`

## Expected Behavior

Being be to create a question

## Current Behavior

Unable to create a question

## Possible Solution

Fix the edit button.

## Steps to Reproduce (for bugs)

1. Create a new CFP
2. Go on `https://[Domain]/orga/event/[CFP_name]/cfp/questions/new`
3. Click on the edit button (the pen)

## Context

Creating a CFP

## Your Environment
* Version used: 0.4
* Environment name and version: Firefox 58.0.1 & Chromium 64.0.3282.119
* Operating System and version: Ubuntu 17.10 / Server: Ubuntu 16.04 with python 3.6

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `src/pretalx/orga/views/cfp.py`
Content:
```
1 from csp.decorators import csp_update
2 from django.contrib import messages
3 from django.db import transaction
4 from django.db.models.deletion import ProtectedError
5 from django.forms.models import inlineformset_factory
6 from django.shortcuts import redirect
7 from django.utils.decorators import method_decorator
8 from django.utils.functional import cached_property
9 from django.utils.translation import ugettext_lazy as _
10 from django.views.generic import ListView, TemplateView, UpdateView, View
11 
12 from pretalx.common.forms import I18nFormSet
13 from pretalx.common.mixins.views import ActionFromUrl, PermissionRequired
14 from pretalx.common.views import CreateOrUpdateView
15 from pretalx.orga.forms import CfPForm, QuestionForm, SubmissionTypeForm
16 from pretalx.orga.forms.cfp import AnswerOptionForm, CfPSettingsForm
17 from pretalx.submission.models import (
18     AnswerOption, CfP, Question, SubmissionType,
19 )
20 
21 
22 class CfPTextDetail(PermissionRequired, ActionFromUrl, UpdateView):
23     form_class = CfPForm
24     model = CfP
25     template_name = 'orga/cfp/text.html'
26     permission_required = 'orga.edit_cfp'
27     write_permission_required = 'orga.edit_cfp'
28 
29     def get_context_data(self, *args, **kwargs):
30         ctx = super().get_context_data(*args, **kwargs)
31         ctx['sform'] = self.sform
32         return ctx
33 
34     @cached_property
35     def sform(self):
36         return CfPSettingsForm(
37             read_only=(self._action == 'view'),
38             locales=self.request.event.locales,
39             obj=self.request.event,
40             attribute_name='settings',
41             data=self.request.POST if self.request.method == "POST" else None,
42             prefix='settings'
43         )
44 
45     def get_object(self):
46         return self.request.event.cfp
47 
48     def get_success_url(self) -> str:
49         return self.get_object().urls.text
50 
51     def form_valid(self, form):
52         if not self.sform.is_valid():
53             messages.error(self.request, _('We had trouble saving your input.'))
54             return self.form_invalid(form)
55         messages.success(self.request, 'The CfP update has been saved.')
56         form.instance.event = self.request.event
57         ret = super().form_valid(form)
58         if form.has_changed():
59             form.instance.log_action('pretalx.cfp.update', person=self.request.user, orga=True)
60         self.sform.save()
61         return ret
62 
63 
64 class CfPQuestionList(PermissionRequired, TemplateView):
65     template_name = 'orga/cfp/question_view.html'
66     permission_required = 'orga.view_question'
67 
68     def get_permission_object(self):
69         return self.request.event
70 
71     def get_context_data(self, *args, **kwargs):
72         ctx = super().get_context_data(*args, **kwargs)
73         ctx['speaker_questions'] = Question.all_objects.filter(event=self.request.event, target='speaker')
74         ctx['submission_questions'] = Question.all_objects.filter(event=self.request.event, target='submission')
75         return ctx
76 
77 
78 @method_decorator(csp_update(SCRIPT_SRC="'self' 'unsafe-inline'"), name='dispatch')
79 class CfPQuestionDetail(PermissionRequired, ActionFromUrl, CreateOrUpdateView):
80     model = Question
81     form_class = QuestionForm
82     permission_required = 'orga.edit_question'
83     write_permission_required = 'orga.edit_question'
84 
85     def get_template_names(self):
86         if self.request.path.lstrip('/').endswith('edit'):
87             return 'orga/cfp/question_form.html'
88         return 'orga/cfp/question_detail.html'
89 
90     def get_permission_object(self):
91         return self.get_object() or self.request.event
92 
93     def get_object(self) -> Question:
94         return Question.all_objects.filter(event=self.request.event, pk=self.kwargs.get('pk')).first()
95 
96     @cached_property
97     def formset(self):
98         formset_class = inlineformset_factory(
99             Question, AnswerOption, form=AnswerOptionForm, formset=I18nFormSet,
100             can_delete=True, extra=0,
101         )
102         return formset_class(
103             self.request.POST if self.request.method == 'POST' else None,
104             queryset=AnswerOption.objects.filter(question=self.get_object()) if self.get_object() else AnswerOption.objects.none(),
105             event=self.request.event
106         )
107 
108     def save_formset(self, obj):
109         if self.formset.is_valid():
110             for form in self.formset.initial_forms:
111                 if form in self.formset.deleted_forms:
112                     if not form.instance.pk:
113                         continue
114                     obj.log_action(
115                         'pretalx.question.option.delete', person=self.request.user, orga=True, data={
116                             'id': form.instance.pk
117                         }
118                     )
119                     form.instance.delete()
120                     form.instance.pk = None
121                 elif form.has_changed():
122                     form.instance.question = obj
123                     form.save()
124                     change_data = {k: form.cleaned_data.get(k) for k in form.changed_data}
125                     change_data['id'] = form.instance.pk
126                     obj.log_action(
127                         'pretalx.question.option.update',
128                         person=self.request.user, orga=True, data=change_data
129                     )
130 
131             for form in self.formset.extra_forms:
132                 if not form.has_changed():
133                     continue
134                 if self.formset._should_delete_form(form):
135                     continue
136                 form.instance.question = obj
137                 form.save()
138                 change_data = {k: form.cleaned_data.get(k) for k in form.changed_data}
139                 change_data['id'] = form.instance.pk
140                 obj.log_action(
141                     'pretalx.question.option.create',
142                     person=self.request.user, orga=True, data=change_data
143                 )
144 
145             return True
146         return False
147 
148     def get_context_data(self, *args, **kwargs):
149         ctx = super().get_context_data(*args, **kwargs)
150         ctx['formset'] = self.formset
151         ctx['question'] = self.get_object()
152         return ctx
153 
154     def get_form_kwargs(self, *args, **kwargs):
155         kwargs = super().get_form_kwargs(*args, **kwargs)
156         if not self.get_object():
157             initial = kwargs['initial'] or dict()
158             initial['target'] = self.request.GET.get('type')
159             kwargs['initial'] = initial
160         return kwargs
161 
162     def get_success_url(self) -> str:
163         obj = self.get_object() or self.instance
164         return obj.urls.base
165 
166     @transaction.atomic
167     def form_valid(self, form):
168         form.instance.event = self.request.event
169         self.instance = form.instance
170         ret = super().form_valid(form)
171         if form.cleaned_data.get('variant') in ('choices', 'multiple_choice'):
172             result = self.save_formset(self.instance)
173             if not result:
174                 return self.get(self.request, *self.args, **self.kwargs)
175         if form.has_changed():
176             action = 'pretalx.question.' + ('update' if self.object else 'create')
177             form.instance.log_action(action, person=self.request.user, orga=True)
178         messages.success(self.request, 'The question has been saved.')
179         return ret
180 
181 
182 class CfPQuestionDelete(PermissionRequired, View):
183     permission_required = 'orga.remove_question'
184 
185     def get_object(self) -> Question:
186         return Question.all_objects.get(event=self.request.event, pk=self.kwargs.get('pk'))
187 
188     def dispatch(self, request, *args, **kwargs):
189         super().dispatch(request, *args, **kwargs)
190         question = self.get_object()
191 
192         try:
193             with transaction.atomic():
194                 question.options.all().delete()
195                 question.delete()
196                 question.log_action('pretalx.question.delete', person=self.request.user, orga=True)
197                 messages.success(request, _('The question has been deleted.'))
198         except ProtectedError:
199             question.active = False
200             question.save()
201             messages.error(request, _('You cannot delete a question that has already been answered. We have deactivated the question instead.'))
202         return redirect(self.request.event.cfp.urls.questions)
203 
204 
205 class CfPQuestionToggle(PermissionRequired, View):
206     permission_required = 'orga.edit_question'
207 
208     def get_object(self) -> Question:
209         return Question.all_objects.filter(event=self.request.event, pk=self.kwargs.get('pk')).first()
210 
211     def dispatch(self, request, *args, **kwargs):
212         super().dispatch(request, *args, **kwargs)
213         question = self.get_object()
214 
215         question.active = not question.active
216         question.save(update_fields=['active'])
217         return redirect(question.urls.base)
218 
219 
220 class SubmissionTypeList(PermissionRequired, ListView):
221     template_name = 'orga/cfp/submission_type_view.html'
222     context_object_name = 'types'
223     permission_required = 'orga.view_submission_type'
224 
225     def get_permission_object(self):
226         return self.request.event
227 
228     def get_queryset(self):
229         return self.request.event.submission_types.all()
230 
231 
232 class SubmissionTypeDetail(PermissionRequired, ActionFromUrl, CreateOrUpdateView):
233     model = SubmissionType
234     form_class = SubmissionTypeForm
235     template_name = 'orga/cfp/submission_type_form.html'
236     permission_required = 'orga.edit_submission_type'
237     write_permission_required = 'orga.edit_submission_type'
238 
239     def get_success_url(self) -> str:
240         return self.request.event.cfp.urls.types
241 
242     def get_object(self):
243         return self.request.event.submission_types.filter(pk=self.kwargs.get('pk')).first()
244 
245     def get_permission_object(self):
246         return self.get_object() or self.request.event
247 
248     def form_valid(self, form):
249         messages.success(self.request, 'The Submission Type has been saved.')
250         form.instance.event = self.request.event
251         ret = super().form_valid(form)
252         if form.has_changed():
253             action = 'pretalx.submission_type.' + ('update' if self.object else 'create')
254             form.instance.log_action(action, person=self.request.user, orga=True)
255         return ret
256 
257 
258 class SubmissionTypeDefault(PermissionRequired, View):
259     permission_required = 'orga.edit_submission_type'
260 
261     def get_object(self):
262         return self.request.event.submission_types.get(pk=self.kwargs.get('pk'))
263 
264     def dispatch(self, request, *args, **kwargs):
265         super().dispatch(request, *args, **kwargs)
266         submission_type = self.get_object()
267         self.request.event.cfp.default_type = submission_type
268         self.request.event.cfp.save(update_fields=['default_type'])
269         submission_type.log_action('pretalx.submission_type.make_default', person=self.request.user, orga=True)
270         messages.success(request, _('The Submission Type has been made default.'))
271         return redirect(self.request.event.cfp.urls.types)
272 
273 
274 class SubmissionTypeDelete(PermissionRequired, View):
275     permission_required = 'orga.remove_submission_type'
276 
277     def get_object(self):
278         return self.request.event.submission_types.get(pk=self.kwargs.get('pk'))
279 
280     def dispatch(self, request, *args, **kwargs):
281         super().dispatch(request, *args, **kwargs)
282         submission_type = self.get_object()
283 
284         if request.event.submission_types.count() == 1:
285             messages.error(request, _('You cannot delete the only submission type. Try creating another one first!'))
286         elif request.event.cfp.default_type == submission_type:
287             messages.error(request, _('You cannot delete the default submission type. Make another type default first!'))
288         else:
289             try:
290                 submission_type.delete()
291                 request.event.log_action('pretalx.submission_type.delete', person=self.request.user, orga=True)
292                 messages.success(request, _('The Submission Type has been deleted.'))
293             except ProtectedError:  # TODO: show which/how many submissions are concerned
294                 messages.error(request, _('This Submission Type is in use in a submission and cannot be deleted.'))
295         return redirect(self.request.event.cfp.urls.types)
296
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/src/pretalx/orga/views/cfp.py b/src/pretalx/orga/views/cfp.py
--- a/src/pretalx/orga/views/cfp.py
+++ b/src/pretalx/orga/views/cfp.py
@@ -83,7 +83,8 @@
     write_permission_required = 'orga.edit_question'
 
     def get_template_names(self):
-        if self.request.path.lstrip('/').endswith('edit'):
+        action = self.request.path.lstrip('/').rpartition('/')[2]
+        if action in ('edit', 'new'):
             return 'orga/cfp/question_form.html'
         return 'orga/cfp/question_detail.html'

{"golden_diff": "diff --git a/src/pretalx/orga/views/cfp.py b/src/pretalx/orga/views/cfp.py\n--- a/src/pretalx/orga/views/cfp.py\n+++ b/src/pretalx/orga/views/cfp.py\n@@ -83,7 +83,8 @@\n     write_permission_required = 'orga.edit_question'\n \n     def get_template_names(self):\n-        if self.request.path.lstrip('/').endswith('edit'):\n+        action = self.request.path.lstrip('/').rpartition('/')[2]\n+        if action in ('edit', 'new'):\n             return 'orga/cfp/question_form.html'\n         return 'orga/cfp/question_detail.html'\n", "issue": "Unable to add questions\nThe following link doesn't do anything: `https://[Domain]/orga/event/[CFP_name]/cfp/questions/new`\r\n\r\n## Expected Behavior\r\n\r\nBeing be to create a question\r\n\r\n## Current Behavior\r\n\r\nUnable to create a question\r\n\r\n## Possible Solution\r\n\r\nFix the edit button.\r\n\r\n## Steps to Reproduce (for bugs)\r\n\r\n1. Create a new CFP\r\n2. Go on `https://[Domain]/orga/event/[CFP_name]/cfp/questions/new`\r\n3. Click on the edit button (the pen)\r\n\r\n## Context\r\n\r\nCreating a CFP\r\n\r\n## Your Environment\r\n* Version used: 0.4\r\n* Environment name and version: Firefox 58.0.1 & Chromium 64.0.3282.119\r\n* Operating System and version: Ubuntu 17.10 / Server: Ubuntu 16.04 with python 3.6\n", "before_files": [{"content": "from csp.decorators import csp_update\nfrom django.contrib import messages\nfrom django.db import transaction\nfrom django.db.models.deletion import ProtectedError\nfrom django.forms.models import inlineformset_factory\nfrom django.shortcuts import redirect\nfrom django.utils.decorators import method_decorator\nfrom django.utils.functional import cached_property\nfrom django.utils.translation import ugettext_lazy as _\nfrom django.views.generic import ListView, TemplateView, UpdateView, View\n\nfrom pretalx.common.forms import I18nFormSet\nfrom pretalx.common.mixins.views import ActionFromUrl, PermissionRequired\nfrom pretalx.common.views import CreateOrUpdateView\nfrom pretalx.orga.forms import CfPForm, QuestionForm, SubmissionTypeForm\nfrom pretalx.orga.forms.cfp import AnswerOptionForm, CfPSettingsForm\nfrom pretalx.submission.models import (\n    AnswerOption, CfP, Question, SubmissionType,\n)\n\n\nclass CfPTextDetail(PermissionRequired, ActionFromUrl, UpdateView):\n    form_class = CfPForm\n    model = CfP\n    template_name = 'orga/cfp/text.html'\n    permission_required = 'orga.edit_cfp'\n    write_permission_required = 'orga.edit_cfp'\n\n    def get_context_data(self, *args, **kwargs):\n        ctx = super().get_context_data(*args, **kwargs)\n        ctx['sform'] = self.sform\n        return ctx\n\n    @cached_property\n    def sform(self):\n        return CfPSettingsForm(\n            read_only=(self._action == 'view'),\n            locales=self.request.event.locales,\n            obj=self.request.event,\n            attribute_name='settings',\n            data=self.request.POST if self.request.method == \"POST\" else None,\n            prefix='settings'\n        )\n\n    def get_object(self):\n        return self.request.event.cfp\n\n    def get_success_url(self) -> str:\n        return self.get_object().urls.text\n\n    def form_valid(self, form):\n        if not self.sform.is_valid():\n            messages.error(self.request, _('We had trouble saving your input.'))\n            return self.form_invalid(form)\n        messages.success(self.request, 'The CfP update has been saved.')\n        form.instance.event = self.request.event\n        ret = super().form_valid(form)\n        if form.has_changed():\n            form.instance.log_action('pretalx.cfp.update', person=self.request.user, orga=True)\n        self.sform.save()\n        return ret\n\n\nclass CfPQuestionList(PermissionRequired, TemplateView):\n    template_name = 'orga/cfp/question_view.html'\n    permission_required = 'orga.view_question'\n\n    def get_permission_object(self):\n        return self.request.event\n\n    def get_context_data(self, *args, **kwargs):\n        ctx = super().get_context_data(*args, **kwargs)\n        ctx['speaker_questions'] = Question.all_objects.filter(event=self.request.event, target='speaker')\n        ctx['submission_questions'] = Question.all_objects.filter(event=self.request.event, target='submission')\n        return ctx\n\n\n@method_decorator(csp_update(SCRIPT_SRC=\"'self' 'unsafe-inline'\"), name='dispatch')\nclass CfPQuestionDetail(PermissionRequired, ActionFromUrl, CreateOrUpdateView):\n    model = Question\n    form_class = QuestionForm\n    permission_required = 'orga.edit_question'\n    write_permission_required = 'orga.edit_question'\n\n    def get_template_names(self):\n        if self.request.path.lstrip('/').endswith('edit'):\n            return 'orga/cfp/question_form.html'\n        return 'orga/cfp/question_detail.html'\n\n    def get_permission_object(self):\n        return self.get_object() or self.request.event\n\n    def get_object(self) -> Question:\n        return Question.all_objects.filter(event=self.request.event, pk=self.kwargs.get('pk')).first()\n\n    @cached_property\n    def formset(self):\n        formset_class = inlineformset_factory(\n            Question, AnswerOption, form=AnswerOptionForm, formset=I18nFormSet,\n            can_delete=True, extra=0,\n        )\n        return formset_class(\n            self.request.POST if self.request.method == 'POST' else None,\n            queryset=AnswerOption.objects.filter(question=self.get_object()) if self.get_object() else AnswerOption.objects.none(),\n            event=self.request.event\n        )\n\n    def save_formset(self, obj):\n        if self.formset.is_valid():\n            for form in self.formset.initial_forms:\n                if form in self.formset.deleted_forms:\n                    if not form.instance.pk:\n                        continue\n                    obj.log_action(\n                        'pretalx.question.option.delete', person=self.request.user, orga=True, data={\n                            'id': form.instance.pk\n                        }\n                    )\n                    form.instance.delete()\n                    form.instance.pk = None\n                elif form.has_changed():\n                    form.instance.question = obj\n                    form.save()\n                    change_data = {k: form.cleaned_data.get(k) for k in form.changed_data}\n                    change_data['id'] = form.instance.pk\n                    obj.log_action(\n                        'pretalx.question.option.update',\n                        person=self.request.user, orga=True, data=change_data\n                    )\n\n            for form in self.formset.extra_forms:\n                if not form.has_changed():\n                    continue\n                if self.formset._should_delete_form(form):\n                    continue\n                form.instance.question = obj\n                form.save()\n                change_data = {k: form.cleaned_data.get(k) for k in form.changed_data}\n                change_data['id'] = form.instance.pk\n                obj.log_action(\n                    'pretalx.question.option.create',\n                    person=self.request.user, orga=True, data=change_data\n                )\n\n            return True\n        return False\n\n    def get_context_data(self, *args, **kwargs):\n        ctx = super().get_context_data(*args, **kwargs)\n        ctx['formset'] = self.formset\n        ctx['question'] = self.get_object()\n        return ctx\n\n    def get_form_kwargs(self, *args, **kwargs):\n        kwargs = super().get_form_kwargs(*args, **kwargs)\n        if not self.get_object():\n            initial = kwargs['initial'] or dict()\n            initial['target'] = self.request.GET.get('type')\n            kwargs['initial'] = initial\n        return kwargs\n\n    def get_success_url(self) -> str:\n        obj = self.get_object() or self.instance\n        return obj.urls.base\n\n    @transaction.atomic\n    def form_valid(self, form):\n        form.instance.event = self.request.event\n        self.instance = form.instance\n        ret = super().form_valid(form)\n        if form.cleaned_data.get('variant') in ('choices', 'multiple_choice'):\n            result = self.save_formset(self.instance)\n            if not result:\n                return self.get(self.request, *self.args, **self.kwargs)\n        if form.has_changed():\n            action = 'pretalx.question.' + ('update' if self.object else 'create')\n            form.instance.log_action(action, person=self.request.user, orga=True)\n        messages.success(self.request, 'The question has been saved.')\n        return ret\n\n\nclass CfPQuestionDelete(PermissionRequired, View):\n    permission_required = 'orga.remove_question'\n\n    def get_object(self) -> Question:\n        return Question.all_objects.get(event=self.request.event, pk=self.kwargs.get('pk'))\n\n    def dispatch(self, request, *args, **kwargs):\n        super().dispatch(request, *args, **kwargs)\n        question = self.get_object()\n\n        try:\n            with transaction.atomic():\n                question.options.all().delete()\n                question.delete()\n                question.log_action('pretalx.question.delete', person=self.request.user, orga=True)\n                messages.success(request, _('The question has been deleted.'))\n        except ProtectedError:\n            question.active = False\n            question.save()\n            messages.error(request, _('You cannot delete a question that has already been answered. We have deactivated the question instead.'))\n        return redirect(self.request.event.cfp.urls.questions)\n\n\nclass CfPQuestionToggle(PermissionRequired, View):\n    permission_required = 'orga.edit_question'\n\n    def get_object(self) -> Question:\n        return Question.all_objects.filter(event=self.request.event, pk=self.kwargs.get('pk')).first()\n\n    def dispatch(self, request, *args, **kwargs):\n        super().dispatch(request, *args, **kwargs)\n        question = self.get_object()\n\n        question.active = not question.active\n        question.save(update_fields=['active'])\n        return redirect(question.urls.base)\n\n\nclass SubmissionTypeList(PermissionRequired, ListView):\n    template_name = 'orga/cfp/submission_type_view.html'\n    context_object_name = 'types'\n    permission_required = 'orga.view_submission_type'\n\n    def get_permission_object(self):\n        return self.request.event\n\n    def get_queryset(self):\n        return self.request.event.submission_types.all()\n\n\nclass SubmissionTypeDetail(PermissionRequired, ActionFromUrl, CreateOrUpdateView):\n    model = SubmissionType\n    form_class = SubmissionTypeForm\n    template_name = 'orga/cfp/submission_type_form.html'\n    permission_required = 'orga.edit_submission_type'\n    write_permission_required = 'orga.edit_submission_type'\n\n    def get_success_url(self) -> str:\n        return self.request.event.cfp.urls.types\n\n    def get_object(self):\n        return self.request.event.submission_types.filter(pk=self.kwargs.get('pk')).first()\n\n    def get_permission_object(self):\n        return self.get_object() or self.request.event\n\n    def form_valid(self, form):\n        messages.success(self.request, 'The Submission Type has been saved.')\n        form.instance.event = self.request.event\n        ret = super().form_valid(form)\n        if form.has_changed():\n            action = 'pretalx.submission_type.' + ('update' if self.object else 'create')\n            form.instance.log_action(action, person=self.request.user, orga=True)\n        return ret\n\n\nclass SubmissionTypeDefault(PermissionRequired, View):\n    permission_required = 'orga.edit_submission_type'\n\n    def get_object(self):\n        return self.request.event.submission_types.get(pk=self.kwargs.get('pk'))\n\n    def dispatch(self, request, *args, **kwargs):\n        super().dispatch(request, *args, **kwargs)\n        submission_type = self.get_object()\n        self.request.event.cfp.default_type = submission_type\n        self.request.event.cfp.save(update_fields=['default_type'])\n        submission_type.log_action('pretalx.submission_type.make_default', person=self.request.user, orga=True)\n        messages.success(request, _('The Submission Type has been made default.'))\n        return redirect(self.request.event.cfp.urls.types)\n\n\nclass SubmissionTypeDelete(PermissionRequired, View):\n    permission_required = 'orga.remove_submission_type'\n\n    def get_object(self):\n        return self.request.event.submission_types.get(pk=self.kwargs.get('pk'))\n\n    def dispatch(self, request, *args, **kwargs):\n        super().dispatch(request, *args, **kwargs)\n        submission_type = self.get_object()\n\n        if request.event.submission_types.count() == 1:\n            messages.error(request, _('You cannot delete the only submission type. Try creating another one first!'))\n        elif request.event.cfp.default_type == submission_type:\n            messages.error(request, _('You cannot delete the default submission type. Make another type default first!'))\n        else:\n            try:\n                submission_type.delete()\n                request.event.log_action('pretalx.submission_type.delete', person=self.request.user, orga=True)\n                messages.success(request, _('The Submission Type has been deleted.'))\n            except ProtectedError:  # TODO: show which/how many submissions are concerned\n                messages.error(request, _('This Submission Type is in use in a submission and cannot be deleted.'))\n        return redirect(self.request.event.cfp.urls.types)\n", "path": "src/pretalx/orga/views/cfp.py"}], "after_files": [{"content": "from csp.decorators import csp_update\nfrom django.contrib import messages\nfrom django.db import transaction\nfrom django.db.models.deletion import ProtectedError\nfrom django.forms.models import inlineformset_factory\nfrom django.shortcuts import redirect\nfrom django.utils.decorators import method_decorator\nfrom django.utils.functional import cached_property\nfrom django.utils.translation import ugettext_lazy as _\nfrom django.views.generic import ListView, TemplateView, UpdateView, View\n\nfrom pretalx.common.forms import I18nFormSet\nfrom pretalx.common.mixins.views import ActionFromUrl, PermissionRequired\nfrom pretalx.common.views import CreateOrUpdateView\nfrom pretalx.orga.forms import CfPForm, QuestionForm, SubmissionTypeForm\nfrom pretalx.orga.forms.cfp import AnswerOptionForm, CfPSettingsForm\nfrom pretalx.submission.models import (\n    AnswerOption, CfP, Question, SubmissionType,\n)\n\n\nclass CfPTextDetail(PermissionRequired, ActionFromUrl, UpdateView):\n    form_class = CfPForm\n    model = CfP\n    template_name = 'orga/cfp/text.html'\n    permission_required = 'orga.edit_cfp'\n    write_permission_required = 'orga.edit_cfp'\n\n    def get_context_data(self, *args, **kwargs):\n        ctx = super().get_context_data(*args, **kwargs)\n        ctx['sform'] = self.sform\n        return ctx\n\n    @cached_property\n    def sform(self):\n        return CfPSettingsForm(\n            read_only=(self._action == 'view'),\n            locales=self.request.event.locales,\n            obj=self.request.event,\n            attribute_name='settings',\n            data=self.request.POST if self.request.method == \"POST\" else None,\n            prefix='settings'\n        )\n\n    def get_object(self):\n        return self.request.event.cfp\n\n    def get_success_url(self) -> str:\n        return self.get_object().urls.text\n\n    def form_valid(self, form):\n        if not self.sform.is_valid():\n            messages.error(self.request, _('We had trouble saving your input.'))\n            return self.form_invalid(form)\n        messages.success(self.request, 'The CfP update has been saved.')\n        form.instance.event = self.request.event\n        ret = super().form_valid(form)\n        if form.has_changed():\n            form.instance.log_action('pretalx.cfp.update', person=self.request.user, orga=True)\n        self.sform.save()\n        return ret\n\n\nclass CfPQuestionList(PermissionRequired, TemplateView):\n    template_name = 'orga/cfp/question_view.html'\n    permission_required = 'orga.view_question'\n\n    def get_permission_object(self):\n        return self.request.event\n\n    def get_context_data(self, *args, **kwargs):\n        ctx = super().get_context_data(*args, **kwargs)\n        ctx['speaker_questions'] = Question.all_objects.filter(event=self.request.event, target='speaker')\n        ctx['submission_questions'] = Question.all_objects.filter(event=self.request.event, target='submission')\n        return ctx\n\n\n@method_decorator(csp_update(SCRIPT_SRC=\"'self' 'unsafe-inline'\"), name='dispatch')\nclass CfPQuestionDetail(PermissionRequired, ActionFromUrl, CreateOrUpdateView):\n    model = Question\n    form_class = QuestionForm\n    permission_required = 'orga.edit_question'\n    write_permission_required = 'orga.edit_question'\n\n    def get_template_names(self):\n        action = self.request.path.lstrip('/').rpartition('/')[2]\n        if action in ('edit', 'new'):\n            return 'orga/cfp/question_form.html'\n        return 'orga/cfp/question_detail.html'\n\n    def get_permission_object(self):\n        return self.get_object() or self.request.event\n\n    def get_object(self) -> Question:\n        return Question.all_objects.filter(event=self.request.event, pk=self.kwargs.get('pk')).first()\n\n    @cached_property\n    def formset(self):\n        formset_class = inlineformset_factory(\n            Question, AnswerOption, form=AnswerOptionForm, formset=I18nFormSet,\n            can_delete=True, extra=0,\n        )\n        return formset_class(\n            self.request.POST if self.request.method == 'POST' else None,\n            queryset=AnswerOption.objects.filter(question=self.get_object()) if self.get_object() else AnswerOption.objects.none(),\n            event=self.request.event\n        )\n\n    def save_formset(self, obj):\n        if self.formset.is_valid():\n            for form in self.formset.initial_forms:\n                if form in self.formset.deleted_forms:\n                    if not form.instance.pk:\n                        continue\n                    obj.log_action(\n                        'pretalx.question.option.delete', person=self.request.user, orga=True, data={\n                            'id': form.instance.pk\n                        }\n                    )\n                    form.instance.delete()\n                    form.instance.pk = None\n                elif form.has_changed():\n                    form.instance.question = obj\n                    form.save()\n                    change_data = {k: form.cleaned_data.get(k) for k in form.changed_data}\n                    change_data['id'] = form.instance.pk\n                    obj.log_action(\n                        'pretalx.question.option.update',\n                        person=self.request.user, orga=True, data=change_data\n                    )\n\n            for form in self.formset.extra_forms:\n                if not form.has_changed():\n                    continue\n                if self.formset._should_delete_form(form):\n                    continue\n                form.instance.question = obj\n                form.save()\n                change_data = {k: form.cleaned_data.get(k) for k in form.changed_data}\n                change_data['id'] = form.instance.pk\n                obj.log_action(\n                    'pretalx.question.option.create',\n                    person=self.request.user, orga=True, data=change_data\n                )\n\n            return True\n        return False\n\n    def get_context_data(self, *args, **kwargs):\n        ctx = super().get_context_data(*args, **kwargs)\n        ctx['formset'] = self.formset\n        ctx['question'] = self.get_object()\n        return ctx\n\n    def get_form_kwargs(self, *args, **kwargs):\n        kwargs = super().get_form_kwargs(*args, **kwargs)\n        if not self.get_object():\n            initial = kwargs['initial'] or dict()\n            initial['target'] = self.request.GET.get('type')\n            kwargs['initial'] = initial\n        return kwargs\n\n    def get_success_url(self) -> str:\n        obj = self.get_object() or self.instance\n        return obj.urls.base\n\n    @transaction.atomic\n    def form_valid(self, form):\n        form.instance.event = self.request.event\n        self.instance = form.instance\n        ret = super().form_valid(form)\n        if form.cleaned_data.get('variant') in ('choices', 'multiple_choice'):\n            result = self.save_formset(self.instance)\n            if not result:\n                return self.get(self.request, *self.args, **self.kwargs)\n        if form.has_changed():\n            action = 'pretalx.question.' + ('update' if self.object else 'create')\n            form.instance.log_action(action, person=self.request.user, orga=True)\n        messages.success(self.request, 'The question has been saved.')\n        return ret\n\n\nclass CfPQuestionDelete(PermissionRequired, View):\n    permission_required = 'orga.remove_question'\n\n    def get_object(self) -> Question:\n        return Question.all_objects.get(event=self.request.event, pk=self.kwargs.get('pk'))\n\n    def dispatch(self, request, *args, **kwargs):\n        super().dispatch(request, *args, **kwargs)\n        question = self.get_object()\n\n        try:\n            with transaction.atomic():\n                question.options.all().delete()\n                question.delete()\n                question.log_action('pretalx.question.delete', person=self.request.user, orga=True)\n                messages.success(request, _('The question has been deleted.'))\n        except ProtectedError:\n            question.active = False\n            question.save()\n            messages.error(request, _('You cannot delete a question that has already been answered. We have deactivated the question instead.'))\n        return redirect(self.request.event.cfp.urls.questions)\n\n\nclass CfPQuestionToggle(PermissionRequired, View):\n    permission_required = 'orga.edit_question'\n\n    def get_object(self) -> Question:\n        return Question.all_objects.filter(event=self.request.event, pk=self.kwargs.get('pk')).first()\n\n    def dispatch(self, request, *args, **kwargs):\n        super().dispatch(request, *args, **kwargs)\n        question = self.get_object()\n\n        question.active = not question.active\n        question.save(update_fields=['active'])\n        return redirect(question.urls.base)\n\n\nclass SubmissionTypeList(PermissionRequired, ListView):\n    template_name = 'orga/cfp/submission_type_view.html'\n    context_object_name = 'types'\n    permission_required = 'orga.view_submission_type'\n\n    def get_permission_object(self):\n        return self.request.event\n\n    def get_queryset(self):\n        return self.request.event.submission_types.all()\n\n\nclass SubmissionTypeDetail(PermissionRequired, ActionFromUrl, CreateOrUpdateView):\n    model = SubmissionType\n    form_class = SubmissionTypeForm\n    template_name = 'orga/cfp/submission_type_form.html'\n    permission_required = 'orga.edit_submission_type'\n    write_permission_required = 'orga.edit_submission_type'\n\n    def get_success_url(self) -> str:\n        return self.request.event.cfp.urls.types\n\n    def get_object(self):\n        return self.request.event.submission_types.filter(pk=self.kwargs.get('pk')).first()\n\n    def get_permission_object(self):\n        return self.get_object() or self.request.event\n\n    def form_valid(self, form):\n        messages.success(self.request, 'The Submission Type has been saved.')\n        form.instance.event = self.request.event\n        ret = super().form_valid(form)\n        if form.has_changed():\n            action = 'pretalx.submission_type.' + ('update' if self.object else 'create')\n            form.instance.log_action(action, person=self.request.user, orga=True)\n        return ret\n\n\nclass SubmissionTypeDefault(PermissionRequired, View):\n    permission_required = 'orga.edit_submission_type'\n\n    def get_object(self):\n        return self.request.event.submission_types.get(pk=self.kwargs.get('pk'))\n\n    def dispatch(self, request, *args, **kwargs):\n        super().dispatch(request, *args, **kwargs)\n        submission_type = self.get_object()\n        self.request.event.cfp.default_type = submission_type\n        self.request.event.cfp.save(update_fields=['default_type'])\n        submission_type.log_action('pretalx.submission_type.make_default', person=self.request.user, orga=True)\n        messages.success(request, _('The Submission Type has been made default.'))\n        return redirect(self.request.event.cfp.urls.types)\n\n\nclass SubmissionTypeDelete(PermissionRequired, View):\n    permission_required = 'orga.remove_submission_type'\n\n    def get_object(self):\n        return self.request.event.submission_types.get(pk=self.kwargs.get('pk'))\n\n    def dispatch(self, request, *args, **kwargs):\n        super().dispatch(request, *args, **kwargs)\n        submission_type = self.get_object()\n\n        if request.event.submission_types.count() == 1:\n            messages.error(request, _('You cannot delete the only submission type. Try creating another one first!'))\n        elif request.event.cfp.default_type == submission_type:\n            messages.error(request, _('You cannot delete the default submission type. Make another type default first!'))\n        else:\n            try:\n                submission_type.delete()\n                request.event.log_action('pretalx.submission_type.delete', person=self.request.user, orga=True)\n                messages.success(request, _('The Submission Type has been deleted.'))\n            except ProtectedError:  # TODO: show which/how many submissions are concerned\n                messages.error(request, _('This Submission Type is in use in a submission and cannot be deleted.'))\n        return redirect(self.request.event.cfp.urls.types)\n", "path": "src/pretalx/orga/views/cfp.py"}]}