示例#1
0
def add_account_to_circle(account: Account, circle: Circle, admin: Account,
                          comment: str):
    logger.info("Adding %s to circle %s, admin=%s" %
                (account.username, circle.name, admin.username))

    _assert_can_admin_circle(admin, circle)

    circle.add_member(account, comment)
示例#2
0
def remove_account_from_circle(account: Account, circle: Circle,
                               admin: Account):
    logger.info("Removing %s from circle %s, admin=%s" %
                (account.username, circle.name, admin.username))

    _assert_can_admin_circle(admin, circle)

    circle.remove_member(account)
示例#3
0
def remove_account_from_circle(account: Account, circle: Circle,
                               admin: Account):
    logger.info("Removing %s from circle %s, admin=%s" %
                (account.username, circle.name, admin.username))

    _assert_can_admin_circle(admin, circle)

    if not is_account_in_circle(account, circle):
        raise P2k16UserException(
            "Account isn't a member of the circle, cannot be removed")

    circle.remove_member(account)
示例#4
0
def add_account_to_circle(account: Account, circle: Circle, admin: Account,
                          comment: str):
    logger.info("Adding %s to circle %s, admin=%s" %
                (account.username, circle.name, admin.username))

    _assert_can_admin_circle(admin, circle)

    if is_account_in_circle(account, circle):
        raise P2k16UserException(
            "Account is already a member of the cirlce, cannot be added again")

    circle.add_member(account, comment)
示例#5
0
def _data_tool_save():
    circle_name = request.json["circle"]
    circle = Circle.find_by_name(circle_name)

    if not circle:
        raise P2k16UserException("No such circle: {}".format(circle_name))

    _id = request.json.get("id", None)

    if _id:
        tool = ToolDescription.find_by_id(_id)

        if tool is None:
            abort(404)

        logger.info("Updating tool: {}".format(tool))
        tool.name = request.json["name"]
        tool.circle = circle
        tool.description = request.json["description"]
    else:
        logger.info("Creating new tooldescription: {}".format(
            request.json["name"]))
        tool = ToolDescription(request.json["name"],
                               request.json["description"], circle)

    db.session.add(tool)
    db.session.commit()
    db.session.flush()
    logger.info("Update tool: {}".format(tool.name))

    return jsonify(tool_to_json(tool))
示例#6
0
def data_account_summary(account_id):
    account = Account.find_account_by_id(account_id)

    if account is None:
        abort(404)

    badges = badge_management.badges_for_account(account.id)

    open_door_event = event_management.last_door_open(account)

    # Add to response
    from .badge_blueprint import badge_to_json
    summary = {
        "account": account_to_json(account),
        "badges": [badge_to_json(b) for b in badges],
        "lastDoorOpen": open_door_event.to_dict() if open_door_event else None,
    }

    # Add information about membership if current user is in a circle
    admin_circle = Circle.get_by_name('insight-fee')
    if account_management.is_account_in_circle(
            flask_login.current_user.account, admin_circle):
        membership = get_membership(account)
        membership_details = {}
        if membership is not None:
            membership_details['fee'] = membership.fee
            membership_details[
                'first_membership'] = membership.first_membership
            membership_details[
                'start_membership'] = membership.start_membership
        else:
            membership_details['fee'] = 0
        summary['membership'] = membership_details

    return jsonify(summary)
示例#7
0
    def open_doors(self, account: Account, doors: List[Door]):
        door_circle = Circle.get_by_name('door')

        if not account_management.is_account_in_circle(account, door_circle):
            raise P2k16UserException('{} is not in the door circle'.format(
                account.display_name()))

        publishes = []

        if not event_management.has_opened_door(account):
            system = Account.find_account_by_username("system")
            logger.info("First door opening for {}".format(account))
            badge_management.create_badge(account, system,
                                          "first-door-opening")

        for door in doors:
            logger.info(
                'Opening door. username={}, door={}, open_time={}'.format(
                    account.username, door.key, door.open_time))
            event_management.save_event(OpenDoorEvent(door.key))
            publishes.append((self.prefix + door.topic, str(door.open_time)))

        # Make sure everything has been written to the database before actually opening the door.
        db.session.flush()

        # TODO: move this to a handler that runs after the transaction is done
        # TODO: we can look at the responses and see if they where successfully sent/received.
        for topic, open_time in publishes:
            logger.info("Sending message: {}: {}".format(topic, open_time))
            self._client.publish(topic, open_time)
