def post(request, lang): image_model = Image() file = request.FILES.get('image_file') if file is None: return JsonResponse({ 'status': 500, 'message': 'NotRegister'}, status=500) status, image_data = set_image_upload(file) if status == 500: return JsonResponse({ 'status': 500, 'message': 'NotRegister'}, status=500) try: image_model.create_image({ 'title': image_data.get('title'), 'image_id': image_data.get('image_id'), 'image_url': image_data.get('image_url'), 'lang': lang, }) return JsonResponse({ 'status': 200, 'image_id': image_data.get('image_id'), 'image_url': image_data.get('image_url'), 'message': 'Success' }, status=200) except: return JsonResponse({ 'status': 500, 'message': 'NotRegister'}, status=500)
def get(_, lang, paged=1): try: image_model = Image() total = image_model.get_all().count() pagination = Pagination( page=paged, per_page=10, total=total, slug='') images = image_model.get_all()[ pagination.offset:pagination.offset + pagination.per_page] res = ImagesSerializer(images, many=True).data return JsonResponse({ 'total': pagination.pages, 'paged': paged, 'images': res, 'lang': lang, }, safe=False) except Exception as e: return JsonResponse({ 'status': 500, 'message': 'Exception Error ' + str(e) }, status=500)
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 get(_, lang, image_id): sid = transaction.savepoint() image_model = Image() try: delete_resources([image_id]) image_model.delete_image(image_id) transaction.savepoint_commit(sid) except: transaction.savepoint_rollback(sid) pass return HttpResponseRedirect('/{}/admin/images'.format(lang))
def remove_image(cls, topic_id): all_image_ids = [v.id for v in Image.get_all()] try: topic = cls.objects.get(id=topic_id) for v in all_image_ids: topic.images.remove(v) except: pass
def get(request, lang, paged=1): image_model = Image() total = image_model.get_all().count() pagination = Pagination( page=paged, per_page=10, total=total, slug='/{}/admin/images/page/'.format(lang)) images = image_model.get_all()[ pagination.offset:pagination.offset + pagination.per_page] return TemplateResponse(request, 'images.html', { 'title': '画像一覧 | FEED App 管理', 'images': images, 'information': pagination.information(), 'pagination': pagination, 'lang': lang, })
def add_image(cls, topic_id, image_ids): image_ids = list(map(int, image_ids)) all_image_ids = [v.id for v in Image.get_all()] try: topic = cls.objects.get(id=topic_id) for v in all_image_ids: if v in image_ids: try: topic.images.add(v) except: pass else: topic.images.remove(v) except: pass
class TopicEnForm(forms.Form): post_type = forms.CharField(required=True, error_messages=ERROR_MESSAGES) new = forms.BooleanField(required=False, error_messages=ERROR_MESSAGES) title = forms.CharField(required=True, error_messages=ERROR_MESSAGES) text = forms.CharField(required=True, error_messages=ERROR_MESSAGES) url = forms.CharField(required=True, error_messages=ERROR_MESSAGES) button_label = forms.CharField(required=True, error_messages=ERROR_MESSAGES) published_at = forms.DateTimeField(required=True, error_messages=ERROR_MESSAGES) event_date = forms.DateTimeField(required=False, error_messages=ERROR_MESSAGES) thumbnail = forms.CharField(required=True, error_messages=ERROR_MESSAGES) images = forms.MultipleChoiceField( required=False, widget=forms.CheckboxSelectMultiple, choices=lambda: [(v.id, v.id) for v in Image.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): 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'}, ] })