Esempio n. 1
0
def resource_quickupload_file(request):
    """
    json view used by quikupload script
    when uploading a file
    return success/error + file infos (url/preview/title ...)
    """               
    msg = {}
    if request.META.get('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest':
        file_name = urllib.unquote(request.META.get('HTTP_X_FILE_NAME'))
        title = request.GET.get('title', '')
        upload_with = "XHR"        
        try:
            # the solution for sync ajax file upload
            file_data = SimpleUploadedFile(file_name, request.raw_post_data)
        except:
            log.debug("XHR Upload of %s has been aborted" %file_name)
            file_data = None
            # not really useful here since the upload block
            # is removed by "cancel" action, but
            # could be useful if someone change the js behavior
            msg = {u'error': u'emptyError'}
    else:
        # MSIE fallback behavior (classic upload with iframe)
        file_data = request.FILES.get("qqfile", None)
        filename = getattr(file_data,'name', '')
        file_name = filename.split("\\")[-1]
        title = request.POST.get('title', '')
        upload_with = "CLASSIC FORM POST"
        # we must test the file size in this case (because there is no size client test with msie file field)
        if not utils._check_file_size(file_data):
            log.debug("The file %s is too big, quick iframe upload rejected" % file_name) 
            msg = {u'error': u'sizeError'}

    if file_data and not msg:
        publisher_id = request.GET.get('publisher_id', request.POST.get('publisher_id', ''))
        try:                publisher_id = int(publisher_id)
        except ValueError:  publisher_id = None
        content_type = mimetypes.guess_type(file_name)[0] or 'application/octet-stream'

        try:
            # Create the resource itself
            resource = Resource.objects.create(
                resource_file = file_data,
                title = title,
                publisher_id = publisher_id and int(publisher_id),
                filename = file_name,
            )
            is_image = resource.is_image
            type = is_image and 'image' or 'file'
            # Generate the preview thumbnails
            thumbnails = resource.thumbnails
            msg = {
                'success':       True,
                'value':         resource.id,
                'url':           resource.get_absolute_url(),
                'thumbnail_url': is_image and thumbnails['medium'].url or thumbnails['big_icon'].url,
                'mini_url':      is_image and thumbnails['summary_preview'].url or thumbnails['big_icon'].url,
                # only used by image size selection (wysiwyg browser)
                'summary_url':   is_image and thumbnails['summary'].url or '',
                'preview_url':   is_image and thumbnails['preview'].url or '',
                'legend':        title and title or file_name, 
                'scope':         publisher_id,
                'type':          type
            }
        except PermissionDenied, e:
            log.exception(e)
            msg = {u'error': u'pemissiondeniedError'}
        except:
Esempio n. 2
0
                'value':         resource.id,
                'url':           resource.get_absolute_url(),
                'thumbnail_url': is_image and thumbnails['medium'].url or thumbnails['big_icon'].url,
                'mini_url':      is_image and thumbnails['summary_preview'].url or thumbnails['big_icon'].url,
                # only used by image size selection (wysiwyg browser)
                'summary_url':   is_image and thumbnails['summary'].url or '',
                'preview_url':   is_image and thumbnails['preview'].url or '',
                'legend':        title and title or file_name, 
                'scope':         publisher_id,
                'type':          type
            }
        except PermissionDenied, e:
            log.exception(e)
            msg = {u'error': u'pemissiondeniedError'}
        except:
            log.exception("Unexpected error while trying to upload a file.")
            msg = {u'error': u'unexpectedError'}
    else:
        msg = {u'error': u'serverError'}

    return HttpResponse( json.dumps(msg),
                         mimetype='text/html')

###############################
# Resource Browser json views #
###############################

