예제 #1
0
    def post(request, lang):

        sid = transaction.savepoint()

        if lang == 'ja':
            form = IntroductionForm(request.POST)
            introduction_model = Introduction()
            video_model = Video()
            title_model = Title()
        else:
            form = IntroductionEnForm(request.POST)
            introduction_model = IntroductionEn()
            video_model = VideoEn()
            title_model = TitleEn()

        if form.errors:
            messages.add_message(request, messages.INFO,
                                 dict(form.errors.items()))

        if form.is_valid():
            try:
                res_introduction = introduction_model.create_introduction({
                    'name':
                    form.cleaned_data.get('name'),
                    'text':
                    form.cleaned_data.get('text'),
                    'thumbnail':
                    form.cleaned_data.get('thumbnail'),
                })
                add_videos = form.cleaned_data.get('videos')
                if add_videos:
                    video_model.add_video_from_introduction(
                        res_introduction.id, add_videos)

                add_titles = form.cleaned_data.get('titles')
                if add_titles:
                    introduction_model.add_title(res_introduction.id,
                                                 add_titles)

                transaction.savepoint_commit(sid)

                return HttpResponseRedirect(
                    '/{}/admin/introductions'.format(lang))

            except:

                transaction.savepoint_rollback(sid)
                pass

        select_titles = []
        if form.cleaned_data.get('titles'):
            title_ids = list(map(int, form.cleaned_data.get('titles')))
            select_titles = title_model.get_by_ids(title_ids)

        select_videos = []
        if form.cleaned_data.get('videos'):
            video_ids = list(map(int, form.cleaned_data.get('videos')))
            select_videos = video_model.get_by_ids(video_ids)

        return TemplateResponse(
            request, 'introduction_create.html', {
                'title': '新規投稿 | イントロダクション | FEED App 管理',
                'select_titles': select_titles,
                'select_videos': select_videos,
                'form_data': form.cleaned_data,
                'error_messages': get_error_message(request),
                'lang': lang,
            })