def upload_doc_api(request): user = request.user if request.user.is_authenticated else None if user is None: return HttpResponse(_('You must be logged to upload files.\n'), status=401) elif request.method != 'POST': return HttpResponse(_('Only POST requests are allowed.\n'), status=400) form = UploadApiForm(request.GET) if not form.is_valid(): return HttpResponse(_('You must supply filename, name and keywords in your query.\n'), status=400) tmp_file = tempfile.NamedTemporaryFile(mode='wb', dir=settings.FILE_UPLOAD_TEMP_DIR, delete=False) c = False chunk = request.read(32768) while chunk: tmp_file.write(chunk) c = True chunk = request.read(32768) tmp_file.flush() if not c: os.remove(tmp_file.name) return HttpResponse(_('Empty file. You must POST a valid file.\n'), status=400) # ok, we have the tmp file existing_objs = list(UploadDoc.query(request).filter(name=form.cleaned_data['name'])[0:1]) if existing_objs: doc = existing_objs[0] doc.keywords.clear() else: doc = UploadDoc(uid=str(uuid.uuid1()), name=form.cleaned_data['name'], user=user) doc.save() for keyword in strip_split(form.cleaned_data['keywords'].lower()): doc.keywords.add(Keyword.get(keyword)) scall(request, 'updoc.process_file', to=[SERVER], doc_id=doc.id, filename=tmp_file.name, original_filename=os.path.basename(form.cleaned_data['filename'])) return HttpResponse(_('File successfully uploaded. It will be uncompressed and indexed.\n'), status=200)
def chat_receive(window_info, content=""): matcher = re.match(r"^@([\w\-_]+).*", content) if matcher: dest = "chat-%s" % matcher.group(1) else: dest = BROADCAST scall(window_info, "demo.chat.send", to=[WINDOW, dest], content=content)
def chat_receive(window_info, content=""): matcher = re.match(r"^@([\w\-_]+).*", content) if matcher: dest = "chat-%s" % matcher.group(1) else: dest = BROADCAST scall(window_info, "demo.chat.send", to=[WINDOW, dest], content=content)
def renew_csrf(window_info): if not window_info.csrf_cookie: csrf_secret = _get_new_csrf_string() window_info.csrf_cookie = _salt_cipher_secret(csrf_secret) else: csrf_secret = _unsalt_cipher_token(window_info.csrf_cookie) value = _salt_cipher_secret(csrf_secret) scall(window_info, "df.validate.update_csrf", to=[WINDOW], value=value)
def renew_csrf(window_info): if not window_info.csrf_cookie: csrf_secret = _get_new_csrf_string() window_info.csrf_cookie = _salt_cipher_secret(csrf_secret) else: csrf_secret = _unsalt_cipher_token(window_info.csrf_cookie) value = _salt_cipher_secret(csrf_secret) scall(window_info, "df.validate.update_csrf", to=[WINDOW], value=value)
def process_file(window_info: WindowInfo, doc_id: int): """ :param window_info: :param doc_id: """ for doc in UploadDoc.objects.filter(id=doc_id): doc.delete() content = _('%(name)s has been deleted') % {'name': doc.name} notify(window_info, content, to=[USER], style=NOTIFICATION, level=INFO, timeout=7000) scall(window_info, 'updoc.delete_doc_info', to=[BROADCAST], doc_id=doc_id)
def print_sig2(window_info, content=""): scall( window_info, "df.notify", to=[BROADCAST, SERVER], content="Server notification that causes an error in the Celery queue" "[with some special chars for checking encoding: éà] [%r]" % content, level="warning", timeout=2, style="notification", ) # noinspection PyStatementEffect 1 / 0
def print_sig2(window_info, content=""): scall( window_info, "df.notify", to=[BROADCAST, SERVER], content="Server notification that causes an error in the Celery queue" "[with some special chars for checking encoding: éà] [%r]" % content, level="warning", timeout=2, style="notification", ) # noinspection PyStatementEffect 1 / 0
def val(window_info, selector, value, to=WINDOW): """Set the value of every matched element.""" return scall(window_info, "html.val", to=to, selector=selector, value=value)
def trigger(window_info, selector, event, to=WINDOW): """Execute all handlers and behaviors attached to the matched elements for the given event type.""" return scall(window_info, "html.replace_with", to=to, selector=selector, event=event)
def text(window_info, selector, text_content, to=WINDOW): """Set the text contents of the matched elements.""" return scall(window_info, "html.text", to=to, selector=selector, content=text_content)
def replace_with(window_info, selector, html_content, to=WINDOW): """Replace each element in the set of matched elements with the provided new content.""" return scall(window_info, "html.replace_with", to=to, selector=selector, content=html_content)
def remove_attr(window_info, selector, attr_name, to=WINDOW): """Remove an attribute from each element in the set of matched elements.""" return scall(window_info, "html.remove_attr", to=to, selector=selector, attr_name=attr_name)
def download_file(window_info, url, filename=None, to=WINDOW): """Force the client to download the given file.""" return scall(window_info, "html.download_file", to=to, url=url, filename=filename)
def fade_out(window_info, selector, duration=400, to=WINDOW): """Hide the matched elements by fading them to transparent.""" return scall(window_info, "html.fade_out", to=to, selector=selector, duration=duration)
def add(window_info, selector, html_content, to=WINDOW): """Create a new JS object (with jQuery) with elements added to the set of matched elements.""" return scall(window_info, "html.add", to=to, selector=selector, content=html_content)
def notify( window_info, content, title=None, timeout=5000, style=NOTIFICATION, level=INFO, to=WINDOW, ): """Display a notification to the selected users. Can be a banner (on the top of the page), a Growl-like notification (bubble on the top-right), a modal or a system notification. :param window_info: a :class:`djangofloor.wsgi.window_info.WindowInfo` object :param content: content of the notification :param title: title of the notification :param timeout: number of milliseconds before hiding the message :param style: one of `djangofloor.signals.bootstrap3.{NOTIFICATION,BANNER,MODAL,SYSTEM}` :param level: one of `djangofloor.signals.bootstrap3.{INFO,DEFAULT,SUCCESS,DANGER,WARNING}` :param to: list of signal clients """ return scall( window_info, "df.notify", to=to, content=content and str(content) or None, title=title and str(title) or None, timeout=timeout, style=style, level=level, )
def before(window_info, selector, html_content, to=WINDOW): """Insert content, specified by the parameter, before each element in the set of matched elements.""" return scall(window_info, "html.before", to=to, selector=selector, content=html_content)
def append(window_info, selector, html_content, to=WINDOW): """Insert content, specified by the parameter, to the end of each element in the set of matched elements.""" return scall(window_info, "html.append", to=to, selector=selector, content=html_content)
def add_class(window_info, selector, class_name, to=WINDOW): """Adds the specified class(es) to each of the set of matched elements.""" return scall(window_info, "html.add_class", to=to, selector=selector, class_name=class_name)
def fade_in(window_info, selector, duration=400, to=WINDOW): """Display the matched elements by fading them to opaque.""" return scall(window_info, "html.fade_in", to=to, selector=selector, duration=duration)
def remove_class(window_info, selector, class_name, to=WINDOW): """Remove a single class, multiple classes, or all classes from each element in the set of matched elements.""" return scall( window_info, "html.remove_class", to=to, selector=selector, class_name=class_name, )
def remove_class(window_info, selector, class_name, to=WINDOW): """Remove a single class, multiple classes, or all classes from each element in the set of matched elements.""" return scall( window_info, "html.remove_class", to=to, selector=selector, class_name=class_name, )
def toggle(window_info, selector, duration=400, easing="swing", to=WINDOW): """Display or hide the matched elements.""" return scall( window_info, "html.toggle", to=to, selector=selector, duration=duration, easing=easing, )
def show(window_info, selector, duration=400, easing="swing", to=WINDOW): """Display the matched elements.""" return scall( window_info, "html.show", to=to, selector=selector, duration=duration, easing=easing, )
def show(window_info, selector, duration=400, easing="swing", to=WINDOW): """Display the matched elements.""" return scall( window_info, "html.show", to=to, selector=selector, duration=duration, easing=easing, )
def hide(window_info, selector, duration=400, easing="swing", to=WINDOW): """Hide the matched elements.""" return scall( window_info, "html.hide", to=to, selector=selector, duration=duration, easing=easing, )
def print_sig1(window_info, content=""): logger.debug("Debug log message [%r]" % content) logger.info("Debug info message [%r]" % content) logger.warning("Debug warn message [%r]" % content) logger.error("Debug error message [%r]" % content) logger2.debug("Debug log message / logger2 [%r]" % content) logger2.info("Debug info message / logger2 [%r]" % content) logger2.warning("Debug warn message / logger2 [%r]" % content) logger2.error("Debug error message / logger2 [%r]" % content) scall( window_info, "df.notify", to=[BROADCAST, SERVER], content="Some lines should be added in the Celery log files", level="success", timeout=2, style="notification", ) scall(window_info, "demo.print_sig2", to=[BROADCAST, SERVER], content=content)
def add_attribute(window_info, selector, attr_name, attr_value, to=WINDOW): """Adds the specified attribute(s) to each of the set of matched elements.""" return scall( window_info, "html.add_attribute", to=to, selector=selector, attr_name=attr_name, attr_value=attr_value, )
def hide(window_info, selector, duration=400, easing="swing", to=WINDOW): """Hide the matched elements.""" return scall( window_info, "html.hide", to=to, selector=selector, duration=duration, easing=easing, )
def add_attribute(window_info, selector, attr_name, attr_value, to=WINDOW): """Adds the specified attribute(s) to each of the set of matched elements.""" return scall( window_info, "html.add_attribute", to=to, selector=selector, attr_name=attr_name, attr_value=attr_value, )
def toggle(window_info, selector, duration=400, easing="swing", to=WINDOW): """Display or hide the matched elements.""" return scall( window_info, "html.toggle", to=to, selector=selector, duration=duration, easing=easing, )
def content(window_info, selector, html_content, to=WINDOW): """set the HTML contents of every matched element. :param window_info: a :class:`djangofloor.wsgi.window_info.WindowInfo` object :param html_content: HTML content sent to the client :param selector: jQuery selector (like `"#my_div"`) :param to: list of signal clients """ return scall( window_info, "html.content", to=to, selector=selector, content=html_content )
def content(window_info, selector, html_content, to=WINDOW): """set the HTML contents of every matched element. :param window_info: a :class:`djangofloor.wsgi.window_info.WindowInfo` object :param html_content: HTML content sent to the client :param selector: jQuery selector (like `"#my_div"`) :param to: list of signal clients """ return scall(window_info, "html.content", to=to, selector=selector, content=html_content)
def slow_signal(window_info, content=""): logger.warning( "wait for 10 seconds [with some special chars to check the encoding: éà]…" ) scall( window_info, "df.notify", to=[BROADCAST, SERVER], content="A second message should be display in ten seconds.", level="success", timeout=2, style="notification", ) time.sleep(10) logger.warning("10 seconds: done.") scall( window_info, "demo.print_sig2", to=[BROADCAST, SERVER], content="This message is sent by a dedicated Celery queue" " [with some special chars to check the encoding: éà]", )
def print_sig1(window_info, content=""): logger.debug("Debug log message [%r]" % content) logger.info("Debug info message [%r]" % content) logger.warning("Debug warn message [%r]" % content) logger.error("Debug error message [%r]" % content) logger2.debug("Debug log message / logger2 [%r]" % content) logger2.info("Debug info message / logger2 [%r]" % content) logger2.warning("Debug warn message / logger2 [%r]" % content) logger2.error("Debug error message / logger2 [%r]" % content) scall( window_info, "df.notify", to=[BROADCAST, SERVER], content="Some lines should be added in the Celery log files", level="success", timeout=2, style="notification", ) scall(window_info, "demo.print_sig2", to=[BROADCAST, SERVER], content=content)
def slow_signal(window_info, content=""): logger.warning( "wait for 10 seconds [with some special chars to check the encoding: éà]…" ) scall( window_info, "df.notify", to=[BROADCAST, SERVER], content="A second message should be display in ten seconds.", level="success", timeout=2, style="notification", ) time.sleep(10) logger.warning("10 seconds: done.") scall( window_info, "demo.print_sig2", to=[BROADCAST, SERVER], content="This message is sent by a dedicated Celery queue" " [with some special chars to check the encoding: éà]", )
def render_to_modal( window_info, template_name, context, width=None, to=WINDOW, using=None ): """ render a template and display the result in a modal window on every selected clients :param window_info: a :class:`djangofloor.wsgi.window_info.WindowInfo` object :param template_name: name of the Django template :param context: template context (a dict) :param width: width of the modal, in pixels :param to: list of signal clients :param using: the `using` parameter of Django templates """ html_content = render_to_string(template_name, context=context, using=using) return scall(window_info, "df.modal.show", to=to, width=width, html=html_content)
def upload_doc_progress(request): form = FileUploadForm(request.POST, request.FILES) if not form.is_valid(): raise PermissionDenied uploaded_file = request.FILES['file'] tmp_file = tempfile.NamedTemporaryFile(mode='wb', dir=settings.FILE_UPLOAD_TEMP_DIR, delete=False) chunk = uploaded_file.read(16384) while chunk: tmp_file.write(chunk) chunk = uploaded_file.read(16384) tmp_file.flush() basename = os.path.basename(uploaded_file.name).rpartition('.')[0] if basename.endswith('.tar'): basename = basename[:-4] doc = UploadDoc(name=basename, user=request.user if request.user.is_authenticated else None, uid=str(uuid.uuid1())) doc.save() scall(request, 'updoc.process_file', to=[SERVER], doc_id=doc.id, filename=tmp_file.name, original_filename=uploaded_file.name) # offer a correct name for the newly uploaded document form = MetadatadUploadForm(initial={'pk': doc.pk, 'name': basename, }) template_values = {'form': form, } return TemplateResponse(request, 'updoc/upload_doc_progress.html', template_values)
def render_to_client( window_info, template_name, context, selector, to=WINDOW, using=None ): """ render a template and send the result inside a HTML selector on every selected clients :param window_info: a :class:`djangofloor.wsgi.window_info.WindowInfo` object :param template_name: name of the Django template :param context: template context (a dict) :param selector: jQuery selector (like `"#my_div"`) :param to: list of signal clients :param using: the `using` parameter of Django templates """ html_content = render_to_string(template_name, context=context, using=using) return scall( window_info, "html.content", to=to, selector=selector, content=html_content )
def fade_to(window_info, selector, opacity, duration=400, to=WINDOW): """ Adjust the opacity of the matched elements. :param window_info: :class:`djangofloor.wsgi.window_info.WindowInfo` :param selector: jQuery selector :param duration: determining how long the animation will run (ms). :param opacity: A number between 0 and 1 denoting the target opacity. :param to: target of the signal """ return scall( window_info, "html.fade_to", to=to, selector=selector, duration=duration, opacity=opacity, )
def fade_toggle(window_info, selector, duration=400, easing="swing", to=WINDOW): """ Display or hide the matched elements by animating their opacity. :param window_info: :class:`djangofloor.wsgi.window_info.WindowInfo` :param selector: jQuery selector :param duration: determining how long the animation will run (ms). :param easing: A string indicating which easing function to use for the transition. :param to: target of the signal """ return scall( window_info, "html.fade_toggle", to=to, selector=selector, duration=duration, easing=easing, )
def fade_to(window_info, selector, opacity, duration=400, to=WINDOW): """ Adjust the opacity of the matched elements. :param window_info: :class:`djangofloor.wsgi.window_info.WindowInfo` :param selector: jQuery selector :param duration: determining how long the animation will run (ms). :param opacity: A number between 0 and 1 denoting the target opacity. :param to: target of the signal """ return scall( window_info, "html.fade_to", to=to, selector=selector, duration=duration, opacity=opacity, )
def fade_toggle(window_info, selector, duration=400, easing="swing", to=WINDOW): """ Display or hide the matched elements by animating their opacity. :param window_info: :class:`djangofloor.wsgi.window_info.WindowInfo` :param selector: jQuery selector :param duration: determining how long the animation will run (ms). :param easing: A string indicating which easing function to use for the transition. :param to: target of the signal """ return scall( window_info, "html.fade_toggle", to=to, selector=selector, duration=duration, easing=easing, )
def render_to_client(window_info, template_name, context, selector, to=WINDOW, using=None): """ render a template and send the result inside a HTML selector on every selected clients :param window_info: a :class:`djangofloor.wsgi.window_info.WindowInfo` object :param template_name: name of the Django template :param context: template context (a dict) :param selector: jQuery selector (like `"#my_div"`) :param to: list of signal clients :param using: the `using` parameter of Django templates """ html_content = render_to_string(template_name, context=context, using=using) return scall(window_info, "html.content", to=to, selector=selector, content=html_content)
def check_websockets(window_info): """Used for checking if websockets are functional or not for your installation. Called from the monitoring view.""" logger.info("websocket OK") scall(window_info, "df.monitoring.checked_ws", to=[WINDOW])
def trigger(window_info, selector, event, to=WINDOW): """Execute all handlers and behaviors attached to the matched elements for the given event type.""" return scall( window_info, "html.replace_with", to=to, selector=selector, event=event )
def remove_attr(window_info, selector, attr_name, to=WINDOW): """Remove an attribute from each element in the set of matched elements.""" return scall( window_info, "html.remove_attr", to=to, selector=selector, attr_name=attr_name )
def delete_doc(request, doc_id): obj = get_object_or_404(UploadDoc.query(request), id=doc_id) name = obj.name scall(request, 'updoc.delete_file', to=[SERVER], doc_id=doc_id) messages.info(request, _('%(doc)s will be quickly deleted') % {'doc': name}) return HttpResponseRedirect(reverse('updoc:my_docs'))
def after(window_info, selector, html_content, to=WINDOW): """Insert content, specified by the parameter, before after element in the set of matched elements.""" return scall( window_info, "html.after", to=to, selector=selector, content=html_content )
def add(window_info, selector, html_content, to=WINDOW): """Create a new JS object (with jQuery) with elements added to the set of matched elements.""" return scall( window_info, "html.add", to=to, selector=selector, content=html_content )
def replace_with(window_info, selector, html_content, to=WINDOW): """Replace each element in the set of matched elements with the provided new content.""" return scall( window_info, "html.replace_with", to=to, selector=selector, content=html_content )
def text(window_info, selector, text_content, to=WINDOW): """Set the text contents of the matched elements.""" return scall( window_info, "html.text", to=to, selector=selector, content=text_content )
def empty(window_info, selector, to=WINDOW): """Remove all child nodes of the set of matched elements from the DOM.""" return scall(window_info, "html.empty", to=to, selector=selector)
def val(window_info, selector, value, to=WINDOW): """Set the value of every matched element.""" return scall(window_info, "html.val", to=to, selector=selector, value=value)
def append(window_info, selector, html_content, to=WINDOW): """Insert content, specified by the parameter, to the end of each element in the set of matched elements.""" return scall( window_info, "html.append", to=to, selector=selector, content=html_content )
def modal_hide(window_info, to=WINDOW): return scall(window_info, "df.modal.hide", to=to)
def fade_out(window_info, selector, duration=400, to=WINDOW): """Hide the matched elements by fading them to transparent.""" return scall( window_info, "html.fade_out", to=to, selector=selector, duration=duration )
def add_class(window_info, selector, class_name, to=WINDOW): """Adds the specified class(es) to each of the set of matched elements.""" return scall( window_info, "html.add_class", to=to, selector=selector, class_name=class_name )
def download_file(window_info, url, filename=None, to=WINDOW): """Force the client to download the given file.""" return scall(window_info, "html.download_file", to=to, url=url, filename=filename)