Example #1
0
 def get_action_message(self):
     """Retrieve the currently assigned widget, if any."""
     try:
         return unicodifier(self.get_extra_data()["_message"])
     except KeyError:
         # No widget
         return ""
Example #2
0
    def create(cls, data, schema=None):
        db.session.begin(subtransactions=True)
        try:
            record = cls(unicodifier(data))

            from invenio.modules.jsonalchemy.registry import functions

            list(functions("recordext"))

            toposort_send(before_record_insert, record)

            if schema is not None:
                validate(record, schema)

            metadata = dict(json=dict(record))
            if record.get("recid", None) is not None:
                metadata["id"] = record.get("recid")

            db.session.add(RecordMetadata(**metadata))
            db.session.commit()

            toposort_send(after_record_insert, record)

            return record
        except Exception:
            current_app.logger.exception("Problem creating a record.")
            db.session.rollback()
            raise
Example #3
0
def get_func_info(func):
    """Retrieve a function's information."""
    name = func.func_name
    doc = func.func_doc or ""
    try:
        nicename = func.description
    except AttributeError:
        if doc:
            nicename = doc.split('\n')[0]
            if len(nicename) > 80:
                nicename = name
        else:
            nicename = name
    parameters = []
    closure = func.func_closure
    varnames = func.func_code.co_freevars
    if closure:
        for index, arg in enumerate(closure):
            if not callable(arg.cell_contents):
                parameters.append((varnames[index],
                                   text_type(arg.cell_contents)))
    return unicodifier({
        "nicename": nicename,
        "doc": doc,
        "parameters": parameters,
        "name": name
    })
Example #4
0
    def create(cls, data, schema=None):
        db.session.begin(subtransactions=True)
        try:
            record = cls(unicodifier(data))

            from invenio.modules.jsonalchemy.registry import functions
            list(functions('recordext'))

            toposort_send(before_record_insert, record)

            if schema is not None:
                validate(record, schema)

            metadata = dict(json=dict(record))
            if record.get('recid', None) is not None:
                metadata['id'] = record.get('recid')

            db.session.add(RecordMetadata(**metadata))
            db.session.commit()

            toposort_send(after_record_insert, record)

            return record
        except Exception:
            current_app.logger.exception("Problem creating a record.")
            db.session.rollback()
            raise
Example #5
0
def get_func_info(func):
    """Retrieve a function's information."""
    name = func.func_name
    doc = func.func_doc or ""
    try:
        nicename = func.description
    except AttributeError:
        if doc:
            nicename = doc.split('\n')[0]
            if len(nicename) > 80:
                nicename = name
        else:
            nicename = name
    parameters = []
    closure = func.func_closure
    varnames = func.func_code.co_freevars
    if closure:
        for index, arg in enumerate(closure):
            if not callable(arg.cell_contents):
                parameters.append((varnames[index], arg.cell_contents))
    return unicodifier({
        "nicename": nicename,
        "doc": doc,
        "parameters": parameters,
        "name": name
    })
Example #6
0
    def get_form(self, formdata=None, load_draft=True,
                 validate_draft=False):
        """
        Create form instance with draft data and form data if provided.

        :param formdata: Incoming form data.
        :param files: Files to ingest into form
        :param load_draft: True to initialize form with draft data.
        :param validate_draft: Set to true to validate draft data, when no form
             data is provided.
        """
        if not self.has_form():
            raise FormDoesNotExists(self.id)

        draft_data = unicodifier(self.values) if load_draft else {}
        formdata = MultiDict(formdata or {})

        form = self.form_class(
            formdata=formdata, **draft_data
        )

        if formdata:
            form.reset_field_data(exclude=formdata.keys())

        # Set field flags
        if load_draft and self.flags:
            form.set_flags(self.flags)

        if validate_draft and draft_data and formdata is None:
            form.validate()

        return form
Example #7
0
 def get_action_message(self):
     """Retrieve the currently assigned widget, if any."""
     try:
         return unicodifier(self.get_extra_data()["_message"])
     except KeyError:
         # No widget
         return ""
