def post(request, lang, topic_id): if lang == 'ja': topic_model = Topic() else: topic_model = TopicEn() topic = topic_model.get_by_id(topic_id) if topic is None: return JsonResponse({ 'status': 503, 'message': '投稿が存在しません'}, status=503) form = NewForm(request.POST) if form.errors: messages.add_message(request, messages.INFO, dict(form.errors.items())) if form.is_valid(): try: topic_model.new_change( form.cleaned_data.get('new'), topic_id) except: return JsonResponse({ 'status': 500, 'message': 'Not Change'}, status=500) else: return JsonResponse({ 'status': 500, 'message': get_error_message(request)}, status=500) return JsonResponse({ 'status': 200, 'message': 'Changed'}, status=200)
def get(request, lang, topic_id): if lang == 'ja': topic_model = Topic() else: topic_model = TopicEn() topic = topic_model.get_by_id(topic_id) if topic is None: raise Http404 title = topic.title if lang == 'ja': use_videos = topic.video_set.all().order_by('video_topic.id') use_images = topic.images.all().order_by('topic_images.id') else: use_videos = topic.videoen_set.all().order_by('video_en_topic.id') use_images = topic.images.all().order_by('topic_en_images.id') return TemplateResponse(request, 'topic.html', { 'title': title + ' | トピックス | FEED App 管理', 'topic': topic, 'use_videos': use_videos, 'use_images': use_images, 'error_messages': {}, 'form_data': {}, 'lang': lang, 'post_types': [ {'name': 'トピック', 'val': 'topic'}, {'name': 'イベント', 'val': 'event'}, ] })
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'}, ] })