Example #1
0
    def post(request, lang):

        sid = transaction.savepoint()

        if lang == 'ja':
            form = VideoForm(request.POST)
            video_model = Video()
        else:
            form = VideoEnForm(request.POST)
            video_model = VideoEn()
        if form.errors:
            messages.add_message(request, messages.INFO,
                                 dict(form.errors.items()))

        if form.is_valid():
            try:
                res_video = video_model.create_video({
                    'published_at':
                    form.cleaned_data.get('published_at'),
                    'title':
                    form.cleaned_data.get('title'),
                    'text':
                    form.cleaned_data.get('text'),
                    'youtube_id':
                    form.cleaned_data.get('youtube_id'),
                })
                add_introductions = form.cleaned_data.get('introductions')
                if add_introductions:
                    video_model.add_introduction(res_video.id,
                                                 add_introductions)
                add_categories = form.cleaned_data.get('categories')
                if add_categories:
                    video_model.add_category(res_video.id, add_categories)
                add_topics = form.cleaned_data.get('topics')
                if add_topics:
                    video_model.add_topic(res_video.id, add_topics)

                transaction.savepoint_commit(sid)

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

            except:
                transaction.savepoint_rollback(sid)
                pass

        if lang == 'ja':
            topic_model = Topic()
            introduction_model = Introduction()
        else:
            topic_model = TopicEn()
            introduction_model = IntroductionEn()

        select_categories = []
        if form.cleaned_data.get('categories'):
            category_ids = list(map(int, form.cleaned_data.get('categories')))
            select_categories = Category.get_by_ids(category_ids)

        select_introductions = []
        if form.cleaned_data.get('introductions'):
            title_ids = list(map(int, form.cleaned_data.get('introductions')))
            select_introductions = introduction_model.get_by_ids(title_ids)

        select_topics = []
        if form.cleaned_data.get('topics'):
            topic_ids = list(map(int, form.cleaned_data.get('topics')))
            select_topics = topic_model.get_by_ids(topic_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)

        groups = Group.get_all()

        return TemplateResponse(
            request, 'video_create.html', {
                'title': '新規投稿 | 動画 | FEED App 管理',
                'select_categories': select_categories,
                'select_introductions': select_introductions,
                'select_topics': select_topics,
                'select_videos': select_videos,
                'groups': groups,
                'form_data': form.cleaned_data,
                'error_messages': get_error_message(request),
                'lang': lang,
            })