Exemplo 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)
Exemplo n.º 2
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,
            })