def create_poll_image(request, library_code, id, managed_libraries=[]): library = org_utils.get_library(library_code, managed_libraries) if not library: return HttpResponseForbidden(u'Вы должны быть сотрудником этой организации') poll = get_object_or_404(Poll, id=id, library=library) if request.method == 'POST': poll_image_form = PollImageForm(request.POST,request.FILES, prefix='poll_image_form') poll_image_content_forms = [] for lang in settings.LANGUAGES: poll_image_content_forms.append({ 'form':PollImageContentForm(request.POST,prefix='poll_image_content' + lang[0]), 'lang':lang[0] }) if poll_image_form.is_valid(): valid = False for poll_image_content_form in poll_image_content_forms: valid = poll_image_content_form['form'].is_valid() if not valid: break if valid: poll_image = poll_image_form.save(commit=False) poll_image.poll = poll poll_image.save() for poll_image_content_form in poll_image_content_forms: poll_image_content = poll_image_content_form['form'].save(commit=False) poll_image_content.lang = poll_image_content_form['lang'] poll_image_content.poll_image = poll_image poll_image_content.save() poll_image_form.save_m2m() return redirect('participant_photopolls:administration:poll_detail', library_code=library_code, id=id) else: poll_image_form = PollImageForm(prefix="poll_image_form") poll_image_content_forms = [] for lang in settings.LANGUAGES: poll_image_content_forms.append({ 'form': PollImageContentForm(prefix='poll_image_content' + lang[0]), 'lang': lang[0] }) return render(request, 'participant_photopolls/administration/create_poll_image.html', { 'library': library, 'poll': poll, 'poll_image_form': poll_image_form, 'poll_image_content_forms': poll_image_content_forms, })
def edit_poll_image(request, library_code, id, image_id, managed_libraries=[]): library = org_utils.get_library(library_code, managed_libraries) if not library: return HttpResponseForbidden(u'Вы должны быть сотрудником этой организации') poll = get_object_or_404(Poll, id=id, library=library) poll_image = get_object_or_404(PollImage, poll=poll, id=image_id) poll_image_contents = PollImageContent.objects.filter(poll_image=poll_image) poll_image_contents_langs = {} for lang in settings.LANGUAGES: poll_image_contents_langs[lang] = None for poll_image_content in poll_image_contents: poll_image_contents_langs[poll_image_content.lang] = poll_image_content if request.method == 'POST': poll_image_form = PollImageForm(request.POST, request.FILES, prefix='poll_image_form', instance=poll_image) poll_image_content_forms = [] if poll_image_form.is_valid(): poll_image_form.save() poll_image_content_forms = [] for lang in settings.LANGUAGES: if lang in poll_image_contents_langs: lang = lang[0] if lang in poll_image_contents_langs: poll_image_content_forms.append({ 'form':PollImageContentForm(request.POST,prefix='poll_image_content_' + lang, instance=poll_image_contents_langs[lang]), 'lang':lang }) else: poll_image_content_forms.append({ 'form':PollImageContentForm(request.POST,prefix='poll_image_content_' + lang), 'lang':lang }) valid = False for poll_image_content_form in poll_image_content_forms: valid = poll_image_content_form['form'].is_valid() if not valid: break if valid: for poll_image_content_form in poll_image_content_forms: poll_image_content = poll_image_content_form['form'].save(commit=False) poll_image_content.poll_image = poll_image poll_image_content.lang = poll_image_content_form['lang'] poll_image_content.save() return redirect('participant_photopolls:administration:poll_detail', library_code=library_code, id=id) else: poll_image_form = PollImageForm(prefix="poll_image_form", instance=poll_image) poll_image_content_forms = [] for lang in settings.LANGUAGES: lang = lang[0] if lang in poll_image_contents_langs: poll_image_content_forms.append({ 'form':PollImageContentForm(prefix='poll_image_content_' + lang, instance=poll_image_contents_langs[lang]), 'lang':lang }) else: poll_image_content_forms.append({ 'form':PollImageContentForm(prefix='poll_image_content_' + lang), 'lang':lang }) return render(request, 'participant_photopolls/administration/edit_poll_image.html', { 'poll_image': poll_image, 'poll': poll, 'library': library, 'poll_image_form': poll_image_form, 'poll_image_content_forms': poll_image_content_forms, 'content_type': 'participant_photopoll_images_' + str(library.id), 'content_id': unicode(poll_image.id) })
def edit_poll_image(request, library_code, id, image_id, managed_libraries=[]): library = org_utils.get_library(library_code, managed_libraries) if not library: return HttpResponseForbidden( u'Вы должны быть сотрудником этой организации') poll = get_object_or_404(Poll, id=id, library=library) poll_image = get_object_or_404(PollImage, poll=poll, id=image_id) poll_image_contents = PollImageContent.objects.filter( poll_image=poll_image) poll_image_contents_langs = {} for lang in settings.LANGUAGES: poll_image_contents_langs[lang] = None for poll_image_content in poll_image_contents: poll_image_contents_langs[poll_image_content.lang] = poll_image_content if request.method == 'POST': poll_image_form = PollImageForm(request.POST, request.FILES, prefix='poll_image_form', instance=poll_image) poll_image_content_forms = [] if poll_image_form.is_valid(): poll_image_form.save() poll_image_content_forms = [] for lang in settings.LANGUAGES: if lang in poll_image_contents_langs: lang = lang[0] if lang in poll_image_contents_langs: poll_image_content_forms.append({ 'form': PollImageContentForm( request.POST, prefix='poll_image_content_' + lang, instance=poll_image_contents_langs[lang]), 'lang': lang }) else: poll_image_content_forms.append({ 'form': PollImageContentForm(request.POST, prefix='poll_image_content_' + lang), 'lang': lang }) valid = False for poll_image_content_form in poll_image_content_forms: valid = poll_image_content_form['form'].is_valid() if not valid: break if valid: for poll_image_content_form in poll_image_content_forms: poll_image_content = poll_image_content_form['form'].save( commit=False) poll_image_content.poll_image = poll_image poll_image_content.lang = poll_image_content_form['lang'] poll_image_content.save() return redirect( 'participant_photopolls:administration:poll_detail', library_code=library_code, id=id) else: poll_image_form = PollImageForm(prefix="poll_image_form", instance=poll_image) poll_image_content_forms = [] for lang in settings.LANGUAGES: lang = lang[0] if lang in poll_image_contents_langs: poll_image_content_forms.append({ 'form': PollImageContentForm( prefix='poll_image_content_' + lang, instance=poll_image_contents_langs[lang]), 'lang': lang }) else: poll_image_content_forms.append({ 'form': PollImageContentForm(prefix='poll_image_content_' + lang), 'lang': lang }) return render( request, 'participant_photopolls/administration/edit_poll_image.html', { 'poll_image': poll_image, 'poll': poll, 'library': library, 'poll_image_form': poll_image_form, 'poll_image_content_forms': poll_image_content_forms, 'content_type': 'participant_photopoll_images_' + str(library.id), 'content_id': unicode(poll_image.id) })
def create_poll_image(request, library_code, id, managed_libraries=[]): library = org_utils.get_library(library_code, managed_libraries) if not library: return HttpResponseForbidden( u'Вы должны быть сотрудником этой организации') poll = get_object_or_404(Poll, id=id, library=library) if request.method == 'POST': poll_image_form = PollImageForm(request.POST, request.FILES, prefix='poll_image_form') poll_image_content_forms = [] for lang in settings.LANGUAGES: poll_image_content_forms.append({ 'form': PollImageContentForm(request.POST, prefix='poll_image_content' + lang[0]), 'lang': lang[0] }) if poll_image_form.is_valid(): valid = False for poll_image_content_form in poll_image_content_forms: valid = poll_image_content_form['form'].is_valid() if not valid: break if valid: poll_image = poll_image_form.save(commit=False) poll_image.poll = poll poll_image.save() for poll_image_content_form in poll_image_content_forms: poll_image_content = poll_image_content_form['form'].save( commit=False) poll_image_content.lang = poll_image_content_form['lang'] poll_image_content.poll_image = poll_image poll_image_content.save() poll_image_form.save_m2m() return redirect( 'participant_photopolls:administration:poll_detail', library_code=library_code, id=id) else: poll_image_form = PollImageForm(prefix="poll_image_form") poll_image_content_forms = [] for lang in settings.LANGUAGES: poll_image_content_forms.append({ 'form': PollImageContentForm(prefix='poll_image_content' + lang[0]), 'lang': lang[0] }) return render( request, 'participant_photopolls/administration/create_poll_image.html', { 'library': library, 'poll': poll, 'poll_image_form': poll_image_form, 'poll_image_content_forms': poll_image_content_forms, })