@require_access
def resource_by_publisher_json(request, publisher_id):
    """
    return all resources for a publisher id
Esempio n. 3
0
    def __call__(self, sender, **kwargs):
        """
        Generate the message itself.
        XXX TODO: Handle translation correctly (not from the request only)
        """
        # Fake-Login with SystemAccount so that everybody can be notified,
        # even users this current user can't list.
        from twistranet.twistapp.models import SystemAccount, Account, UserAccount, Community, Twistable
        __account__ = SystemAccount.get()
        from_email = settings.SERVER_EMAIL
        host = settings.EMAIL_HOST
        cache_mimeimages = {}
        if not host:
            # If host is disabled (EMAIL_HOST is None), skip that
            return

        # Handle recipients emails
        recipients = kwargs.get(self.recipient_arg, None)
        if not recipients:
            raise ValueError("Recipient must be provided as a '%s' parameter" %
                             self.recipient_arg)
        to_list = []
        if not isinstance(recipients, (
                list,
                tuple,
                QuerySet,
        )):
            recipients = (recipients, )
        for recipient in recipients:
            if isinstance(recipient, Twistable):
                recipient = recipient.object
            if isinstance(recipient, UserAccount):
                to = recipient.email
                if not to:
                    log.warning(
                        "Can't send email for '%s': %s doesn't have an email registered."
                        % (
                            sender,
                            recipient,
                        ))
                    return
                to_list.append(to)
            elif isinstance(recipient, Community):
                if self.managers_only:
                    members = recipient.managers
                else:
                    members = recipient.members
                # XXX Suboptimal for very large communities
                to_list.extend(
                    [member.email for member in members if member.email])
            elif type(recipient) in (
                    str,
                    unicode,
            ):
                to_list.append(recipient)  # XXX Todo: check the '@'
            else:
                raise ValueError("Invalid recipient: %s (%s)" % (
                    recipient,
                    type(recipient),
                ))

        # Fetch templates
        text_template = kwargs.get('text_template', self.text_template)
        html_template = kwargs.get('html_template', self.html_template)

        # Now generate template and send mail for each recipient
        # XXX TODO: See http://docs.djangoproject.com/en/1.2/topics/email/#sending-multiple-e-mails
        # for the good approach to use.
        for to in to_list:
            # Append domain (and site info) to kwargs
            d = kwargs.copy()
            domain = cache.get("twistranet_site_domain")
            d.update({
                "domain": domain,
                "site_name": utils.get_site_name(),
                "baseline": utils.get_baseline(),
                "recipient": to,  # A string
            })

            # Load both templates and render them with kwargs context
            text_tpl = get_template(text_template)
            c = Context(d)
            text_content = text_tpl.render(c).strip()
            if html_template:
                html_tpl = get_template(html_template)
                html_content = html_tpl.render(c)
            else:
                html_content = None

            # Fetch back subject from text template
            subject = self.subject
            if not subject:
                match = SUBJECT_REGEX.search(text_content)
                if match:
                    subject = match.groups()[0]
            if not subject:
                raise ValueError(
                    "No subject provided nor 'Subject:' first line in your text template"
                )

            # Remove empty lines and "Subject:" line from text templates
            text_content = SUBJECT_REGEX.sub('', text_content)
            text_content = EMPTY_LINE_REGEX.sub('\n', text_content)

            # Prepare messages
            msg = EmailMultiAlternatives(
                subject,
                text_content,
                from_email,
                [to],
            )

            if html_content:
                if getattr(settings, 'SEND_EMAIL_IMAGES_AS_ATTACHMENTS',
                           DEFAULT_SEND_EMAIL_IMAGES_AS_ATTACHMENTS):
                    # we replace img links by img Mime Images
                    mimeimages = []

                    def replace_img_url(match):
                        """Change src url by mimeurl
                           fill the mimeimages list
                        """
                        urlpath = str(match.group('urlpath'))
                        attribute = str(match.group('attribute'))

                        is_static = False
                        pathSplit = urlpath.split('/')
                        if 'static' in pathSplit:
                            filename = urlpath.split('/static/')[-1]
                            is_static = True
                        else:
                            # XXX TODO : need to be improved split with site path (for vhosts)
                            filename = urlpath.split('/')[-1]
                        nb = len(mimeimages) + 1
                        mimeimages.append((filename, 'img%i' % nb, is_static))
                        mimeurl = "cid:img%i" % nb
                        return '%s="%s"' % (attribute, mimeurl)

                    img_url_expr = re.compile(
                        '(?P<attribute>src)\s*=\s*([\'\"])(%s)?(?P<urlpath>[^\"\']*)\\2'
                        % domain, re.IGNORECASE)
                    html_content = img_url_expr.sub(replace_img_url,
                                                    html_content)
                    msg.attach_alternative(html_content, "text/html")
                    if mimeimages:
                        msg.mixed_subtype = 'related'
                        for fkey, name, is_static in mimeimages:
                            if cache_mimeimages.has_key(fkey):
                                msgImage = cache_mimeimages[fkey]
                            else:
                                if is_static:
                                    f = open(
                                        path.join(
                                            settings.TWISTRANET_STATIC_PATH,
                                            fkey), 'rb')
                                else:
                                    f = open(
                                        path.join(settings.MEDIA_ROOT, fkey),
                                        'rb')
                                cache_mimeimages[fkey] = msgImage = MIMEImage(
                                    f.read())
                                f.close()
                            msgImage.add_header('Content-ID', '<%s>' % name)
                            msgImage.add_header('Content-Disposition',
                                                'inline')
                            msg.attach(msgImage)
                # just inline images
                else:
                    msg.attach_alternative(html_content, "text/html")
            # Send safely
            try:
                log.debug("Sending mail: '%s' from '%s' to '%s'" %
                          (subject, from_email, to))
                msg.send()
            except:
                log.warning("Unable to send message to %s" % to)
                log.exception("Here's what we've got as an error.")
    def __call__(self, sender, **kwargs):
        """
        Generate the message itself.
        XXX TODO: Handle translation correctly (not from the request only)
        """
        # Fake-Login with SystemAccount so that everybody can be notified,
        # even users this current user can't list.
        from twistranet.twistapp.models import SystemAccount, Account, UserAccount, Community, Twistable
        __account__ = SystemAccount.get()
        from_email = settings.SERVER_EMAIL
        host = settings.EMAIL_HOST
        cache_mimeimages = {}
        if not host:
            # If host is disabled (EMAIL_HOST is None), skip that
            return
        
        # Handle recipients emails
        recipients = kwargs.get(self.recipient_arg, None)
        if not recipients:
            raise ValueError("Recipient must be provided as a '%s' parameter" % self.recipient_arg)
        to_list = []
        if not isinstance(recipients, (list, tuple, QuerySet, )):
            recipients = (recipients, )
        for recipient in recipients:
            if isinstance(recipient, Twistable):
                recipient = recipient.object
            if isinstance(recipient, UserAccount):
                to = recipient.email
                if not to:
                    log.warning("Can't send email for '%s': %s doesn't have an email registered." % (sender, recipient, ))
                    return
                to_list.append(to)
            elif isinstance(recipient, Community):
                if self.managers_only:
                    members = recipient.managers
                else:
                    members = recipient.members
                # XXX Suboptimal for very large communities
                to_list.extend([ member.email for member in members if member.email ])
            elif type(recipient) in (str, unicode, ):
                to_list.append(recipient)        # XXX Todo: check the '@'
            else:
                raise ValueError("Invalid recipient: %s (%s)" % (recipient, type(recipient), ))
                
        # Fetch templates
        text_template = kwargs.get('text_template', self.text_template)
        html_template = kwargs.get('html_template', self.html_template)
                
        # Now generate template and send mail for each recipient
        # XXX TODO: See http://docs.djangoproject.com/en/1.2/topics/email/#sending-multiple-e-mails
        # for the good approach to use.
        for to in to_list:
            # Append domain (and site info) to kwargs
            d = kwargs.copy()
            domain = cache.get("twistranet_site_domain")
            d.update({
                "domain":       domain,
                "site_name":    utils.get_site_name(),
                "baseline":     utils.get_baseline(),
                "recipient":    to,     # A string
            })
        
            # Load both templates and render them with kwargs context
            text_tpl = get_template(text_template)
            c = Context(d)
            text_content = text_tpl.render(c).strip()
            if html_template:
                html_tpl = get_template(html_template)
                html_content = html_tpl.render(c)
            else:
                html_content = None
            
            # Fetch back subject from text template
            subject = self.subject
            if not subject:
                match = SUBJECT_REGEX.search(text_content)
                if match:
                    subject = match.groups()[0]
            if not subject:
                raise ValueError("No subject provided nor 'Subject:' first line in your text template")
            
            # Remove empty lines and "Subject:" line from text templates
            text_content = SUBJECT_REGEX.sub('', text_content)
            text_content = EMPTY_LINE_REGEX.sub('\n', text_content)
        
            # Prepare messages
            msg = EmailMultiAlternatives(subject, text_content, from_email, [ to ], )
            if html_content:
                if getattr(settings, 'SEND_EMAIL_IMAGES_AS_ATTACHMENTS', DEFAULT_SEND_EMAIL_IMAGES_AS_ATTACHMENTS):
                    # we replace img links by img Mime Images
                    mimeimages = []
                    def replace_img_url(match):
                        """Change src url by mimeurl
                           fill the mimeimages list
                        """
                        urlpath = str(match.group('urlpath'))
                        attribute = str(match.group('attribute'))

                        is_static = False
                        pathSplit = urlpath.split('/')
                        if 'static' in pathSplit:
                            filename = urlpath.split('/static/')[-1]
                            is_static = True
                        else:
                            # XXX TODO : need to be improved split with site path (for vhosts)
                            filename = urlpath.split('/')[-1]
                        nb = len(mimeimages)+1
                        mimeimages.append((filename, 'img%i'%nb, is_static))
                        mimeurl = "cid:img%i" %nb
                        return '%s="%s"' % (attribute,mimeurl)

                    img_url_expr = re.compile('(?P<attribute>src)\s*=\s*([\'\"])(%s)?(?P<urlpath>[^\"\']*)\\2' %domain, re.IGNORECASE)
                    html_content = img_url_expr.sub(replace_img_url, html_content)
                    msg.attach_alternative(html_content, "text/html")
                    if mimeimages:
                        msg.mixed_subtype = 'related'
                        for fkey, name, is_static in mimeimages:
                            if cache_mimeimages.has_key(fkey):
                                msgImage = cache_mimeimages[fkey]
                            else:
                                if is_static:
                                    f = open(path.join(settings.TWISTRANET_STATIC_PATH, fkey), 'rb')
                                else:
                                    f = open(path.join(settings.MEDIA_ROOT, fkey), 'rb')
                                cache_mimeimages[fkey] = msgImage = MIMEImage(f.read())
                                f.close()
                            msgImage.add_header('Content-ID', '<%s>' % name)
                            msgImage.add_header('Content-Disposition', 'inline')
                            msg.attach(msgImage)
                # just inline images
                else:
                    msg.attach_alternative(html_content, "text/html")
            # Send safely
            try:
                log.debug("Sending mail: '%s' from '%s' to '%s'" % (subject, from_email, to))
                msg.send()
            except:
                log.warning("Unable to send message to %s" % to)
                log.exception("Here's what we've got as an error.")
Esempio n. 5
0
 def __call__(self, sender, **kwargs):
     """
     Generate the message itself.
     XXX TODO: Handle translation correctly (not from the request only)
     """
     # Fake-Login with SystemAccount so that everybody can be notified,
     # even users this current user can't list.
     from twistranet.twistapp.models import SystemAccount, Account, UserAccount, Community, Twistable
     __account__ = SystemAccount.get()
     from_email = settings.SERVER_EMAIL
     host = settings.EMAIL_HOST
     if not host:
         # If host is disabled (EMAIL_HOST is None), skip that
         return
     
     # Handle recipients emails
     recipients = kwargs.get(self.recipient_arg, None)
     if not recipients:
         raise ValueError("Recipient must be provided as a '%s' parameter" % self.recipient_arg)
     to_list = []
     if not isinstance(recipients, (list, tuple, QuerySet, )):
         recipients = (recipients, )
     for recipient in recipients:
         if isinstance(recipient, Twistable):
             recipient = recipient.object
         if isinstance(recipient, UserAccount):
             to = recipient.email
             if not to:
                 log.warning("Can't send email for '%s': %s doesn't have an email registered." % (sender, recipient, ))
                 return
             to_list.append(to)
         elif isinstance(recipient, Community):
             if self.managers_only:
                 members = recipient.managers
             else:
                 members = recipient.members
             # XXX Suboptimal for very large communities
             to_list.extend([ member.email for member in members if member.email ])
         elif type(recipient) in (str, unicode, ):
             to_list.append(recipient)        # XXX Todo: check the '@'
         else:
             raise ValueError("Invalid recipient: %s (%s)" % (recipient, type(recipient), ))
             
     # Fetch templates
     text_template = kwargs.get('text_template', self.text_template)
     html_template = kwargs.get('html_template', self.html_template)
             
     # Now generate template and send mail for each recipient
     # XXX TODO: See http://docs.djangoproject.com/en/1.2/topics/email/#sending-multiple-e-mails
     # for the good approach to use.
     for to in to_list:
         # Append domain (and site info) to kwargs
         d = kwargs.copy()
         domain = cache.get("twistranet_site_domain")
         d.update({
             "domain":       domain,
             "site_name":    utils.get_site_name(),
             "baseline":     utils.get_baseline(),
             "recipient":    to,     # A string
         })
     
         # Load both templates and render them with kwargs context
         text_tpl = get_template(text_template)
         c = Context(d)
         text_content = text_tpl.render(c).strip()
         if html_template:
             html_tpl = get_template(html_template)
             html_content = html_tpl.render(c)
         else:
             html_content = None
         
         # Fetch back subject from text template
         subject = self.subject
         if not subject:
             match = SUBJECT_REGEX.search(text_content)
             if match:
                 subject = match.groups()[0]
         if not subject:
             raise ValueError("No subject provided nor 'Subject:' first line in your text template")
         
         # Remove empty lines and "Subject:" line from text templates
         text_content = SUBJECT_REGEX.sub('', text_content)
         text_content = EMPTY_LINE_REGEX.sub('\n', text_content)
     
         # Prepare messages
         msg = EmailMultiAlternatives(subject, text_content, from_email, [ to ], )
         if html_content:
             msg.attach_alternative(html_content, "text/html")
         
         # Send safely
         try:
             log.debug("Sending mail: '%s' from '%s' to '%s'" % (subject, from_email, to))
             msg.send()
         except:
             log.warning("Unable to send message to %s" % to)
             log.exception("Here's what we've got as an error.")