def removeAccessPermission(self, json_dict):
        if self.checkRights(json_dict[E_MAIL_MELLON]) is False:
            return None

        e_mail, patient_id = AuthorizationHandler.extractAccessPermissionParams(
            json_dict)

        # Patient must exist...
        patient = sm.query(Patient).filter_by(orthanc_pid=patient_id).first()
        user = filter(lambda x: x.e_mail == e_mail, patient.permitted_users)

        if len(user) == 0:
            # @TODO log this
            print "User %s has no priviledges to remove at patient %s" % (
                e_mail, patient_id)
        else:
            user = user[0]
            patient.permitted_users.remove(user)

            sm.add(patient)
            sm.commit()

        # to_delete = sm.query(AccessPermission).filter_by(patient_id=patient_id, user_id=user_id).first()
        # sm.delete(to_delete)
        # sm.commit()

        pass
    def removeUser(self, json_dict):
        e_mail, access_level, full_name = AuthorizationHandler.extractUserModification(
            json_dict)
        to_delete = sm.query(OrthancUser).filter_by(e_mail=e_mail).first()

        # @TODO remove associated priviledges

        if to_delete:
            sm.delete(to_delete)
            sm.commit()
    def removePatient(self, json_dict):
        patient_id = AuthorizationHandler.extractPatient(json_dict)

        to_delete = sm.query(Patient).filter_by(orthanc_pid=patient_id).first()

        # @TODO remove associated priviledges

        if to_delete is not None:
            sm.delete(to_delete)
            sm.commit()
        pass
    def addPatient(self, json_dict):
        patient_id = AuthorizationHandler.extractPatient(json_dict)

        if sm.query(Patient).filter_by(orthanc_pid=patient_id).first() is None:
            # ADD PATIENT
            patient = Patient(patient_id, ACCESS_LEVELS.NURSE)
            sm.add(patient)
            sm.commit()
            pass
        else:
            # DO NOTHING
            # @TODO Log this instead of printing
            print "Patient %s already registered" % patient_id
            pass
    def addAccessPermission(self, json_dict):
        if self.checkRights(json_dict[E_MAIL_MELLON]) is False:
            return None

        e_mail, patient_id = AuthorizationHandler.extractAccessPermissionParams(
            json_dict)

        # Patient must exist...
        patient = sm.query(Patient).filter_by(orthanc_pid=patient_id).first()
        user = sm.query(OrthancUser).filter_by(e_mail=e_mail).first()
        patient.permitted_users.append(user)

        sm.add(patient)
        sm.commit()
        pass
    def updateUser(self, json_dict):
        e_mail, access_level, full_name = extractUserModification(json_dict)

        to_update = sm.query(OrthancUser).filter_by(e_mail=e_mail).first()

        if to_update.full_name != full_name:
            return

        if to_update.access_level != access_level:
            to_update.access_level = access_level
            pass

        # Update
        sm.add(to_update)
        sm.commit()
    def addUser(self, json_dict):
        uid = list(map(lambda x: x.uid, sm.query(OrthancUser)))
        if len(uid) == 0:
            uid = 0
        else:
            uid = max(uid)
            uid = uid + 1
        e_mail, access_level, full_name = AuthorizationHandler.extractUserModification(
            json_dict)

        user = sm.query(OrthancUser).filter_by(e_mail=e_mail).first()
        if user is None:
            new_user = OrthancUser(uid, full_name, e_mail, access_level)
            sm.add(new_user)
            sm.commit()
        else:
            # UPDATE
            user.access_level = access_level
            user.full_name = full_name
            sm.commit()
            pass