class TitleEnForm(forms.Form): title = forms.CharField(required=True, error_messages=ERROR_MESSAGES) introductions = forms.MultipleChoiceField( required=False, widget=forms.CheckboxSelectMultiple, choices=lambda: [(v.id, v.id) for v in IntroductionEn.get_all()], error_messages=ERROR_MESSAGES)
def get(request, lang, introduction_id): if lang == 'ja': introduction_model = Introduction() else: introduction_model = IntroductionEn() introduction = introduction_model.get_by_id(introduction_id) if introduction is None: raise Http404 if lang == 'ja': use_videos = introduction.video_set.all().order_by( 'video_introduction.id') use_titles = introduction.titles.all().order_by( 'introduction_titles.id') else: use_videos = introduction.videoen_set.all().order_by( 'video_en_introduction.id') use_titles = introduction.titles.all().order_by( 'introduction_en_titles.id') title = introduction.name return TemplateResponse( request, 'introduction.html', { 'title': title + ' | イントロダクション | FEED App 管理', 'introduction': introduction, 'use_videos': use_videos, 'use_titles': use_titles, 'form_data': {}, 'error_messages': {}, 'lang': lang, })
class VideoEnForm(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 IntroductionEn.get_all()], error_messages=ERROR_MESSAGES) topics = forms.MultipleChoiceField( required=False, widget=forms.CheckboxSelectMultiple, choices=lambda: [(v.id, v.id) for v in TopicEn.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 VideoEn.get_all()], error_messages=ERROR_MESSAGES)
def post(request, lang, title_id): sid = transaction.savepoint() if lang == 'ja': form = TitleForm(request.POST) title_model = Title() introduction_model = Introduction() else: form = TitleEnForm(request.POST) title_model = TitleEn() introduction_model = IntroductionEn() title_post = title_model.get_by_id(title_id) if title_post is None: return HttpResponseRedirect('/{}/admin/titles'.format(lang)) if form.errors: messages.add_message(request, messages.INFO, dict(form.errors.items())) if form.is_valid(): try: title_model.edit_title(title_id, { 'title': form.cleaned_data.get('title'), }) add_introductions = form.cleaned_data.get('introductions') if add_introductions: Introduction.add_introduction_from_title( title_id, add_introductions) else: Introduction.remove_introduction_from_title(title_id) transaction.savepoint_commit(sid) return HttpResponseRedirect('/{}/admin/titles'.format(lang)) except: transaction.savepoint_rollback(sid) pass if form.cleaned_data.get('introductions'): introductions_ids = list( map(int, form.cleaned_data.get('introductions'))) use_introductions = introduction_model.get_by_ids( introductions_ids) else: use_introductions = title_post.introduction_set.all() return TemplateResponse( request, 'title.html', { 'title': '新規投稿 | タイトル | FEED App 管理', 'title_post': title_post, 'use_introductions': use_introductions, 'form_data': form.cleaned_data, 'error_messages': get_error_message(request), 'lang': lang, })
def get(_, lang, image_id): delete_flag = True image_model = Image() category_model = Category() if lang == 'ja': topic_model = Topic() introduction_model = Introduction() else: topic_model = TopicEn() introduction_model = IntroductionEn() try: image = image_model.get_by_image_id(image_id) topic_use_flag, topic_use_posts = topic_model.is_use_image( image.image_url, image.id) if topic_use_flag is True: delete_flag = False introduction_use_flag, introduction_use_posts = \ introduction_model.is_use_image(image.image_url) if introduction_use_flag is True: delete_flag = False category_use_flag, category_use_posts = category_model.is_use_image( image.image_url) if category_use_flag is True: delete_flag = False use_posts = ( topic_use_posts + category_use_posts + introduction_use_posts ) def get_unique_list(seq): seen = [] return [x for x in seq if x not in seen and not seen.append(x)] use_posts = get_unique_list(use_posts) return JsonResponse({ 'status': 200, 'delete_flag': delete_flag, 'use_posts': use_posts, }, status=200) except Exception as e: return JsonResponse({ 'status': 500, 'message': 'Exception Error ' + str(e) }, status=500)
def remove_introduction(cls, video_id): all_introduction_ids = [v.id for v in IntroductionEn.get_all()] try: video = cls.objects.get(id=video_id) for v in all_introduction_ids: video.introduction.remove(v) except: pass
def post(request, lang): sid = transaction.savepoint() if lang == 'ja': form = TitleForm(request.POST) title_model = Title() introduction_model = Introduction() else: form = TitleEnForm(request.POST) title_model = TitleEn() introduction_model = IntroductionEn() if form.errors: messages.add_message(request, messages.INFO, dict(form.errors.items())) if form.is_valid(): try: res_title = title_model.create_title({ 'title': form.cleaned_data.get('title'), }) add_introductions = form.cleaned_data.get('introductions') if add_introductions: introduction_model.add_title(res_title.id, add_introductions) transaction.savepoint_commit(sid) return HttpResponseRedirect('/{}/admin/titles'.format(lang)) except: transaction.savepoint_rollback(sid) pass select_introductions = [] if form.cleaned_data.get('introductions'): introductions_ids = list( map(int, form.cleaned_data.get('introductions'))) select_introductions = introduction_model.get_by_ids( introductions_ids) return TemplateResponse( request, 'title_create.html', { 'title': '新規投稿 | タイトル | FEED App 管理', 'select_introductions': select_introductions, 'form_data': form.cleaned_data, 'error_messages': get_error_message(request), 'lang': lang, })
def add_introduction(cls, video_id, introduction_ids): introduction_ids = list(map(int, introduction_ids)) all_introduction_ids = [v.id for v in IntroductionEn.get_all()] try: video = cls.objects.get(id=video_id) for v in all_introduction_ids: if v in introduction_ids: try: video.introduction.add(v) except: pass else: video.introduction.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 get(request, lang, paged=1): try: value = request.GET.get('value') if lang == 'ja': introduction_model = Introduction() else: introduction_model = IntroductionEn() if value != '': total = introduction_model.get_search_all(value).count() else: total = introduction_model.get_all().count() pagination = Pagination( page=paged, per_page=10, total=total, slug='') if value != '': introductions = introduction_model.get_search_all(value)[ pagination.offset:pagination.offset + pagination.per_page] else: introductions = introduction_model.get_all()[ pagination.offset:pagination.offset + pagination.per_page] if lang == 'ja': res = IntroductionsSerializer(introductions, many=True).data else: res = IntroductionsEnSerializer(introductions, many=True).data return JsonResponse({ 'total': pagination.pages, 'paged': paged, 'introductions': res, }, safe=False) except Exception as e: return JsonResponse({ 'status': 500, 'message': 'Exception Error ' + str(e) }, status=500)
def get(request, lang, paged=1): search = request.GET.get('search', '') if lang == 'ja': introduction_model = Introduction() else: introduction_model = IntroductionEn() if search != '': total = introduction_model.get_search_all(search).count() else: total = introduction_model.get_all().count() pagination = Pagination( page=paged, per_page=10, total=total, query=search, slug='/{}/admin/introductions/page/'.format(lang)) if search != '': introductions = introduction_model.get_search_all( search)[pagination.offset:pagination.offset + pagination.per_page] else: introductions = introduction_model.get_all( )[pagination.offset:pagination.offset + pagination.per_page] return TemplateResponse( request, 'introductions.html', { 'title': 'イントロダクション | FEED App 管理', 'introductions': introductions, 'information': pagination.information(), 'pagination': pagination, 'lang': lang, 'search': search, })
def post(request, lang, introduction_id): if lang == 'ja': introduction_model = Introduction() else: introduction_model = IntroductionEn() introduction = introduction_model.get_by_id(introduction_id) if introduction is None: return JsonResponse({ 'status': 503, 'message': '投稿が存在しません' }, status=503) form = StatusForm(request.POST) if form.errors: messages.add_message(request, messages.INFO, dict(form.errors.items())) if form.is_valid(): try: introduction_model.status_change( form.cleaned_data.get('status'), introduction_id) except: return JsonResponse({ 'status': 500, 'message': 'Not Delete' }, status=500) else: return JsonResponse( { 'status': 500, 'message': get_error_message(request) }, status=500) return JsonResponse({'status': 200, 'message': 'Changed'}, status=200)
def post(_, lang, title_id): sid = transaction.savepoint() if lang == 'ja': title_model = Title() introduction_model = Introduction() else: title_model = TitleEn() introduction_model = IntroductionEn() try: introduction_model.remove_introduction_from_title(title_id) title_model.delete_title(title_id) transaction.savepoint_commit(sid) except: transaction.savepoint_rollback(sid) pass return HttpResponseRedirect('/{}/admin/titles'.format(lang))
def post(_, lang, introduction_id): sid = transaction.savepoint() if lang == 'ja': introduction_model = Introduction() video_model = Video() else: introduction_model = IntroductionEn() video_model = VideoEn() try: video_model.remove_video(introduction_id) introduction_model.remove_title(introduction_id) introduction_model.delete_introduction(introduction_id) transaction.savepoint_commit(sid) except: transaction.savepoint_rollback(sid) pass return HttpResponseRedirect('/{}/admin/introductions'.format(lang))
def get(_, lang, introduction_id): if lang == 'ja': cached_introduction = Cache.get('api_introduction_' + str(introduction_id)) if cached_introduction is None: introduction = Introduction.get_published_by_id( introduction_id) if introduction is None: return JsonResponse({'message': 'Not Found'}, status=404) res = IntroductionSerializer(introduction).data Cache.set('api_introduction_' + str(introduction_id), res) else: res = cached_introduction elif lang == 'en': try: cached_introduction_en = Cache.get('api_introduction_en_' + str(introduction_id)) except: cached_introduction_en = None if cached_introduction_en is None: introduction = IntroductionEn.get_published_by_id( introduction_id) if introduction is None: return JsonResponse({'message': 'Not Found'}, status=404) res = IntroductionEnSerializer(introduction).data Cache.set('api_introduction_en_' + str(introduction_id), res) else: res = cached_introduction_en else: return JsonResponse({'message': 'Not Found'}, status=404) return JsonResponse(res, safe=False)
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, })
def post(request, lang): sid = transaction.savepoint() if lang == 'ja': form = IntroductionForm(request.POST) introduction_model = Introduction() video_model = Video() title_model = Title() else: form = IntroductionEnForm(request.POST) introduction_model = IntroductionEn() video_model = VideoEn() title_model = TitleEn() if form.errors: messages.add_message(request, messages.INFO, dict(form.errors.items())) if form.is_valid(): try: res_introduction = introduction_model.create_introduction({ 'name': form.cleaned_data.get('name'), 'text': form.cleaned_data.get('text'), 'thumbnail': form.cleaned_data.get('thumbnail'), }) add_videos = form.cleaned_data.get('videos') if add_videos: video_model.add_video_from_introduction( res_introduction.id, add_videos) add_titles = form.cleaned_data.get('titles') if add_titles: introduction_model.add_title(res_introduction.id, add_titles) transaction.savepoint_commit(sid) return HttpResponseRedirect( '/{}/admin/introductions'.format(lang)) except: transaction.savepoint_rollback(sid) pass select_titles = [] if form.cleaned_data.get('titles'): title_ids = list(map(int, form.cleaned_data.get('titles'))) select_titles = title_model.get_by_ids(title_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) return TemplateResponse( request, 'introduction_create.html', { 'title': '新規投稿 | イントロダクション | FEED App 管理', 'select_titles': select_titles, 'select_videos': select_videos, 'form_data': form.cleaned_data, 'error_messages': get_error_message(request), 'lang': lang, })