Example #8
0
    def marshal_deposition(cls, deposition):
        """
        Generate a JSON representation for REST API of a Deposition
        """
        # Get draft
        if deposition.has_sip() and '_edit' in deposition.drafts:
            draft = deposition.get_draft('_edit')
            metadata_fields = cls.marshal_metadata_edit_fields
        elif deposition.has_sip():
            # FIXME: Not based on latest available data in record.
            sip = deposition.get_latest_sip(sealed=True)
            draft = record_to_draft(
                Record.create(sip.package, master_format='marc'),
                post_process=process_draft
            )
            metadata_fields = cls.marshal_metadata_edit_fields
        else:
            draft = deposition.get_or_create_draft('_metadata')
            metadata_fields = cls.marshal_metadata_fields

        # Fix known differences in marshalling
        current_app.logger.debug(draft.values)
        draft.values = filter_empty_elements(draft.values)
        current_app.logger.debug(draft.values)

        # Set disabled values to None in output
        for field, flags in draft.flags.items():
            if 'disabled' in flags and field in draft.values:
                current_app.logger.debug(field)
                del draft.values[field]

        # Marshal deposition
        obj = marshal(deposition, cls.marshal_deposition_fields)
        # Marshal the metadata attribute
        obj['metadata'] = marshal(unicodifier(draft.values), metadata_fields)

        # Add record and DOI information from latest SIP
        for sip in deposition.sips:
            if sip.is_sealed():
                recjson = sip.metadata
                if recjson.get('recid'):
                    obj['record_id'] = fields.Integer().format(
                        recjson.get('recid')
                    )
                    obj['record_url'] = fields.String().format(url_for(
                        'record.metadata',
                        recid=recjson.get('recid'),
                        _external=True
                    ))
                if (recjson.get('doi') and recjson.get('doi').startswith(
                        cfg['CFG_DATACITE_DOI_PREFIX'] + "/")):
                    obj['doi'] = fields.String().format(recjson.get('doi'))
                    obj['doi_url'] = fields.String().format(
                        "http://dx.doi.org/%s" % obj['doi']
                    )
                break

        return obj
Example #9
0
    def get_form(self, formdata=None, load_draft=True,
                 validate_draft=False):
        """
        Create form instance with draft data and form data if provided.

        :param formdata: Incoming form data.
        :param files: Files to ingest into form
        :param load_draft: True to initialize form with draft data.
        :param validate_draft: Set to true to validate draft data, when no form
             data is provided.
        """
        if not self.has_form():
            raise FormDoesNotExists(self.id)

        # If a field is not present in formdata, Form.process() will assume it
        # is blank instead of using the draft_data value. Most of the time we
        # are only submitting a single field in JSON via AJAX requests. We
        # therefore reset non-submitted fields to the draft_data value with
        # form.reset_field_data().

        # WTForms deal with unicode - we deal with UTF8 so convert all
        draft_data = unicodifier(self.values) if load_draft else {}
        formdata = MultiDict(formdata or {})

        form = self.form_class(
            formdata=formdata, **draft_data
        )
        if formdata:
            form.reset_field_data(exclude=formdata.keys())

        # Set field flags
        if load_draft and self.flags:
            form.set_flags(self.flags)

        # Ingest files in form
        if self._deposition_ref:
            form.files = self._deposition_ref.files
        else:
            form.files = []

        if validate_draft and draft_data and formdata is None:
            form.validate()

        return form
Example #10
0
    def get_form(self, formdata=None, load_draft=True,
                 validate_draft=False):
        """
        Create form instance with draft data and form data if provided.

        :param formdata: Incoming form data.
        :param files: Files to ingest into form
        :param load_draft: True to initialize form with draft data.
        :param validate_draft: Set to true to validate draft data, when no form
             data is provided.
        """
        if not self.has_form():
            raise FormDoesNotExists(self.id)

        # If a field is not present in formdata, Form.process() will assume it
        # is blank instead of using the draft_data value. Most of the time we
        # are only submitting a single field in JSON via AJAX requests. We
        # therefore reset non-submitted fields to the draft_data value with
        # form.reset_field_data().

        # WTForms deal with unicode - we deal with UTF8 so convert all
        draft_data = unicodifier(self.values) if load_draft else {}
        formdata = MultiDict(formdata or {})

        form = self.form_class(
            formdata=formdata, **draft_data
        )
        if formdata:
            form.reset_field_data(exclude=formdata.keys())

        # Set field flags
        if load_draft and self.flags:
            form.set_flags(self.flags)

        # Ingest files in form
        if self._deposition_ref:
            form.files = self._deposition_ref.files
        else:
            form.files = []

        if validate_draft and draft_data and formdata is None:
            form.validate()

        return form
Example #11
0
 def unicode_gettext_wrapper(text, **kwargs):
     from invenio.base.helpers import unicodifier
     from invenio.utils.text import wash_for_utf8
     return wash_for_utf8(gettext(unicodifier(text),
                                  **unicodifier(kwargs)))
Example #12
0
 def unicode_gettext_wrapper(text, **kwargs):
     from invenio.base.helpers import unicodifier
     from invenio.utils.text import wash_for_utf8
     return wash_for_utf8(
         gettext(unicodifier(text), **unicodifier(kwargs)))
