Esempio n. 1
0
    def create_superuser_and_provision_device(self, username, dataset_id):
        # Prompt user to pick a superuser if one does not currently exist
        while not DevicePermissions.objects.filter(is_superuser=True).exists():
            # specify username of account that will become a superuser
            if not username:
                username = input(
                    "Please enter username of account that will become the superuser on this device: "
                )
            if not FacilityUser.objects.filter(username=username).exists():
                self.stderr.write(
                    "User with username {} does not exist".format(username)
                )
                username = None
                continue

            # make the user with the given credentials, a superuser for this device
            user = FacilityUser.objects.get(username=username, dataset_id=dataset_id)

            # create permissions for the authorized user
            DevicePermissions.objects.update_or_create(
                user=user, defaults={"is_superuser": True, "can_manage_content": True}
            )

        # if device has not been provisioned, set it up
        if not device_provisioned():
            provision_device()
Esempio n. 2
0
    def provisiondevice(self, request):
        """
        After importing a Facility and designating/creating a super admins,
        provision the device using that facility
        """

        # TODO validate the data
        device_name = request.data.get("device_name", "")
        language_id = request.data.get("language_id", "")

        # Get the imported facility (assuming its the only one at this point)
        the_facility = Facility.objects.get()

        # Use the facility's preset to determine whether to allow guest access
        allow_guest_access = the_facility.dataset.preset != "formal"

        # Finally: Call provision_device
        provision_device(
            device_name=device_name,
            language_id=language_id,
            default_facility=the_facility,
            allow_guest_access=allow_guest_access,
        )

        return Response({})
Esempio n. 3
0
def create_device_settings(language_id=None, facility=None, interactive=False):
    if language_id is None and interactive:
        language_id = get_user_response(
            "Enter a default language code [{langs}]: ".format(
                langs=",".join(languages.keys())),
            valid_answers=languages,
        )
    provision_device(language_id=language_id, default_facility=facility)
Esempio n. 4
0
def provision_single_user_device(user_id):

    user = FacilityUser.objects.get(id=user_id)

    # if device has not been provisioned, set it up
    if not device_provisioned():
        provision_device(default_facility=user.facility)

    DevicePermissions.objects.get_or_create(user=user,
                                            defaults={
                                                "is_superuser": False,
                                                "can_manage_content": True
                                            })
Esempio n. 5
0
 def create(self, validated_data):
     """
     Endpoint for initial setup of a device.
     Expects a value for:
     default language - the default language of this Kolibri device
     facility - the required fields for setting up a facility
     facilitydataset - facility configuration options
     superuser - the required fields for a facilityuser who will be set as the super user for this device
     """
     with transaction.atomic():
         facility = Facility.objects.create(
             **validated_data.pop("facility"))
         preset = validated_data.pop("preset")
         dataset_data = mappings[preset]
         for key, value in dataset_data.items():
             setattr(facility.dataset, key, value)
         # overwrite the settings in dataset_data with validated_data.settings
         custom_settings = validated_data.pop("settings")
         for key, value in custom_settings.items():
             if value is not None:
                 setattr(facility.dataset, key, value)
         facility.dataset.save()
         superuser_data = validated_data.pop("superuser")
         superuser_data["facility"] = facility
         superuser = FacilityUserSerializer(
             data=superuser_data).create(superuser_data)
         superuser.set_password(superuser_data["password"])
         superuser.save()
         facility.add_role(superuser, ADMIN)
         DevicePermissions.objects.create(user=superuser, is_superuser=True)
         language_id = validated_data.pop("language_id")
         allow_guest_access = validated_data.pop("allow_guest_access")
         if allow_guest_access is None:
             allow_guest_access = preset != "formal"
         provision_device(
             device_name=validated_data["device_name"],
             language_id=language_id,
             default_facility=facility,
             allow_guest_access=allow_guest_access,
         )
         return {
             "facility": facility,
             "preset": preset,
             "superuser": superuser,
             "language_id": language_id,
             "settings": custom_settings,
             "allow_guest_access": allow_guest_access,
         }
Esempio n. 6
0
def create_superuser_and_provision_device(username,
                                          dataset_id,
                                          noninteractive=False):
    facility = Facility.objects.get(dataset_id=dataset_id)
    # if device has not been provisioned, set it up
    if not device_provisioned():
        provision_device(default_facility=facility)

    # Prompt user to pick a superuser if one does not currently exist
    while not DevicePermissions.objects.filter(is_superuser=True).exists():
        # specify username of account that will become a superuser
        if not username:
            if (
                    noninteractive
            ):  # we don't want to setup a device without a superuser, so create a temporary one
                superuser = FacilityUser.objects.create(username="******",
                                                        facility=facility)
                superuser.set_password("password")
                superuser.save()
                DevicePermissions.objects.create(user=superuser,
                                                 is_superuser=True,
                                                 can_manage_content=True)
                print(
                    "Temporary superuser with username: `superuser` and password: `password` created"
                )
                return
            username = input(
                "Please enter username of account that will become the superuser on this device: "
            )
        if not FacilityUser.objects.filter(username=username).exists():
            print(
                "User with username `{}` does not exist on this device".format(
                    username))
            username = None
            continue

        # make the user with the given credentials, a superuser for this device
        user = FacilityUser.objects.get(username=username,
                                        dataset_id=dataset_id)

        # create permissions for the authorized user
        DevicePermissions.objects.update_or_create(user=user,
                                                   defaults={
                                                       "is_superuser": True,
                                                       "can_manage_content":
                                                       True
                                                   })
