Esempio n. 1
0
            return False
        value = getattr(obj, col.name)
        try:
            return value == col.type.python_type(data_val)
        except ValueError:
            # coerce failed
            return False

    for obj in coll:
        if all(match_pk(obj, col) for col in mapper.primary_key):
            return obj

    return None

fe = wtforms_alchemy.utils.find_entity
patch_logger.info(fe)
fe.func_code = find_entity.func_code
fe.__module__ = find_entity.__module__
del fe
del find_entity
# end monkey patch

class ModelFieldList(BaseModelFieldList):
  """ Filter empty entries
  """

  def validate(self, form, extra_validators=tuple()):
    for field in self.entries:
      is_subform = isinstance(field, FormField)
      data = field.data.values() if is_subform else [field.data]
Esempio n. 2
0
_TEXT_ANALYZER = StemmingAnalyzer() | CharsetFilter(accent_map)

_pending_indexation_attr = "abilian_pending_indexation"

# as of whoosh 2.5.7, a method is missing on WrappingCollector. See
# https://bitbucket.org/mchaput/whoosh/issue/394/error-when-searching-with-groupedby-and
_PATCHED = False

if not _PATCHED:

    def wrapping_collector_remove(self, global_docnum):
        return self.child.remove(global_docnum)

    from abilian.core.logging import patch_logger

    patch_logger.info(WrappingCollector.remove)
    WrappingCollector.remove = wrapping_collector_remove
    _PATCHED = True
    del patch_logger
    del wrapping_collector_remove
# END PATCH


def url_for_hit(hit, default="#"):
    """Helper for building URLs from results."""
    try:
        object_type = hit["object_type"]
        object_id = int(hit["id"])
        return current_app.default_view.url_for(hit, object_type, object_id)
    except KeyError:
        return default
Esempio n. 3
0
# FIXME: we'ld rather subclass Message and update all imports
def _message_send(self, connection):
    """Send a single message instance.

    If TESTING is True the message will not actually be sent.

    :param self: a Message instance.
    """
    sender = current_app.config["MAIL_SENDER"]
    if not self.extra_headers:
        self.extra_headers = {}
    self.extra_headers["Sender"] = sender
    connection.send(self, sender)


patch_logger.info(flask_mail.Message.send)
flask_mail.Message.send = _message_send

mail = flask_mail.Mail()

db = SQLAlchemy()


@sa.event.listens_for(db.metadata, "before_create")
@sa.event.listens_for(db.metadata, "before_drop")
def _filter_metadata_for_connection(target, connection, **kw):
    """Listener to control what indexes get created.

    Useful for skipping postgres-specific indexes on a sqlite for example.

    It's looking for info entry `engines` on an index
Esempio n. 4
0
# application. So we can't use app.script_manager to add commands from
# plugins. If app is created before the arg parser, plugin commands are properly
# registered
_flask_script_manager_run = Manager.run


def _manager_run(self, *args, **kwargs):
    self()
    if 'sentry' in self.app.extensions:
        client = self.app.extensions['sentry'].client
        client.tags['process_type'] = 'shell'

    return _flask_script_manager_run(self, *args, **kwargs)


patch_logger.info(Manager.run)
Manager.run = _manager_run

#: ``flask_script.Manager`` instance for abilian commands
manager = Manager(usage='Abilian base commands')


def _log_config(config):
    lines = ["Application configuration:"]

    if config.get('CONFIGURED'):
        settings = get_service("settings")
        try:
            db_settings = set(settings.namespace('config').keys())
        except sa.exc.ProgrammingError:
            # there is config.py, db uri, but maybe "initdb" has yet to be run
Esempio n. 5
0
# sender
# FIXME: we'ld rather subclass Message and update all imports
def _message_send(self, connection):
  """
  Sends a single message instance. If TESTING is True the message will
  not actually be sent.

  :param message: a Message instance.
  """
  sender = current_app.config['MAIL_SENDER']
  if not self.extra_headers:
    self.extra_headers = {}
  self.extra_headers['Sender'] = sender
  connection.send(self, sender)

patch_logger.info(flask_mail.Message.send)
flask_mail.Message.send = _message_send


mail = flask_mail.Mail()


import sqlalchemy as sa
from ..sqlalchemy import SQLAlchemy
db = SQLAlchemy()

@sa.event.listens_for(db.metadata, 'before_create')
@sa.event.listens_for(db.metadata, 'before_drop')
def _filter_metadata_for_connection(target, connection, **kw):
  """
  listener to control what indexes get created.
Esempio n. 6
0
    Field.view_template = None

    _wtforms_Field_init = Field.__init__

    def _core_field_init(self, *args, **kwargs):
        view_widget = None
        if 'view_widget' in kwargs:
            view_widget = kwargs.pop('view_widget')

        _wtforms_Field_init(self, *args, **kwargs)
        if view_widget is None:
            view_widget = self.widget

        self.view_widget = view_widget

    patch_logger.info(Field.__init__)
    Field.__init__ = _core_field_init
    del _core_field_init

    def _core_field_repr(self):
        """
        `__repr__` that shows the name of the field instance. Useful for tracing field
        errors (like in Sentry).
        """
        return '<{}.{} at 0x{:x} name={!r}>'.format(self.__class__.__module__,
                                                    self.__class__.__name__,
                                                    id(self), self.name)

    patch_logger.info(Field.__module__ + '.Field.__repr__')
    Field.__repr__ = _core_field_repr
    del _core_field_repr
Esempio n. 7
0
# application. So we can't use app.script_manager to add commands from
# plugins. If app is created before the arg parser, plugin commands are properly
# registered
_flask_script_manager_run = Manager.run


def _manager_run(self, *args, **kwargs):
  self()
  if 'sentry' in self.app.extensions:
    client = self.app.extensions['sentry'].client
    client.tags['process_type'] = 'shell'

  return _flask_script_manager_run(self, *args, **kwargs)


patch_logger.info(Manager.run)
Manager.run = _manager_run

#: ``flask.ext.script.Manager`` instance for abilian commands
manager = Manager(usage='Abilian base commands')


def _log_config(config):
  lines = ["Application configuration:"]

  if config.get('CONFIGURED'):
    settings = current_app.services['settings']
    try:
      db_settings = set(settings.namespace('config').keys())
    except sa.exc.ProgrammingError:
      # there is config.py, db uri, but maybe "initdb" has yet to be run
Esempio n. 8
0
    Field.view_template = None

    _wtforms_Field_init = Field.__init__

    def _core_field_init(self: Any, *args: Any, **kwargs: Any) -> None:
        view_widget = None
        if "view_widget" in kwargs:
            view_widget = kwargs.pop("view_widget")

        _wtforms_Field_init(self, *args, **kwargs)
        if view_widget is None:
            view_widget = self.widget

        self.view_widget = view_widget

    patch_logger.info(Field.__init__)
    Field.__init__ = _core_field_init
    del _core_field_init

    def _core_field_repr(self):
        """`__repr__` that shows the name of the field instance.

        Useful for tracing field errors (like in Sentry).
        """
        return "<{}.{} at 0x{:x} name={!r}>".format(self.__class__.__module__,
                                                    self.__class__.__name__,
                                                    id(self), self.name)

    patch_logger.info(Field.__module__ + ".Field.__repr__")
    Field.__repr__ = _core_field_repr
    del _core_field_repr