Example #13
0
def forge_email(fromaddr, toaddr, subject, content, html_content='',
                html_images=None, usebcc=False, header=None, footer=None,
                html_header=None, html_footer=None, ln=None,
                charset=None, replytoaddr="", attachments=None, bccaddr=""):
    """Prepare email. Add header and footer if needed.
    @param fromaddr: [string] sender
    @param toaddr: [string or list-of-strings] list of receivers (if string, then
                   receivers are separated by ',')
    @param usebcc: [bool] True for using Bcc in place of To
    @param subject: [string] subject of the email
    @param content: [string] content of the email
    @param html_content: [string] html version of the email
    @param html_images: [dict] dictionary of image id, image path
    @param header: [string] None for the default header
    @param footer: [string] None for the default footer
    @param ln: language
    @charset: [string] the content charset. By default is None which means
    to try to encode the email as ascii, then latin1 then utf-8.
    @param replytoaddr: [string or list-of-strings] to be used for the
                        reply-to header of the email (if string, then
                        receivers are separated by ',')
    @param attachments: list of paths of files to be attached. Alternatively,
        every element of the list could be a tuple: (filename, mimetype)
    @param bccaddr: [string or list-of-strings] to be used for BCC header of the email
                    (if string, then receivers are separated by ',')
    @return: forged email as a string"""
    ln = default_ln(ln)
    if html_images is None:
        html_images = {}

    content = render_template_to_string('mail_text.tpl',
                                        content=unicodifier(content),
                                        header=unicodifier(header),
                                        footer=unicodifier(footer)
                                        ).encode('utf8')

    if type(toaddr) is list:
        toaddr = ','.join(toaddr)

    if type(bccaddr) is list:
        bccaddr = ','.join(bccaddr)

    if type(replytoaddr) is list:
        replytoaddr = ','.join(replytoaddr)

    toaddr = remove_temporary_emails(toaddr)

    headers = {}
    kwargs = {'to': [], 'cc': [], 'bcc': []}

    if replytoaddr:
        headers['Reply-To'] = replytoaddr
    if usebcc:
        headers['Bcc'] = bccaddr
        kwargs['bcc'] = toaddr.split(',') + bccaddr.split(',')
        kwargs['to'] = ['Undisclosed.Recipients:']
    else:
        kwargs['to'] = toaddr.split(',')
    headers['From'] = fromaddr
    headers['Date'] = formatdate(localtime=True)
    headers['User-Agent'] = 'Invenio %s at %s' % (cfg['CFG_VERSION'],
                                                  cfg['CFG_SITE_URL'])

    if html_content:
        html_content = render_template_to_string(
            'mail_html.tpl',
            content=unicodifier(html_content),
            header=unicodifier(html_header),
            footer=unicodifier(html_footer)
            ).encode('utf8')

        msg_root = EmailMultiAlternatives(subject=subject, body=content,
                                          from_email=fromaddr,
                                          headers=headers, **kwargs)
        msg_root.attach_alternative(html_content, "text/html")

        #if not html_images:
        #    # No image? Attach the HTML to the root
        #    msg_root.attach(msg_text)
        #else:
        if html_images:
            # Image(s)? Attach the HTML and image(s) as children of a
            # "related" block
            msg_related = MIMEMultipart('related')
            #msg_related.attach(msg_text)
            for image_id, image_path in iteritems(html_images):
                attach_embed_image(msg_related, image_id, image_path)
            msg_root.attach(msg_related)
    else:
        msg_root = EmailMessage(subject=subject, body=content,
                                from_email=fromaddr, headers=headers, **kwargs)

    if attachments:
        from invenio.legacy.bibdocfile.api import _mimes, guess_format_from_url
        #old_msg_root = msg_root
        #msg_root = MIMEMultipart()
        #msg_root.attach(old_msg_root)
        for attachment in attachments:
            try:
                mime = None
                if type(attachment) in (list, tuple):
                    attachment, mime = attachment
                if mime is None:
                    ## Automatic guessing of mimetype
                    mime = _mimes.guess_type(attachment)[0]
                if mime is None:
                    ext = guess_format_from_url(attachment)
                    mime = _mimes.guess_type("foo" + ext)[0]
                if not mime:
                    mime = 'application/octet-stream'
                part = MIMEBase(*mime.split('/', 1))
                part.set_payload(open(attachment, 'rb').read())
                Encoders.encode_base64(part)
                part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(attachment))
                msg_root.attach(part)
            except:
                from invenio.ext.logging import register_exception
                register_exception(alert_admin=True, prefix="Can't attach %s" % attachment)

    return msg_root
Example #14
0
 def __init__(self, query):
     """Initialize with search query."""
     self._query = unicodifier(query)
Example #15
0
 def __init__(self, query):
     """Initialize with search query."""
     self._query = unicodifier(query)
