Esempio n. 1
0
    def post(request, lang, topic_id):

        sid = transaction.savepoint()
        if lang == 'ja':
            form = TopicForm(request.POST)
            topic_model = Topic()
            video_model = Video()
        else:
            form = TopicEnForm(request.POST)
            topic_model = TopicEn()
            video_model = VideoEn()

        topic = topic_model.get_by_id(topic_id)
        if topic is None:
            return HttpResponseRedirect('/{}/admin/topics'.format(lang))

        if form.errors:
            messages.add_message(request, messages.INFO,
                                 dict(form.errors.items()))
        if form.is_valid():
            try:
                topic_model.edit_topic(topic_id, {
                    'new': form.cleaned_data.get('new'),
                    'post_type': form.cleaned_data.get('post_type'),
                    'title': form.cleaned_data.get('title'),
                    'text': form.cleaned_data.get('text'),
                    'thumbnail': form.cleaned_data.get('thumbnail'),
                    'url': form.cleaned_data.get('url'),
                    'button_label': form.cleaned_data.get('button_label'),
                    'event_date': form.cleaned_data.get('event_date'),
                    'published_at': form.cleaned_data.get('published_at'),
                })
                add_videos = form.cleaned_data.get('videos')
                if add_videos:
                    video_model.add_video_from_topic(topic_id, add_videos)
                else:
                    video_model.remove_video_from_topic(topic_id)

                add_images = form.cleaned_data.get('images')
                if add_images:
                    topic_model.add_image(topic_id, add_images)
                else:
                    topic_model.remove_image(topic_id)

                transaction.savepoint_commit(sid)

                return HttpResponseRedirect('/{}/admin/topics'.format(lang))
            except :
                transaction.savepoint_rollback(sid)
                pass

        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)

        select_images = []
        if form.cleaned_data.get('images'):
            image_ids = list(map(int, form.cleaned_data.get('images')))
            select_images = Image.get_by_ids(image_ids)

        return TemplateResponse(
            request, 'topic.html', {
                'title': '新規投稿 | トピックス | FEED App 管理',
                'topic': topic,
                'use_videos': select_videos,
                'use_images': select_images,
                'form_data': form.cleaned_data,
                'error_messages': get_error_message(request),
                'lang': lang,
                'post_types': [
                    {'name': 'トピック', 'val': 'topic'},
                    {'name': 'イベント', 'val': 'event'},
                ]
            })