Beispiel #1
0
 def infer_dataset(self, *args, **kwargs):
     if self.user:
         return self.user.dataset
     else:
         facility = Facility.get_default_facility()
         assert facility, "Before you can save logs, you must have a facility"
         return facility.dataset
Beispiel #2
0
 def infer_dataset(self, *args, **kwargs):
     if self.user:
         return self.user.dataset
     else:
         facility = Facility.get_default_facility()
         assert facility, "Before you can save logs, you must have a facility"
         return facility.dataset
Beispiel #3
0
def create_facility(facility_name=None, preset=None, interactive=False):
    if facility_name is None and interactive:
        answer = get_user_response("Do you wish to create a facility? [yn] ", ["y", "n"])
        if answer == "y":
            facility_name = get_user_response("What do you wish to name your facility? ")

    if facility_name:
        facility, created = Facility.objects.get_or_create(name=facility_name)

        if not created:
            logging.warn("Facility with name {name} already exists, not modifying preset.".format(name=facility_name))
            return facility

        logging.info("Facility with name {name} created.".format(name=facility_name))

        if preset is None and interactive:
            preset = get_user_response(
                "Which preset do you wish to use? [{presets}]: ".format(presets=",".join(presets.keys())),
                valid_answers=presets)

        # Only set preset data if we have created the facility, otherwise leave previous data intact
        if preset:
            dataset_data = mappings[preset]
            for key, value in dataset_data.items():
                setattr(facility.dataset, key, value)
            facility.dataset.save()
            logging.info("Facility preset changed to {preset}.".format(preset=preset))
    else:
        facility = Facility.get_default_facility() or Facility.objects.first()
        if not facility:
            raise CommandError('No facility exists')
    return facility
Beispiel #4
0
    def handle(self, *args, **options):
        if options['facility']:
            default_facility = Facility.objects.get(pk=options['facility'])
        else:
            default_facility = Facility.get_default_facility()

        if not default_facility:
            raise CommandError('No default facility exists, please make sure to provision this device before running this command')

        fieldnames = ['full_name', 'username', 'password', 'facility', 'class']
        # open using default OS encoding
        with open(options['filepath']) as f:
            header = next(csv.reader(f, strict=True))
            if all(col in fieldnames for col in header):
                # Every item in the first row matches an item in the fieldnames, it is a header row
                if 'username' not in header:
                    raise CommandError('No usernames specified, this is required for user creation')
                ordered_fieldnames = header
            elif any(col in fieldnames for col in header):
                raise CommandError('Mix of valid and invalid header labels found in first row')
            else:
                ordered_fieldnames = fieldnames

        # open using default OS encoding
        with open(options['filepath']) as f:
            reader = csv.DictReader(f, fieldnames=ordered_fieldnames, strict=True)
            with transaction.atomic():
                create_func = partial(create_user, default_facility=default_facility)
                total = sum(starmap(create_func, enumerate(reader)))
                logging.info('{total} users created'.format(total=total))
Beispiel #5
0
    def get_context_data(self, **kwargs):
        context = super(UserView, self).get_context_data(**kwargs)

        # Put the default facility id into the page so that we can bootstrap the
        # dataset for this facility into the page and allow for quicker rendering
        # of the login page, without having to wait for an AJAX call.

        context['currentFacilityId'] = getattr(Facility.get_default_facility(), 'id', None)

        return context
Beispiel #6
0
 def test_create_device_settings_default_facility(self):
     default_facility = Facility.get_default_facility()
     self.assertEqual(DeviceSettings.objects.get().default_facility,
                      default_facility)
Beispiel #7
0
 def test_create_facility_set_preset(self):
     default_facility = Facility.get_default_facility()
     dataset_data = mappings[self.preset]
     for key, value in dataset_data.items():
         self.assertEqual(getattr(default_facility.dataset, key), value)
Beispiel #8
0
 def test_no_facility_return_default(self):
     setup_device()
     default_facility = Facility.get_default_facility()
     facility = create_facility(facility_name=default_facility.name)
     self.assertEqual(default_facility, facility)