예제 #1
0
 def _save(self):
     blocking = self._blocking
     blocking.reason = self._form.reason.data
     # Just overwrite the whole list
     blocking.allowed = [
         BlockingPrincipal(entity_type=item['_type'], entity_id=item['id'])
         for item in self._form.principals.data
     ]
     # Blocked rooms need some more work as we can't just overwrite them
     old_blocked = {br.room_id for br in blocking.blocked_rooms}
     new_blocked = set(self._form.blocked_rooms.data)
     added_blocks = new_blocked - old_blocked
     removed_blocks = old_blocked - new_blocked
     for room_id in removed_blocks:
         blocked_room = next(br for br in blocking.blocked_rooms
                             if br.room_id == room_id)
         blocking.blocked_rooms.remove(blocked_room)
     added_blocked_rooms = []
     for room_id in added_blocks:
         blocked_room = BlockedRoom(room_id=room_id)
         blocking.blocked_rooms.append(blocked_room)
         added_blocked_rooms.append(blocked_room)
     db.session.flush()
     flash(_(u'Blocking updated'), 'success')
     self._process_blocked_rooms(added_blocked_rooms)
예제 #2
0
def _populate_blocking(blocking, room_ids, allowed_principals, reason):
    blocking.blocked_rooms = [BlockedRoom(room_id=room.id) for room in Room.query.filter(Room.id.in_(room_ids))]
    blocking.reason = reason
    blocking.allowed = [GroupProxy(_group_id_or_name(pr), provider=pr['provider'])
                        if pr.get('is_group')
                        else User.get_one(pr['id'])
                        for pr in allowed_principals]
예제 #3
0
def create_blocking(rooms, start_date, end_date, reason, allowed_principals):
    blocking = Blocking()
    blocking.start_date = start_date
    blocking.end_date = end_date
    blocking.reason = reason
    blocking.created_by_user = session.user
    blocking.allowed = allowed_principals
    blocking.blocked_rooms = [BlockedRoom(room_id=room.id) for room in rooms]
    db.session.add(blocking)
    db.session.flush()
    return blocking
예제 #4
0
 def _save(self):
     self._blocking = blocking = Blocking()
     blocking.start_date = self._form.start_date.data
     blocking.end_date = self._form.end_date.data
     blocking.created_by_user = session.user
     blocking.reason = self._form.reason.data
     blocking.allowed = self._form.principals.data
     blocking.blocked_rooms = [BlockedRoom(room_id=room_id) for room_id in self._form.blocked_rooms.data]
     db.session.add(blocking)
     db.session.flush()  # synchronizes relationships (e.g. BlockedRoom.room)
     flash(_(u'Blocking created'), 'success')
     self._process_blocked_rooms(blocking.blocked_rooms)
예제 #5
0
    def migrate_blockings(self):
        state_map = {
            None: BlockedRoom.State.pending,
            False: BlockedRoom.State.rejected,
            True: BlockedRoom.State.accepted
        }

        print cformat('%{white!}migrating blockings')
        for old_blocking_id, old_blocking in self.rb_root['RoomBlocking'][
                'Blockings'].iteritems():
            b = Blocking(id=old_blocking.id,
                         created_by_id=self.merged_avatars.get(
                             old_blocking._createdBy, old_blocking._createdBy),
                         created_dt=as_utc(old_blocking._utcCreatedDT),
                         start_date=old_blocking.startDate,
                         end_date=old_blocking.endDate,
                         reason=convert_to_unicode(old_blocking.message))

            print cformat(u'- %{cyan}{}').format(b.reason)
            for old_blocked_room in old_blocking.blockedRooms:
                br = BlockedRoom(
                    state=state_map[old_blocked_room.active],
                    rejected_by=old_blocked_room.rejectedBy,
                    rejection_reason=convert_to_unicode(
                        old_blocked_room.rejectionReason),
                )
                room = Room.get(get_room_id(old_blocked_room.roomGUID))
                room.blocked_rooms.append(br)
                b.blocked_rooms.append(br)
                print cformat(u'  %{blue!}Room:%{reset} {} ({})').format(
                    room.full_name,
                    BlockedRoom.State(br.state).title)

            for old_principal in old_blocking.allowed:
                principal_id = old_principal._id
                if old_principal._type == 'Avatar':
                    principal_id = int(
                        self.merged_avatars.get(old_principal._id,
                                                old_principal._id))
                    principal_type = 'User'
                else:
                    principal_type = 'Group'
                bp = BlockingPrincipal(
                    _principal=[principal_type, principal_id])
                b._allowed.add(bp)
                print cformat(u'  %{blue!}Allowed:%{reset} {}({})').format(
                    bp.entity_type, bp.entity_id)
            db.session.add(b)
        db.session.commit()
예제 #6
0
 def _create_blocking(**params):
     room = params.pop('room', dummy_room)
     state = params.pop('state', BlockedRoom.State.pending)
     params.setdefault('start_date', date.today())
     params.setdefault('end_date', date.today())
     params.setdefault('reason', u'Blocked')
     params.setdefault('created_by_user', dummy_user)
     blocking = Blocking(**params)
     if room is not None:
         br = BlockedRoom(room=room, state=state, blocking=blocking)
         if state == BlockedRoom.State.accepted:
             br.approve(notify_blocker=False)
     db.session.add(blocking)
     db.session.flush()
     return blocking
def _update_blocked_rooms(blocking, room_ids):
    old_blocked = {br.room_id for br in blocking.blocked_rooms}
    new_blocked = set(room_ids)
    added_blocks = new_blocked - old_blocked
    removed_blocks = old_blocked - new_blocked
    blocked_rooms_by_room = {br.room_id: br for br in blocking.blocked_rooms}
    for room_id in removed_blocks:
        blocking.blocked_rooms.remove(blocked_rooms_by_room[room_id])
    added_blocked_rooms = set()
    rooms = {r.id: r for r in Room.query.filter(~Room.is_deleted, Room.id.in_(added_blocks))}
    for room_id in added_blocks:
        blocked_room = BlockedRoom(room=rooms[room_id])
        blocking.blocked_rooms.append(blocked_room)
        added_blocked_rooms.add(blocked_room)
    db.session.flush()
    _approve_or_request_rooms(blocking, added_blocked_rooms)
예제 #8
0
 def _save(self):
     blocking = self._blocking
     blocking.reason = self._form.reason.data
     # Update the ACL. We don't use `=` here to prevent SQLAlchemy
     # from deleting and re-adding unchanged entries.
     blocking.allowed |= self._form.principals.data
     blocking.allowed &= self._form.principals.data
     # Add/remove blocked rooms if necessary
     old_blocked = {br.room_id for br in blocking.blocked_rooms}
     new_blocked = set(self._form.blocked_rooms.data)
     added_blocks = new_blocked - old_blocked
     removed_blocks = old_blocked - new_blocked
     for room_id in removed_blocks:
         blocked_room = next(br for br in blocking.blocked_rooms if br.room_id == room_id)
         blocking.blocked_rooms.remove(blocked_room)
     added_blocked_rooms = []
     for room_id in added_blocks:
         blocked_room = BlockedRoom(room_id=room_id)
         blocking.blocked_rooms.append(blocked_room)
         added_blocked_rooms.append(blocked_room)
     db.session.flush()
     flash(_(u'Blocking updated'), 'success')
     self._process_blocked_rooms(added_blocked_rooms)
예제 #9
0
def test_state_name(state):
    assert BlockedRoom(state=state).state_name == state.title