Example #16
0
def forge_email(fromaddr,
                toaddr,
                subject,
                content,
                html_content='',
                html_images=None,
                usebcc=False,
                header=None,
                footer=None,
                html_header=None,
                html_footer=None,
                ln=None,
                charset=None,
                replytoaddr="",
                attachments=None,
                bccaddr=""):
    """Prepare email. Add header and footer if needed.
    @param fromaddr: [string] sender
    @param toaddr: [string or list-of-strings] list of receivers (if string, then
                   receivers are separated by ',')
    @param usebcc: [bool] True for using Bcc in place of To
    @param subject: [string] subject of the email
    @param content: [string] content of the email
    @param html_content: [string] html version of the email
    @param html_images: [dict] dictionary of image id, image path
    @param header: [string] None for the default header
    @param footer: [string] None for the default footer
    @param ln: language
    @charset: [string] the content charset. By default is None which means
    to try to encode the email as ascii, then latin1 then utf-8.
    @param replytoaddr: [string or list-of-strings] to be used for the
                        reply-to header of the email (if string, then
                        receivers are separated by ',')
    @param attachments: list of paths of files to be attached. Alternatively,
        every element of the list could be a tuple: (filename, mimetype)
    @param bccaddr: [string or list-of-strings] to be used for BCC header of the email
                    (if string, then receivers are separated by ',')
    @return: forged email as an EmailMessage object"""
    ln = default_ln(ln)
    if html_images is None:
        html_images = {}

    content = render_template_to_string(
        'mail_text.tpl',
        content=unicodifier(content),
        header=unicodifier(header),
        footer=unicodifier(footer)).encode('utf8')

    if type(toaddr) is list:
        toaddr = ','.join(toaddr)

    if type(bccaddr) is list:
        bccaddr = ','.join(bccaddr)

    if type(replytoaddr) is list:
        replytoaddr = ','.join(replytoaddr)

    toaddr = remove_temporary_emails(toaddr)

    headers = {}
    kwargs = {'to': [], 'cc': [], 'bcc': []}

    if replytoaddr:
        headers['Reply-To'] = replytoaddr
    if usebcc:
        headers['Bcc'] = bccaddr
        kwargs['bcc'] = toaddr.split(',') + bccaddr.split(',')
        kwargs['to'] = ['Undisclosed.Recipients:']
    else:
        kwargs['to'] = toaddr.split(',')
    headers['From'] = fromaddr
    headers['Date'] = formatdate(localtime=True)
    headers['User-Agent'] = 'Invenio %s at %s' % (cfg['CFG_VERSION'],
                                                  cfg['CFG_SITE_URL'])

    if html_content:
        html_content = render_template_to_string(
            'mail_html.tpl',
            content=unicodifier(html_content),
            header=unicodifier(html_header),
            footer=unicodifier(html_footer)).encode('utf8')

        msg_root = EmailMultiAlternatives(subject=subject,
                                          body=content,
                                          from_email=fromaddr,
                                          headers=headers,
                                          **kwargs)
        msg_root.attach_alternative(html_content, "text/html")

        #if not html_images:
        #    # No image? Attach the HTML to the root
        #    msg_root.attach(msg_text)
        #else:
        if html_images:
            # Image(s)? Attach the HTML and image(s) as children of a
            # "related" block
            msg_related = MIMEMultipart('related')
            #msg_related.attach(msg_text)
            for image_id, image_path in iteritems(html_images):
                attach_embed_image(msg_related, image_id, image_path)
            msg_root.attach(msg_related)
    else:
        msg_root = EmailMessage(subject=subject,
                                body=content,
                                from_email=fromaddr,
                                headers=headers,
                                **kwargs)

    if attachments:
        from invenio.legacy.bibdocfile.api import _mimes, guess_format_from_url
        #old_msg_root = msg_root
        #msg_root = MIMEMultipart()
        #msg_root.attach(old_msg_root)
        for attachment in attachments:
            try:
                mime = None
                if type(attachment) in (list, tuple):
                    attachment, mime = attachment
                if mime is None:
                    ## Automatic guessing of mimetype
                    mime = _mimes.guess_type(attachment)[0]
                if mime is None:
                    ext = guess_format_from_url(attachment)
                    mime = _mimes.guess_type("foo" + ext)[0]
                if not mime:
                    mime = 'application/octet-stream'
                part = MIMEBase(*mime.split('/', 1))
                part.set_payload(open(attachment, 'rb').read())
                Encoders.encode_base64(part)
                part.add_header(
                    'Content-Disposition',
                    'attachment; filename="%s"' % os.path.basename(attachment))
                msg_root.attach(part)
            except:
                from invenio.ext.logging import register_exception
                register_exception(alert_admin=True,
                                   prefix="Can't attach %s" % attachment)

    return msg_root
Example #17
0
 def wrapper(*args, **kwds):
     return unicodifier(f(*args, **kwds))