def get(request, lang, paged=1): search = request.GET.get('search', '') if lang == 'ja': topic_model = Topic() else: topic_model = TopicEn() if search != '': total = topic_model.get_topic_search_all(search).count() else: total = topic_model.get_all().count() pagination = Pagination( page=paged, per_page=10, total=total, query=search, slug='/{}/admin/topics/page/'.format(lang)) if search != '': topics = topic_model.get_topic_search_all(search)[ pagination.offset:pagination.offset + pagination.per_page] else: topics = topic_model.get_all()[ pagination.offset:pagination.offset + pagination.per_page] return TemplateResponse(request, 'topics.html', { 'title': 'トピックス | FEED App 管理', 'topics': topics, 'information': pagination.information(), 'pagination': pagination, 'lang': lang, 'search': search, })
class VideoForm(forms.Form): title = forms.CharField(required=True, error_messages=ERROR_MESSAGES) text = forms.CharField(required=True, error_messages=ERROR_MESSAGES) youtube_id = forms.CharField(required=True, error_messages=ERROR_MESSAGES) published_at = forms.DateTimeField(required=True, error_messages=ERROR_MESSAGES) introductions = forms.MultipleChoiceField( required=False, widget=forms.CheckboxSelectMultiple, choices=lambda: [(v.id, v.id) for v in Introduction.get_all()], error_messages=ERROR_MESSAGES) topics = forms.MultipleChoiceField( required=False, widget=forms.CheckboxSelectMultiple, choices=lambda: [(v.id, v.id) for v in Topic.get_all()], error_messages=ERROR_MESSAGES) categories = forms.MultipleChoiceField( required=False, widget=forms.CheckboxSelectMultiple, choices=lambda: [(v.id, v.id) for v in Category.get_all()], error_messages=ERROR_MESSAGES) videos = forms.MultipleChoiceField( required=False, widget=forms.CheckboxSelectMultiple, choices=lambda: [(v.id, v.id) for v in Video.get_all()], error_messages=ERROR_MESSAGES)
def remove_video_self(cls, video_id): all_topic_ids = [v.id for v in Topic.get_all()] try: video = cls.objects.get(id=video_id) for v in all_topic_ids: video.video.remove(v) except: pass
def get(request, lang, paged=1): try: value = request.GET.get('value') if lang == 'ja': topic_model = Topic() else: topic_model = TopicEn() if value != '': total = topic_model.get_topic_search_all(value).count() else: total = topic_model.get_all().count() pagination = Pagination( page=paged, per_page=10, total=total, slug='') if value != '': topics = topic_model.get_topic_search_all(value)[ pagination.offset:pagination.offset + pagination.per_page] else: topics = topic_model.get_all()[ pagination.offset:pagination.offset + pagination.per_page] if lang == 'ja': res = TopicsSerializer(topics, many=True).data else: res = TopicsEnSerializer(topics, many=True).data return JsonResponse({ 'total': pagination.pages, 'paged': paged, 'topics': res, }, safe=False) except Exception as e: return JsonResponse({ 'status': 500, 'message': 'Exception Error ' + str(e) }, status=500)
def add_topic(cls, video_id, topic_ids): topic_ids = list(map(int, topic_ids)) all_topic_ids = [v.id for v in Topic.get_all()] try: video = cls.objects.get(id=video_id) for v in all_topic_ids: if v in topic_ids: try: video.topic.add(v) except: pass else: video.topic.remove(v) except: pass
def get(request, lang): if lang == 'ja': introduction_model = Introduction() video_model = Video() topic_model = Topic() else: introduction_model = IntroductionEn() video_model = VideoEn() topic_model = TopicEn() introductions = introduction_model.get_all()[:3] videos = video_model.get_all()[:3] topics = topic_model.get_all()[:3] return TemplateResponse(request, 'top.html', { 'title': 'FEED App 管理', 'introductions': introductions, 'videos': videos, 'topics': topics, 'lang': lang, })
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)