class STOCK_MAPPER(object):

    def __init__(self):

        self.__product_mapper = PRODUCT_MAPPER()
        self.__entity_mapper = ENTITY_MAPPER()
        self.__unit_mapper = UNIT_MAPPER()

    def __stock_from_row(self, stock_row):

        my_logger.debug(message='%s' % stock_row)
        return STOCK(id=stock_row['stock']['id'],
                                    product=lambda: self.__product_mapper.find(stock_row['stock']['product'])[0],
                                    entity=lambda: self.__entity_mapper.find(stock_row['stock']['entity'])[0],
                                    maximum=stock_row['stock']['maximum'],
                                    maximum_unit=self.__unit_mapper.find(stock_row['stock']['maximum_unit']) if stock_row['stock']['maximum_unit'] is not None else None,
                                    minimum=stock_row['stock']['minimum'],
                                    minimum_unit=self.__unit_mapper.find(stock_row['stock']['minimum_unit']) if stock_row['stock']['minimum_unit'] is not None else None)

    def find(self, product_id, entity_id):

        if type(entity_id) is ListType:
            _stock_rows = current.db((current.db.stock.entity.belongs(tuple(entity_id))) &
                         (current.db.stock.product == product_id) &
                         (current.db.entity.id == current.db.stock.entity)).select()
        else:
            _stock_rows = current.db((current.db.stock.entity == entity_id) &
                         (current.db.stock.product == product_id) &
                         (current.db.entity.id == current.db.stock.entity)).select()

        return [self.__stock_from_row(_stock_row) for _stock_row in _stock_rows]
Beispiel #2
0
def list():
    '''
    lists entities of the authenticated user
    '''
    mylogger.debug(message='request.vars:%s' % request.vars)

    entity_mapper = ENTITY_MAPPER()
    person_mapper = PERSON_MAPPER()

    # getting the authenticated user
    auth_user = person_mapper.find(person_id=auth.user.id)[0]

    # pagination stuff
    paginate_selector = PaginateSelector(anchor='main')
    paginator = Paginator(paginate=paginate_selector.paginate,
                          extra_vars={'v': 1},
                          anchor='main',
                          renderstyle=False)
    paginator.records = entity_mapper.count_all() if auth_user.is_all_entity() \
                                                  else auth_user.compute_nb_entities()
    paginate_info = PaginateInfo(paginator.page, paginator.paginate,
                                 paginator.records)

    # if the authenticated user is in all entities (ie. 'all_entity' entity)
    # retrieving the whole entitities
    if auth_user.is_all_entity():
        auth_user_entitites = entity_mapper.find(limitby=paginator.limitby())
    # else getting the authenticated user entitities
    else:
        auth_user_entitites = entity_mapper.find(person_id=auth.user.id,
                                                 limitby=paginator.limitby())
        # adding the all_entity entity for the whole users
        auth_user_entitites.insert(0, entity_mapper.find(role='all_entity')[0])
        mylogger.debug(message='auth_user_entitites:%s' % auth_user_entitites)

    # putting 'all_entity' at the top of the list
    auth_user_entitites = sorted(
        auth_user_entitites,
        key=lambda k: k.role if k.role != 'all_entity' else '0_%s' % k.role)

    return dict(entities=auth_user_entitites,
                paginator=paginator,
                paginate_selector=paginate_selector,
                paginate_info=paginate_info)
Beispiel #3
0
def delete():
    entity_mapper = ENTITY_MAPPER()

    entity_id = request.args[0]

    mylogger.debug(message='entity_id:%s' % entity_id)

    _entity = entity_mapper.find(entity_id=entity_id)[0]

    entity_mapper.delete(_entity)

    cc.clear_menu_cache()

    redirect(
        URL(request.application,
            request.controller,
            'list_reload.html',
            args=entity_id,
            vars=request.vars))
Beispiel #4
0
def list_action():
    '''
    return actions for the entity given in parameter
    '''
    mylogger.debug(message='request.vars:%s' % request.vars)

    entity_mapper = ENTITY_MAPPER()

    _entity_id = request.args[0]

    _entity = entity_mapper.find(entity_id=_entity_id)[0]

    _updatable_entity = False
    _deletable_entity = False

    # if the is_updatable method throws an HTTP exception, conditions are not met
    try:
        is_updatable()
        _updatable_entity = True
        mylogger.debug(message='is updatable:%s' % _entity_id)
    except HTTP:
        mylogger.debug(message='is not updatable:%s' % _entity_id)

    # if the is_deletable method throws an HTTP exception, conditions are not met
    try:
        is_deletable()
        _deletable_entity = True
        mylogger.debug(message='is deletable:%s' % _entity_id)
    except HTTP:
        mylogger.debug(message='is not deletable:%s' % _entity_id)

    session.flash = None

    return dict(entity=_entity,
                updatable_entity=_updatable_entity,
                deletable_entity=_deletable_entity)
