Beispiel #1
0
 def index(self, **kw):
     if kw.pop('format', 'html') == 'raw':
         if self._blob.size > asint(
                 tg.config.get('scm.download.max_file_bytes',
                               30 * 1000 * 1000)):
             large_size = self._blob.size
             flash(
                 'File is {}.  Too large to download.'.format(
                     h.do_filesizeformat(large_size)), 'warning')
             raise exc.HTTPForbidden
         else:
             return self.raw()
     elif 'diff' in kw:
         tg.decorators.override_template(
             self.index, 'jinja:allura:templates/repo/diff.html')
         return self.diff(kw['diff'], kw.pop('diformat', None),
                          kw.pop('prev_file', None))
     elif 'barediff' in kw:
         tg.decorators.override_template(
             self.index, 'jinja:allura:templates/repo/barediff.html')
         return self.diff(kw['barediff'], kw.pop('diformat', None),
                          kw.pop('prev_file', None))
     else:
         force_display = 'force' in kw
         if self._blob.size > asint(
                 tg.config.get('scm.view.max_file_bytes', 5 * 1000 * 1000)):
             large_size = self._blob.size
             stats = None
         else:
             large_size = False
             stats = utils.generate_code_stats(self._blob)
         return dict(
             blob=self._blob,
             stats=stats,
             force_display=force_display,
             large_size=large_size,
         )
Beispiel #2
0
    def _make_notification(cls, artifact, topic, **kwargs):
        '''
        Create a Notification instance based on an artifact.  Special handling
        for comments when topic=='message'
        '''

        from allura.model import Project
        idx = artifact.index() if artifact else None
        subject_prefix = '[%s:%s] ' % (
            c.project.shortname, c.app.config.options.mount_point)
        post = ''
        if topic == 'message':
            post = kwargs.pop('post')
            text = post.text
            file_info = kwargs.pop('file_info', None)
            if file_info is not None:
                text = "%s\n\n\nAttachment:" % text
                if not isinstance(file_info, list):
                    file_info = [file_info]
                for attach in file_info:
                    attach.file.seek(0, 2)
                    bytecount = attach.file.tell()
                    attach.file.seek(0)
                    text = "%s %s (%s; %s) " % (text, attach.filename, h.do_filesizeformat(bytecount), attach.type)

            subject = post.subject or ''
            if post.parent_id and not subject.lower().startswith('re:'):
                subject = 'Re: ' + subject
            author = post.author()
            msg_id = artifact.url() + post._id
            parent_msg_id = artifact.url() + post.parent_id if post.parent_id else artifact.message_id()
            d = dict(
                _id=msg_id,
                from_address=str(author._id) if author != User.anonymous() else None,
                reply_to_address='"%s" <%s>' % (
                    subject_prefix, getattr(artifact, 'email_address', u'*****@*****.**')),
                subject=subject_prefix + subject,
                text=text,
                in_reply_to=parent_msg_id,
                author_id=author._id,
                pubdate=datetime.utcnow())
        elif topic == 'flash':
            n = cls(topic=topic,
                    text=kwargs['text'],
                    subject=kwargs.pop('subject', ''))
            return n
        else:
            subject = kwargs.pop('subject', '%s modified by %s' % (
                    h.get_first(idx, 'title'),c.user.get_pref('display_name')))
            reply_to = '"%s" <%s>' % (
                h.get_first(idx, 'title'),
                getattr(artifact, 'email_address', u'*****@*****.**'))
            d = dict(
                from_address=reply_to,
                reply_to_address=reply_to,
                subject=subject_prefix + subject,
                text=kwargs.pop('text', subject),
                author_id=c.user._id,
                pubdate=datetime.utcnow())
            if kwargs.get('message_id'):
                d['_id'] = kwargs['message_id']
            if c.user.get_pref('email_address'):
                d['from_address'] = '"%s" <%s>' % (
                    c.user.get_pref('display_name'),
                    c.user.get_pref('email_address'))
            elif c.user.email_addresses:
                d['from_address'] = '"%s" <%s>' % (
                    c.user.get_pref('display_name'),
                    c.user.email_addresses[0])
        if not d.get('text'):
            d['text'] = ''
        try:
            ''' Add addional text to the notification e-mail based on the artifact type '''
            template = cls.view.get_template('mail/' + artifact.type_s + '.txt')
            d['text'] += template.render(dict(c=c, g=g, config=config, data=artifact, post=post, h=h))
        except jinja2.TemplateNotFound:
            pass
        except:
            ''' Catch any errors loading or rendering the template,
            but the notification still gets sent if there is an error
            '''
            log.warn('Could not render notification template %s' % artifact.type_s, exc_info=True)

        assert d['reply_to_address'] is not None
        project = c.project
        if d.get('project_id', c.project._id) != c.project._id:
            project = Project.query.get(_id=d['project_id'])
        if project.notifications_disabled:
            log.info('Notifications disabled for project %s, not sending %s(%r)',
                     project.shortname, topic, artifact)
            return None
        n = cls(ref_id=artifact.index_id(),
                topic=topic,
                link=kwargs.pop('link', artifact.url()),
                **d)
        return n
