Exemplo n.º 1
0
def filter_groups(context: 'micro_context', term: str='',
                  offset: int=0, limit: int=10, id: int=None):
    with context.session as session:
        if id:
            group = session.query(model.Group).get(id)
            result = serialize(group) if group else None
            return result
        term = "{}%".format(term)
        groups = session.query(model.Group).\
            filter(model.Group.name.like(term)).\
            order_by(model.Group.name).\
            offset(offset).\
            limit(limit)

        return [serialize(group) for group in groups]
Exemplo n.º 2
0
def save_user(context: 'micro_context',
              user_id: int,
              name: str=None,
              email: str=None,
              password: str=None):
    with context.session as session:
        user = session.query(model.User).get(user_id)
        if user:
            if name is not None:
                user.name = name
            if email is not None:
                user.email = email
            if password is not None:
                user.password = password
            signal = "user-changed"
        else:
            user = model.User(id=user_id,
                              name=name,
                              email=email,
                              password=password)
            signal = "user-added"
            session.add(user)
        session.commit()
        result = serialize(user)
        del result['password']
        context.broadcast(signal, result)
        return result
Exemplo n.º 3
0
def save_service(context: 'micro_context',
                 service_id: int,
                 name: str,
                 cookie_url: str,
                 cors: str):
    with context.session as session:
        service = session.query(model.Service).get(service_id)
        if service:
            service.name = name
            service.cookie_url = cookie_url
            service.cors = cors
            service.token = gen_token()
            signal = "service-changed"
        else:
            service = model.Service(id=service_id,
                                    name=name,
                                    cookie_url=cookie_url,
                                    cors=cors,
                                    token=gen_token())
            signal = "service-added"
            session.add(service)
        session.commit()
        result = serialize(service)
        context.broadcast(signal, result)
        return result
Exemplo n.º 4
0
def filter_groups(context: 'micro_context', term='',
                  offset=0, limit=10, id=None) -> list:
    '''
        returns a list of groups filtered by
        group.name starts with term
    '''
    with context.session as session:
        if id is not None:
            group = session.query(model.Group).get(id)
            result = serialize(group)
            return result
        term = "{}%".format(term)
        groups = session.query(model.Group).\
            filter(model.Group.name.like(term))

        return [serialize(group) for group in groups]
Exemplo n.º 5
0
def filter_users(context: "micro_context", term="", offset=0, limit=10, id=None):
    with context.session as session:
        if id is not None:
            user = session.query(model.User).get(id)
            result = serialize(user)
            del user["password"]
            subs = active_subscriptions_for(session, user_id=id)
            result.subscriptions = [sub_to_json(o) for o in subs]
            return result
        term = "{}%".format(term)
        users = session.query(model.User).filter(or_(model.User.name.like(term), model.User.email.like(term)))

        result = []
        for user in users:
            item = serialize(user)
            del item["password"]
            result.append(item)
        return result
Exemplo n.º 6
0
def image_list(context: 'micro_context', term: str='',
               offset: int=0, limit: int=10) -> list:
    ''' retrieves a list of images filtered by term on name '''
    with context.session as session:
        term = "{}%".format(term)
        images = session.query(model.Image).\
            filter(model.Image.name.like(term)).\
            order_by(model.Image.name).\
            offset(offset).\
            limit(limit)

        return [serialize(image) for image in images]
Exemplo n.º 7
0
def save_group(context: 'micro_context',
               name: str,
               id: int=None):
    with context.session as session:
        if id:
            group = session.query(model.Group).get(id)
            group.name = name
            signal = "group-changed"
        else:
            group = model.Group(name=name)
            signal = "group-added"
            session.add(group)
        session.commit()
        result = serialize(group)
        context.broadcast(signal, result)
        return result
Exemplo n.º 8
0
def save_group(context: 'micro_context',
               name: str,
               id: int=None):
    '''
        Adds a group to the database if
        it is not already there, otherwise
        it updates it.
    '''
    with context.session as session:
        if id:
            group = session.query(model.Group).get(id)
            group.name = name
            signal = "group-changed"
        else:
            group = model.Group(name=name)
            signal = "group-added"
            session.add(group)
        session.commit()
        result = serialize(group)
        context.broadcast(signal, result)
        return result
Exemplo n.º 9
0
def make_payment(context: 'micro_context',
                 user_id: int=None,
                 group_id: int=None):
    if group_id is not None and user_id is not None:
        raise Exception("only a user or a group can pay for subscription")

    with context.session as session:
        subscriptions = session.query(model.Subscription).\
            filter(model.Subscription.from_date.is_(None))
        if user_id is not None:
            subscriptions = subscriptions.filter(
                model.Subscription.user_id == user_id)
        elif group_id is not None:
            subscriptions = subscriptions.filter(
                model.Subscription.group_id == group_id)
        subscriptions = list(subscriptions)
        if not subscriptions:
            raise Exception("Nothing to pay")

        cost = sum([row.cost for row in subscriptions])
        payee = Payment.PAYEE[0] if user_id is not None else Payment.PAYEE[1]
        payment = model.Payment(amount=cost,
                                payee=payee,
                                date=datetime.date.today())
        session.add(payment)
        for subscription in subscriptions:
            subscription.payment = payment
            subscription.from_date = payment.date
            if subscription.service.duration:
                days = subscription.service.duration
                to_date = payment.date + datetime.timedelta(days=days)
                subscription.to_date = to_date
        session.commit()
        for subscription in subscriptions:
            message = sub_to_json(subscription)
            context.broadcast('subscription-active', message)
        return serialize(payment)
Exemplo n.º 10
0
def sub_to_json(o):
    result = serialize(o)
    result["user"] = o.user.name if o.user else None
    result["group"] = o.group.name if o.group else None
    result["service"] = o.service.name if o.service else None
    return result