Beispiel #5
0
def _create():

    mylogger.debug(message='request.vars:%s' %request.vars)
    mylogger.debug(message='request.args:%s' %request.args)

    person_mapper = PERSON_MAPPER()
    entity_mapper = ENTITY_MAPPER()

    _person_id = request.args[0] if len(request.args) > 0 else None # an id or None
    if _person_id is None:
        _person = person_mapper.create()
    else:
        _person = person_mapper.find(person_id=_person_id)[0]

    _all_entity_id = entity_mapper.find(role='all_entity')[0].id

    form = PERSON_FORM(person=_person).get_form()

    if form.accepts(request.vars, session, dbio=False):
        mylogger.debug(message='form.vars:%s' %form.vars)

        is_virtual = 'is_virtual' in request.vars
        mylogger.debug(message='is_virtual:%s' %is_virtual)

        _person.first_name = form.vars['first_name']
        _person.last_name = form.vars['last_name']
        _person.email = form.vars['email']
        _person.contact = form.vars['email'] # initializing the contact with the email address

        if 'custom_permission' in form.vars.keys():
            _person.permissions = [ PERMISSION(name=_permission_name) for _permission_name in form.vars['custom_permission'] ]

        if 'custom_entity' in form.vars.keys():
            _custom_entity = form.vars['custom_entity']
            if type(_custom_entity) is not ListType:
                _custom_entity = [ _custom_entity ]

            if str(_all_entity_id) in _custom_entity:
                _custom_entity = [ _all_entity_id ]
            _person.entities = [ entity_mapper.find(entity_id=_entity_id)[0] for _entity_id in _custom_entity ]

        if is_virtual:
            # this is a new person
            # sending an email to the creator
            message = cc.get_string("PERSON_VIRTUAL_CREATION_MESSAGE_BODY") %(_person.first_name + ' ' + \
                                                                              _person.last_name, \
                                                                              _person.email, \
                                                                              _person.password)

            _creator = person_mapper.find(person_id=auth.user.id)[0]

            # enabling the new person
            _person.enable()
            _person.virtual=True

            mail_sent = mail.send(_creator.email, subject= cc.get_string("PERSON_VIRTUAL_CREATION_MESSAGE_SUBJECT"), message=message)

            if mail_sent:
                # saving the user
                _new_person_id = person_mapper.save_or_update(_person)

                session.flash=cc.get_string("EMAIL_SENT")
            else:
                del(_person)

                session.flash=cc.get_string("ERROR") + mail.error

        # sending an email to the new user
        elif _person.new_person:

            message = cc.get_string("PERSON_CREATION_MESSAGE_BODY") %(_person.first_name + ' ' + \
                                                        _person.last_name, \
                                                        _person.email, \
                                                        settings['application_url'], \
                                                        _person.password_key)

            mail_sent = mail.send(_person.email, subject= cc.get_string("PERSON_CREATION_MESSAGE_SUBJECT"), message=message)

            if mail_sent:
                # saving the user
                _new_person_id = person_mapper.save_or_update(_person)

                session.flash=cc.get_string("EMAIL_SENT")
            else:
                del(_person)

                mylogger.error(message='mail.error:%s' % mail.error)
                session.flash=cc.get_string("ERROR") + str(mail.error)

                redirect(URL(request.application, request.controller, 'page_reload'))
        else:
            # saving the user
            _new_person_id = person_mapper.save_or_update(_person)

            session.flash=cc.get_string("PERSON_UPDATED")

        mylogger.debug(message='_person:%s' %_person)
        cc.clear_menu_cache()

        if _person_id is not None:
            redirect(URL(request.application, request.controller, 'list_reload', args=_person.id, vars=request.vars))
        else:
            redirect(URL(request.application, request.controller, 'page_reload', vars={'member': _new_person_id, 'display_by': 'person'}))

    else:

        return dict(form=form, all_entities_id=_all_entity_id)