Beispiel #3
0
    def _make_notification(cls, artifact, topic, **kwargs):
        '''
        Create a Notification instance based on an artifact.  Special handling
        for comments when topic=='message'
        '''

        from allura.model import Project
        idx = artifact.index() if artifact else None
        subject_prefix = '[%s:%s] ' % (
            c.project.shortname, c.app.config.options.mount_point)
        post = ''
        if topic == 'message':
            post = kwargs.pop('post')
            text = kwargs.get('text') or post.text
            file_info = kwargs.pop('file_info', None)
            if file_info is not None:
                text = "%s\n\n\nAttachments:\n" % text
                if not isinstance(file_info, list):
                    file_info = [file_info]
                for attach in file_info:
                    attach.file.seek(0, 2)
                    bytecount = attach.file.tell()
                    attach.file.seek(0)
                    url = h.absurl('{}attachment/{}'.format(
                        post.url(), h.urlquote(attach.filename)))
                    text = "%s\n- [%s](%s) (%s; %s)" % (
                        text, attach.filename, url,
                        h.do_filesizeformat(bytecount), attach.type)

            subject = post.subject or ''
            if post.parent_id and not subject.lower().startswith('re:'):
                subject = 'Re: ' + subject
            author = post.author()
            msg_id = kwargs.get('message_id') or artifact.url() + post._id
            parent_msg_id = artifact.url() + \
                post.parent_id if post.parent_id else artifact.message_id()
            d = dict(
                _id=msg_id,
                from_address=str(
                    author._id) if author != User.anonymous() else None,
                reply_to_address='"%s" <%s>' % (
                    subject_prefix, getattr(
                        artifact, 'email_address', g.noreply)),
                subject=subject_prefix + subject,
                text=text,
                in_reply_to=parent_msg_id,
                references=cls._references(artifact, post),
                author_id=author._id,
                pubdate=datetime.utcnow())
        elif topic == 'flash':
            n = cls(topic=topic,
                    text=kwargs['text'],
                    subject=kwargs.pop('subject', ''))
            return n
        else:
            subject = kwargs.pop('subject', '%s modified by %s' % (
                h.get_first(idx, 'title'), c.user.get_pref('display_name')))
            reply_to = '"%s" <%s>' % (
                h.get_first(idx, 'title'),
                getattr(artifact, 'email_address', g.noreply))
            d = dict(
                from_address=reply_to,
                reply_to_address=reply_to,
                subject=subject_prefix + subject,
                text=kwargs.pop('text', subject),
                author_id=c.user._id,
                pubdate=datetime.utcnow())
            if kwargs.get('message_id'):
                d['_id'] = kwargs['message_id']
            if c.user.get_pref('email_address'):
                d['from_address'] = '"%s" <%s>' % (
                    c.user.get_pref('display_name'),
                    c.user.get_pref('email_address'))
            elif c.user.email_addresses:
                d['from_address'] = '"%s" <%s>' % (
                    c.user.get_pref('display_name'),
                    c.user.email_addresses[0])
        if not d.get('text'):
            d['text'] = ''
        try:
            ''' Add addional text to the notification e-mail based on the artifact type '''
            template = cls.view.get_template(
                'mail/' + artifact.type_s + '.txt')
            d['text'] += template.render(dict(c=c, g=g,
                                         config=config, data=artifact, post=post, h=h))
        except jinja2.TemplateNotFound:
            pass
        except:
            ''' Catch any errors loading or rendering the template,
            but the notification still gets sent if there is an error
            '''
            log.warn('Could not render notification template %s' %
                     artifact.type_s, exc_info=True)

        assert d['reply_to_address'] is not None
        project = c.project
        if d.get('project_id', c.project._id) != c.project._id:
            project = Project.query.get(_id=d['project_id'])
        if project.notifications_disabled:
            log.debug(
                'Notifications disabled for project %s, not sending %s(%r)',
                project.shortname, topic, artifact)
            return None
        n = cls(ref_id=artifact.index_id(),
                topic=topic,
                link=kwargs.pop('link', artifact.url()),
                **d)
        return n
    def _make_notification(cls, artifact, topic, **kwargs):
        """
        Create a Notification instance based on an artifact.  Special handling
        for comments when topic=='message'
        """

        from allura.model import Project

        idx = artifact.index() if artifact else None
        subject_prefix = "[%s:%s] " % (c.project.shortname, c.app.config.options.mount_point)
        post = ""
        if topic == "message":
            post = kwargs.pop("post")
            text = post.text
            file_info = kwargs.pop("file_info", None)
            if file_info is not None:
                text = "%s\n\n\nAttachment:" % text
                if not isinstance(file_info, list):
                    file_info = [file_info]
                for attach in file_info:
                    attach.file.seek(0, 2)
                    bytecount = attach.file.tell()
                    attach.file.seek(0)
                    text = "%s %s (%s; %s) " % (text, attach.filename, h.do_filesizeformat(bytecount), attach.type)

            subject = post.subject or ""
            if post.parent_id and not subject.lower().startswith("re:"):
                subject = "Re: " + subject
            author = post.author()
            msg_id = artifact.url() + post._id
            parent_msg_id = artifact.url() + post.parent_id if post.parent_id else artifact.message_id()
            d = dict(
                _id=msg_id,
                from_address=str(author._id) if author != User.anonymous() else None,
                reply_to_address='"%s" <%s>'
                % (subject_prefix, getattr(artifact, "email_address", u"*****@*****.**")),
                subject=subject_prefix + subject,
                text=text,
                in_reply_to=parent_msg_id,
                author_id=author._id,
                pubdate=datetime.utcnow(),
            )
        elif topic == "flash":
            n = cls(topic=topic, text=kwargs["text"], subject=kwargs.pop("subject", ""))
            return n
        else:
            subject = kwargs.pop(
                "subject", "%s modified by %s" % (h.get_first(idx, "title"), c.user.get_pref("display_name"))
            )
            reply_to = '"%s" <%s>' % (
                h.get_first(idx, "title"),
                getattr(artifact, "email_address", u"*****@*****.**"),
            )
            d = dict(
                from_address=reply_to,
                reply_to_address=reply_to,
                subject=subject_prefix + subject,
                text=kwargs.pop("text", subject),
                author_id=c.user._id,
                pubdate=datetime.utcnow(),
            )
            if kwargs.get("message_id"):
                d["_id"] = kwargs["message_id"]
            if c.user.get_pref("email_address"):
                d["from_address"] = '"%s" <%s>' % (c.user.get_pref("display_name"), c.user.get_pref("email_address"))
            elif c.user.email_addresses:
                d["from_address"] = '"%s" <%s>' % (c.user.get_pref("display_name"), c.user.email_addresses[0])
        if not d.get("text"):
            d["text"] = ""
        try:
            """ Add addional text to the notification e-mail based on the artifact type """
            template = cls.view.get_template("mail/" + artifact.type_s + ".txt")
            d["text"] += template.render(dict(c=c, g=g, config=config, data=artifact, post=post, h=h))
        except jinja2.TemplateNotFound:
            pass
        except:
            """ Catch any errors loading or rendering the template,
            but the notification still gets sent if there is an error
            """
            log.warn("Could not render notification template %s" % artifact.type_s, exc_info=True)

        assert d["reply_to_address"] is not None
        project = c.project
        if d.get("project_id", c.project._id) != c.project._id:
            project = Project.query.get(_id=d["project_id"])
        if project.notifications_disabled:
            log.info("Notifications disabled for project %s, not sending %s(%r)", project.shortname, topic, artifact)
            return None
        n = cls(ref_id=artifact.index_id(), topic=topic, link=kwargs.pop("link", artifact.url()), **d)
        return n
