Esempio n. 1
0
    def test_create():
        title = Title.create_title({'title': 'test_title'})

        introduction = Introduction.create_introduction({
            'name':
            'introduction_name',
            'text':
            'text',
            'status':
            1,
            'thumbnail':
            'thumbnail',
            'published_at':
            '2018-09-04 14:57',
        })

        Introduction.add_title(introduction.id, [title.id])

        res1 = Video.create_video({
            'published_at': '2018-09-04 14:57',
            'title': 'video_title1',
            'text': 'video_body1',
            'youtube_id': 'youtube_id',
            'pickup': False,
            'status': 1,
        })

        res = Video.create_video({
            'published_at': '2018-09-04 14:57',
            'title': 'video_title2',
            'text': 'video_body2',
            'youtube_id': 'youtube_id',
            'pickup': False,
            'status': 1,
        })

        Video.add_video_from_video(res.id, [res1.id])
        Video.add_introduction(res.id, [introduction.id])

        video = Video.get_published_by_id(res.id)

        topic = Topic.create_topic({
            'title': 'topic_title',
            'text': 'topic_body',
            'status': 1,
            'images': 'https://aaaa.com/aaa.jpg',
            'url': 'https://yahoo.co.jp',
            'button_label': 'ボタン',
            'published_at': '2018-09-04 14:57'
        })

        pprint(
            IntroductionSerializer(Introduction.get_by_id(
                introduction.id)).data)
        pprint(VideoSerializer(video).data)
        pprint(VideosSerializer(Video.get_all(), many=True).data)
        pprint(TopicSerializer(topic).data)
        pprint(TopicsSerializer(Topic.get_all(), many=True).data)

        Introduction.remove_title(introduction.id)
        Video.remove_video_from_introduction(introduction.id)
        Video.remove_video_self(video.id)

        pprint(
            IntroductionSerializer(Introduction.get_by_id(
                introduction.id)).data)
        pprint(VideoSerializer(Video.get_by_id(video.id)).data)
Esempio n. 2
0
    def post(request, lang):

        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()

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

        if form.is_valid():
            try:
                res_topic = topic_model.create_topic({
                    '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'),
                    'event_date': form.cleaned_data.get('event_date'),
                    'button_label': form.cleaned_data.get('button_label'),
                })
                add_videos = form.cleaned_data.get('videos')
                if add_videos:
                    video_model.add_video_from_topic(res_topic.id, add_videos)

                add_images = form.cleaned_data.get('images')
                if add_images:
                    topic_model.add_image(res_topic.id, add_images)

                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_create.html', {
                'title': '新規投稿 | トピックス | FEED App 管理',
                'select_videos': select_videos,
                'select_images': select_images,
                'form_data': form.cleaned_data,
                'error_messages': get_error_message(request),
                'lang': lang,
                'post_types': [
                    {'name': 'トピック', 'val': 'topic'},
                    {'name': 'イベント', 'val': 'event'},
                ]
            })