def migrate_datas():
    logger = logging.getLogger("alembic.autonomie")
    from autonomie_base.models.base import DBSESSION
    session = DBSESSION()
    from autonomie.models.config import Config
    import json

    from autonomie.models.files import (
        File,
        FileType,
    )
    json_str = Config.get_value("attached_filetypes", "[]")
    try:
        configured_filetypes = json.loads(json_str)
    except:
        logger.exception(u"Error in json str : %s" % json_str)
        configured_filetypes = []
    if configured_filetypes:
        result = []
        for filetype_label in configured_filetypes:
            if filetype_label:
                filetype = FileType(label=filetype_label)
                session.add(filetype)
                session.flush()
                result.append(filetype)

        for typ_ in result:
            query = File.query().filter_by(label=typ_.label)
            for file_ in query:
                file_.file_type_id = typ_.id
                session.merge(file_)
        session.flush()
Ejemplo n.º 2
0
 def __call__(self):
     return dict(
         business_types=BusinessType.query().all(),
         file_types=FileType.query().all(),
         items=self._collect_items(),
         breadcrumb=self.breadcrumb,
         back_link=self.back_link,
         help_message=self.help_message,
     )
Ejemplo n.º 3
0
 def __call__(self):
     return dict(
         business_types=BusinessType.query().all(),
         file_types=FileType.query().all(),
         items=self._collect_items(),
         breadcrumb=self.breadcrumb,
         back_link=self.back_link,
         help_message=self.help_message,
     )
Ejemplo n.º 4
0
def test_file_type_add(config, get_csrf_request_with_db, dbsession):
    from autonomie.views.admin.main.file_types import (
        FileTypeAddView,
        FILE_TYPE_ROUTE,
    )
    from autonomie.models.files import FileType
    config.add_route(FILE_TYPE_ROUTE, FILE_TYPE_ROUTE)
    appstruct = {
        'label': u"Label",
    }
    view = FileTypeAddView(get_csrf_request_with_db())
    view.submit_success(appstruct)
    element = FileType.get_by_label("Label")
    assert element is not None
Ejemplo n.º 5
0
def test_file_type_delete(config, get_csrf_request_with_db, dbsession,
                          file_type):
    from autonomie.views.admin.main.file_types import (
        FileTypeDeleteView,
        FILE_TYPE_ROUTE,
    )
    from autonomie.models.files import FileType
    config.add_route(FILE_TYPE_ROUTE, FILE_TYPE_ROUTE)
    fid = file_type.id
    req = get_csrf_request_with_db()
    req.context = file_type
    view = FileTypeDeleteView(req)
    view()
    dbsession.flush()

    assert FileType.get(fid) is None
Ejemplo n.º 6
0
def test_file_type_add(config, get_csrf_request_with_db, dbsession):
    from autonomie.views.admin.main.file_types import (
        FileTypeAddView,
        FILE_TYPE_ROUTE,
    )
    from autonomie.models.files import FileType
    config.add_route(FILE_TYPE_ROUTE, FILE_TYPE_ROUTE)
    appstruct = {
        'label': u"Label",
    }
    view = FileTypeAddView(
        get_csrf_request_with_db()
    )
    view.submit_success(appstruct)
    element = FileType.get_by_label("Label")
    assert element is not None
Ejemplo n.º 7
0
def test_file_type_delete(config, get_csrf_request_with_db, dbsession,
                          file_type):
    from autonomie.views.admin.main.file_types import (
        FileTypeDeleteView,
        FILE_TYPE_ROUTE,
    )
    from autonomie.models.files import FileType
    config.add_route(FILE_TYPE_ROUTE, FILE_TYPE_ROUTE)
    fid = file_type.id
    req = get_csrf_request_with_db()
    req.context = file_type
    view = FileTypeDeleteView(req)
    view()
    dbsession.flush()

    assert FileType.get(fid) is None
Ejemplo n.º 8
0
    def _collect_available_types(self, context):
        """
        Collect file types that may be loaded for the given context

        :param obj context: The current object we're attaching a file to
        :returns: A list of FileType instances
        """
        result = []
        if isinstance(context, File):
            context = context.parent

        if isinstance(context, Task) or isinstance(context, Business):
            business_type_id = context.business_type_id

            result = BusinessTypeFileType.get_file_type_options(
                business_type_id, context.type_)
        else:
            result = FileType.query().options(load_only('id', 'label')).all()
        return result
Ejemplo n.º 9
0
    def _collect_available_types(self, context):
        """
        Collect file types that may be loaded for the given context

        :param obj context: The current object we're attaching a file to
        :returns: A list of FileType instances
        """
        result = []
        if isinstance(context, File):
            context = context.parent

        if isinstance(context, Task) or isinstance(context, Business):
            business_type_id = context.business_type_id

            result = BusinessTypeFileType.get_file_type_options(
                business_type_id, context.type_
            )
        else:
            result = FileType.query().options(load_only('id', 'label')).all()
        return result
Ejemplo n.º 10
0
    def get_file_type_options(cls, business_type_id, doctype):
        """
        Collect FileTypes associated to (business_type_id, doctype)

        :param int business_type_id: The business type id
        :param str doctype: One of the available doctypes
        :returns: A :class:`sqlalchemy.orm.Query`
        """
        id_query = cls.query('file_type_id')
        id_query = id_query.filter_by(business_type_id=business_type_id)
        id_query = id_query.filter_by(doctype=doctype)
        ids = [i[0] for i in id_query]

        result = []
        if ids is not None:
            from autonomie.models.files import FileType
            query = FileType.query().options(load_only('id', 'label')).filter(
                FileType.id.in_(ids)
            )
            result = query.all()
        return result
Ejemplo n.º 11
0
def _add_filetype_and_reqs(session, business_type_label, filetype,
                           requirements):
    """
    """
    from autonomie.models.files import FileType
    from autonomie.models.project.types import BusinessType
    from autonomie.models.project.file_types import BusinessTypeFileType
    if session.query(FileType.id).filter_by(label=filetype).count() == 0:
        f = FileType(label=filetype)
        session.add(f)
        session.flush()
        btype_id = session.query(
            BusinessType.id).filter_by(name=business_type_label).scalar()

        for req_dict in requirements:
            req = BusinessTypeFileType(file_type_id=f.id,
                                       business_type_id=btype_id,
                                       doctype=req_dict['doctype'],
                                       requirement_type=req_dict['req_type'],
                                       validation=req_dict.get(
                                           'validation', False))
            session.add(req)
        session.flush()
Ejemplo n.º 12
0
def file_type(dbsession):
    from autonomie.models.files import FileType
    f = FileType(label=u"Label")
    dbsession.add(f)
    dbsession.flush()
    return f
Ejemplo n.º 13
0
 def func(label):
     model = FileType(label=label)
     dbsession.add(model)
     dbsession.flush()
     return model