Beispiel #5
0
    def _make_notification(cls, artifact, topic, **kwargs):
        '''
        Create a Notification instance based on an artifact.  Special handling
        for comments when topic=='message'
        '''

        from allura.model import Project
        idx = artifact.index()
        subject_prefix = '[%s:%s] ' % (
            c.project.shortname, c.app.config.options.mount_point)
        if topic == 'message':
            post = kwargs.pop('post')
            text = post.text
            file_info = kwargs.pop('file_info', None)
            if file_info is not None:
                file_info.file.seek(0, 2)
                bytecount = file_info.file.tell()
                file_info.file.seek(0)
                text = "%s\n\n\nAttachment: %s (%s; %s)" % (text, file_info.filename, h.do_filesizeformat(bytecount), file_info.type)

            subject = post.subject or ''
            if post.parent_id and not subject.lower().startswith('re:'):
                subject = 'Re: ' + subject
            author = post.author()
            d = dict(
                _id=artifact.url()+post._id,
                from_address=str(author._id) if author != User.anonymous() else None,
                reply_to_address='"%s" <%s>' % (
                    subject_prefix, getattr(artifact, 'email_address', u'*****@*****.**')),
                subject=subject_prefix + subject,
                text=text,
                in_reply_to=post.parent_id,
                author_id=author._id,
                pubdate=datetime.utcnow())
        else:
            subject = kwargs.pop('subject', '%s modified by %s' % (
                    idx['title_s'],c.user.get_pref('display_name')))
            reply_to = '"%s" <%s>' % (
                idx['title_s'],
                getattr(artifact, 'email_address', u'*****@*****.**'))
            d = dict(
                from_address=reply_to,
                reply_to_address=reply_to,
                subject=subject_prefix + subject,
                text=kwargs.pop('text', subject),
                author_id=c.user._id,
                pubdate=datetime.utcnow())
            if c.user.get_pref('email_address'):
                d['from_address'] = '"%s" <%s>' % (
                    c.user.get_pref('display_name'),
                    c.user.get_pref('email_address'))
            elif c.user.email_addresses:
                d['from_address'] = '"%s" <%s>' % (
                    c.user.get_pref('display_name'),
                    c.user.email_addresses[0])
        if not d.get('text'):
            d['text'] = ''
        try:
            ''' Add addional text to the notification e-mail based on the artifact type '''
            template = cls.view.get_template('mail/' + artifact.type_s + '.txt')
            d['text'] += template.render(dict(c=c, g=g, config=config, data=artifact))
        except jinja2.TemplateNotFound:
            pass
        except:
            ''' Catch any errors loading or rendering the template,
            but the notification still gets sent if there is an error
            '''
            log.warn('Could not render notification template %s' % artifact.type_s, exc_info=True)

        assert d['reply_to_address'] is not None
        project = Project.query.get(_id=d.get('project_id', c.project._id))
        if project.notifications_disabled:
            log.info('Notifications disabled for project %s, not sending %s(%r)',
                     project.shortname, topic, artifact)
            return None
        n = cls(ref_id=artifact.index_id(),
                topic=topic,
                link=kwargs.pop('link', artifact.url()),
                **d)
        return n