def _delete_image(album, request): photo_id = request.POST.get('photo_id', '') if not photo_id: return JSONResponse({'error': 'necessario especificar o photo_id'}) photo = get_object_or_404(Photo.objects.get_all_related(album), id=int(photo_id)) photo.delete_files() photo.delete() return JSONResponse({'error': None})
def post(self, request): today = datetime.date.today() is_ex = Report.objects.filter(create_time__startswith=today).exists() if not is_ex: Report.objects.create(user=request.user, weekly_num=time.strftime('%W')) else: return JSONResponse({'code': -1, 'error': u'已经创建过了'}) return JSONResponse({'code': 0})
def _delete_album(request): album_id = request.POST.get('album_id', '') if not album_id: return JSONResponse({'error': 'necessario especificar o album_id'}) album = get_object_or_404(Album, id=album_id) for photo in Photo.objects.get_all_related(album): photo.delete_files() photo.delete() album.delete() return JSONResponse({'error': None})
def json(request, action=None): response = { "action": action, "result": 'failure', "message": '', } if action == 'getData': response['result'] = 'success' response['data'] = [] if int(request.GET['days']) < 1: datalog_set = DataLog.objects.all().order_by('date_logged') else: t = datetime.now() - timedelta(days=int(request.GET['days'])) datalog_set = DataLog.objects.filter( date_logged__gte=t).order_by('date_logged') for datalog in datalog_set: d = {} d['date_logged'] = datalog.date_logged d['temp_ambient'] = datalog.temp_ambient d['temp_tank'] = datalog.temp_tank d['light_ambient'] = datalog.light_ambient response['data'].append(d) elif action == 'getSettings': pass elif action == 'setSettings': pass callback = None if 'callback' in request.GET: callback = request.GET['callback'] return JSONResponse(response, jsonp=callback)
def post(self, request, report_id): report = Report.objects.get(id=report_id) mode = request.POST.get('mode') if mode == 'check': if ReportContent.objects.filter(user=request.user, report=report).exists(): return JSONResponse({'code': -1, 'error': u'已经写过周报了'}) else: return JSONResponse({'code': 0}) elif mode == 'save': contents = request.POST.get('contents') ReportContent.objects.create(user=request.user, report=report, contents=contents) return JSONResponse({'code': 0}) else: return JSONResponse({'code': -4, 'error': u'未知错误'})
def _edit_image(album, request): photo_id = request.POST.get('photo_id', '') name = request.POST.get('name', '') if not photo_id: return JSONResponse({'error': 'necessario especificar o photo_id'}) photo = get_object_or_404(Photo.objects.get_all_related(album), id=int(photo_id)) photo.name = name photo.save() return JSONResponse({ 'error': None, 'id': photo.id, 'name': photo.name, 'url': photo.image.url, 'thumburl': photo.image.thumbnail.url() })
def _send_file(album, request): try: file = request.FILES['file'] photo = Photo(content_object=album) print photo, type(photo.image) photo.image.save(file.name, file) photo.save() except Exception, e: return JSONResponse({'error': str(e)})
def wrapper(request, *args, **kwargs): return JSONResponse(fun(request, *args, **kwargs))
try: file = request.FILES['file'] photo = Photo(content_object=album) print photo, type(photo.image) photo.image.save(file.name, file) photo.save() except Exception, e: return JSONResponse({'error': str(e)}) else: photo = Photo.objects.get(id=photo.id) #fix bug :-| return JSONResponse({ 'error': None, 'id': photo.id, 'name': photo.name, 'url': photo.image.url, 'size': photo.image.size, 'thumbnail': photo.image.thumbnail.url() }) def _edit_image(album, request): photo_id = request.POST.get('photo_id', '') name = request.POST.get('name', '') if not photo_id: return JSONResponse({'error': 'necessario especificar o photo_id'}) photo = get_object_or_404(Photo.objects.get_all_related(album), id=int(photo_id)) photo.name = name
def free_comment(request, content_type=None, object_id=None, edit_id=None, parent_id=None, add_messages=False, ajax=False, model=FreeThreadedComment, form_class=FreeThreadedCommentForm, context_processors=[], extra_context={}): """ Receives POST data and either creates a new ``ThreadedComment`` or ``FreeThreadedComment``, or edits an old one based upon the specified parameters. If there is a 'preview' key in the POST request, a preview will be forced and the comment will not be saved until a 'preview' key is no longer in the POST request. If it is an *AJAX* request (either XML or JSON), it will return a serialized version of the last created ``ThreadedComment`` and there will be no redirect. If invalid POST data is submitted, this will go to the comment preview page where the comment may be edited until it does not contain errors. """ if not edit_id and not (content_type and object_id): raise Http404 # Must specify either content_type and object_id or edit_id if "preview" in request.POST: return _preview(request, context_processors, extra_context, form_class=form_class) if edit_id: instance = get_object_or_404(model, id=edit_id) else: instance = None _adjust_max_comment_length(form_class) form = form_class(request.POST, instance=instance) if form.is_valid(): new_comment = form.save(commit=False) if not edit_id: new_comment.ip_address = request.META.get('REMOTE_ADDR', None) new_comment.content_type = get_object_or_404(ContentType, id=int(content_type)) new_comment.object_id = int(object_id) if model == ThreadedComment: new_comment.user = request.user if parent_id: new_comment.parent = get_object_or_404(model, id=int(parent_id)) new_comment.save() if model == ThreadedComment: if add_messages: request.user.message_set.create( message="Your message has been posted successfully.") else: request.session['successful_data'] = { 'name': form.cleaned_data['name'], 'website': form.cleaned_data['website'], 'email': form.cleaned_data['email'], } if ajax == 'json': return JSONResponse([ new_comment, ]) elif ajax == 'xml': return XMLResponse([ new_comment, ]) else: return HttpResponseRedirect(_get_next(request)) elif ajax == "json": return JSONResponse({'errors': form.errors}, is_iterable=False) elif ajax == "xml": template_str = """ <errorlist> {% for error,name in errors %} <field name="{{ name }}"> {% for suberror in error %}<error>{{ suberror }}</error>{% endfor %} </field> {% endfor %} </errorlist> """ response_str = Template(template_str).render( Context({'errors': zip(form.errors.values(), form.errors.keys())})) return XMLResponse(response_str, is_iterable=False) else: return _preview(request, context_processors, extra_context, form_class=form_class)
def wikipage(request, url): """ Public interface to the flat page view. Models: `flatpages.flatpages` Templates: Uses the template defined by the ``template_name`` field, or `flatpages/default.html` if template_name is not defined. Context: flatpage `flatpages.flatpages` object """ if not url.endswith('/') and settings.APPEND_SLASH: return HttpResponseRedirect("%s/" % request.path) if not url.startswith('/'): url = "/" + url if request.method == "GET": action = request.GET.get('action', '') if not action: action = request.POST.get('action', '') else: action = request.POST.get('action', '') if not action: action = request.GET.get('action', '') print request.method, request.POST, request.GET try: f = Page.objects.get(url__exact=url) except Page.DoesNotExist: f = None if action: if not request.user.is_authenticated(): return redirect_to_login(request.path) else: if not f: if request.user.is_authenticated(): return render_to_response( "wikipages/404.html", locals(), context_instance=RequestContext(request)) raise Http404 return render_wikipage(request, f) if action == 'edit': if request.method == 'POST': if f: form = PageForm(request.POST, request.FILES, instance=f) else: form = PageForm(request.POST, request.FILES) if form.is_valid(): obj = form.save(commit=False) obj.url = url obj.save() return HttpResponseRedirect(url) else: if f: form = PageForm(instance=f) else: form = PageForm() return render_to_response('wikipages/form.html', locals(), context_instance=RequestContext(request)) if action == "send_image": file = request.FILES['file'] contents = file.read() hash = md5(contents).hexdigest() b, ext = splitext(file.name) filename = "%s%s" % (hash, ext) if f is None: f = Page(url=url) f.save() p = Photo(content_object=f) p.image.save(filename, file) p.save() return JSONResponse({'url': p.image.url}) #actions edit, delete raise Http404