Esempio n. 7
0
    def create(self, validated_data):
        """
        Endpoint for initial setup of a device.
        Expects a value for:
        default language - the default language of this Kolibri device
        facility - the required fields for setting up a facility
        facilitydataset - facility configuration options
        superuser - the required fields for a facilityuser who will be set as the super user for this device
        """
        with transaction.atomic():
            facility = Facility.objects.create(
                **validated_data.pop("facility"))
            preset = validated_data.pop("preset")
            facility.dataset.preset = preset
            facility.dataset.reset_to_default_settings(preset)
            # overwrite the settings in dataset_data with validated_data.settings
            custom_settings = validated_data.pop("settings")
            for key, value in custom_settings.items():
                if value is not None:
                    setattr(facility.dataset, key, value)
            facility.dataset.save()

            # Create superuser
            superuser = create_superuser(validated_data["superuser"],
                                         facility=facility)

            # Create device settings
            language_id = validated_data.pop("language_id")
            allow_guest_access = validated_data.pop("allow_guest_access")
            if allow_guest_access is None:
                allow_guest_access = preset != "formal"
            provision_device(
                device_name=validated_data["device_name"],
                language_id=language_id,
                default_facility=facility,
                allow_guest_access=allow_guest_access,
            )
            return {
                "facility": facility,
                "preset": preset,
                "superuser": superuser,
                "language_id": language_id,
                "settings": custom_settings,
                "allow_guest_access": allow_guest_access,
            }
Esempio n. 8
0
def create_device_settings(language_id=None,
                           facility=None,
                           interactive=False,
                           new_settings={}):
    if language_id is None and interactive:
        language_id = get_user_response(
            "Enter a default language code [{langs}]: ".format(
                langs=",".join(languages.keys())),
            valid_answers=languages,
        )
    # Override any settings passed in
    for key in new_settings:
        check_device_setting(key)

    settings_to_set = dict(new_settings)
    settings_to_set["language_id"] = language_id
    settings_to_set["default_facility"] = facility

    provision_device(**settings_to_set)
    logger.info("Device settings updated with {}".format(settings_to_set))
Esempio n. 9
0
    def test_extract_facility_statistics(self):
        provision_device(allow_guest_access=True)
        facility = self.facilities[0]
        actual = extract_facility_statistics(facility)
        facility_id_hash = actual.pop("fi")
        birth_year_list_learners = [
            int(year) for year in FacilityUser.objects.filter(
                roles__isnull=True).values_list("birth_year", flat=True)
        ]
        # just assert the beginning hex values of the facility id don't match
        self.assertFalse(facility_id_hash.startswith(facility.id[:3]))
        demo_stats = calculate_list_stats(birth_year_list_learners)
        expected = {
            "s": {
                "preset": facility_presets.default,
                "learner_can_edit_username": True,
                "learner_can_edit_name": True,
                "learner_can_edit_password": True,
                "learner_can_sign_up": True,
                "learner_can_delete_account": True,
                "learner_can_login_with_no_password": False,
                "show_download_button_in_learn": True,
                "allow_guest_access": True,
                "registered": False,
            },
            "lc": 20,  # learners_count
            "llc": 20,  # learner_login_count
            "cc": 1,  # coaches_count
            "clc": 1,  # coach_login_count
            "f": "2018-10-11",  # first interaction
            "l": "2019-10-11",  # last interaction
            "ss": 20,  # summarylog_started
            "sc": 20,  # summarylog_complete
            "sk": {
                content_kinds.EXERCISE: 20,
                content_kinds.VIDEO: 20
            },  # sess_kinds
            "lec": 1,  # lesson_count
            "ec": 1,  # exam_count
            "elc": 20,  # exam_log_count
            "alc": 20,  # att_log_count
            "ealc": 20,  # exam_att_log_count
            "suc": 20,  # sess_user_count
            "sac": 20,  # sess_anon_count
            "sut": 20,  # sess_user_time
            "sat": 20,  # sess_anon_time
            "dsl": {
                "bys": {
                    "a": demo_stats["mean"],
                    "sd": demo_stats["std"],
                    "ts": 20,
                    "d": 0,
                    "ns": 0,
                },
                "gc": {
                    gender: FacilityUser.objects.filter(gender=gender).count()
                    for (gender, _) in demographics.choices
                    if FacilityUser.objects.filter(gender=gender).exists()
                },
            },  # demographic_stats_learner
            "dsnl": {},  # demographic_stats_non_learner
            "crc": 1,  # class_count
            "grc": 0,  # group_count
            "sacnv": 20,  # sess_anon_count_no_visitor_id
            "uwl": 20,  # users_with_logs
            "vwl": 0,  # anon_visitors_with_logs
            "dis": {
                "Android,9/Chrome Mobile,86": 20
            },  # device info
        }

        assert actual == expected
Esempio n. 10
0
def setup_device():
    # Test helper to return a facility and superuser
    facility = Facility.objects.create(name="Test")
    superuser = create_superuser(facility)
    provision_device()
    return facility, superuser