示例#8
0
def _load_circle_admin(account_id, circle_id, admin_id):
    account = Account.find_account_by_id(account_id)
    admin = Account.find_account_by_id(admin_id)
    circle = Circle.find_by_id(circle_id)

    if account is None or admin is None or circle is None:
        raise P2k16UserException('Bad values')

    return account, admin, circle
示例#9
0
def _check_is_circle_admin(circle: Circle, admin: Account):
    admin_circle = Circle.find_by_name(circle.name + '-admin')

    if admin_circle is None:
        raise P2k16UserException(
            'There is no admin circle (%s-admin") for circle "%s"'.format(
                circle.name, circle.name))

    if not is_account_in_circle(admin, admin_circle):
        raise P2k16UserException('Account %s is not an administrator of %s' %
                                 (admin.username, circle.description))
示例#10
0
def remove_circle(admin: Account, circle: Circle):
    _assert_can_admin_circle(admin, circle)

    logger.info("Removing circle, id={}, admin={}".format(circle.id, admin.id))
    c = Circle.get_by_id(circle.id)

    if c.management_style == CircleManagementStyle.SELF_ADMIN:
        ok = len(c.members) == 1 and c.members[0].account == admin

        if not ok:
            raise P2k16UserException(
                "A circle which is self-administrated must only contain the remover."
            )

    elif c.management_style == CircleManagementStyle.ADMIN_CIRCLE:
        if len(c.members) != 0:
            raise P2k16UserException(
                "The circle has to be empty to be removed.")
    else:
        raise P2k16UserException("Unknown management style")

    Circle.delete_by_id(circle.id)
示例#11
0
def _manage_circle_membership(create: bool):
    username = request.json.get("accountUsername", None)

    if username is not None:
        account = Account.get_by_username(username)
    else:
        account = Account.get_by_id(request.json["accountId"])

    circle = Circle.get_by_id(request.json["circleId"])

    admin = flask_login.current_user.account

    if create:
        comment = request.json.get("comment", "")
        account_management.add_account_to_circle(account, circle, admin, comment)
    else:
        account_management.remove_account_from_circle(account, circle, admin)

    db.session.commit()
    return jsonify(circle_to_json(circle, include_members=True))
示例#12
0
def create_circle(name: str,
                  description: str,
                  comment_required_for_membership,
                  management_style: CircleManagementStyle,
                  admin_circle_name: Optional[str] = None,
                  username: Optional[str] = None,
                  comment: Optional[str] = None) -> Circle:
    c = Circle(name, description, comment_required_for_membership,
               management_style)

    if management_style == CircleManagementStyle.ADMIN_CIRCLE:
        if admin_circle_name is None:
            raise P2k16UserException(
                "An admin circle is required when management style is set to ADMIN_CIRCLE"
            )

        admin_circle = Circle.find_by_name(admin_circle_name)

        if admin_circle is None:
            raise P2k16UserException(
                "No such circle: {}".format(admin_circle_name))

        c.admin_circle = admin_circle

    elif management_style == CircleManagementStyle.SELF_ADMIN:
        if username is None:
            raise P2k16UserException(
                "An initial member's username is required when management style is set to "
                "SELF_ADMIN")

        account = Account.find_account_by_username(username)

        if account is None:
            raise P2k16UserException("No such account: {}".format(username))

        c.add_member(account, comment or "")

    db.session.add(c)

    return c
示例#13
0
def data_circle(circle_id):
    circle = Circle.get_by_id(circle_id)
    return jsonify(circle_to_json(circle, include_members=True))