def _populate_database(): get_user_model().objects.create_user(username='******', password='******') get_user_model().objects.create_superuser(username='******', password='******') image_name = 'test-image.jpg' image_bytes = None with open(CURRENT_DIR + '/' + image_name, 'rb') as image: image_bytes = image.read() resource = Resource( external_id='recpbs29kfas9i', url='https://test.test/resource.pdf' ) resource.image.save(image_name, ContentFile(image_bytes), save=True) resource.save() for external_id in ('recZxlcM61qaDoOkc', 'recYK5ljTyL3b18J3', 'recvSDrARAcmKogbD'): practice = Practice( external_id=external_id, main_resource=resource, ) practice.image.save(image_name, ContentFile(image_bytes), save=True) practice.save() for category_id in ('rec82929kfas9i', 'rec0098afaooka', 'recppasf09aii'): category = Category( external_id=category_id, practice_external_ids=['recZxlcM61qaDoOkc'] ) category.image.save(image_name, ContentFile(image_bytes), save=True) category.save() category.practices.add(Practice.objects.filter(external_id='recZxlcM61qaDoOkc').first())
def test_update_reservation(sample_shared_resource: Resource): old_timestamp = datetime(year=200, month=1, day=1, tzinfo=utc) sample_shared_resource.last_check_in = old_timestamp sample_shared_resource.save() allocator.update_reservation(sample_shared_resource) sample_shared_resource.refresh_from_db(fields=['last_check_in']) assert sample_shared_resource.last_check_in != old_timestamp
def test_make_reservation(admin_user, sample_unshared_resource: Resource, monkeypatch): assert sample_unshared_resource.user != admin_user mock_for_all_devices = MagicMock() monkeypatch.setattr(allocator, 'for_all_devices', mock_for_all_devices) allocator.make_reservation(sample_unshared_resource, admin_user, used_for='TEST') assert mock_for_all_devices.call_count == 1 assert 'share' in mock_for_all_devices.call_args[0] sample_unshared_resource.refresh_from_db(fields=['user']) assert sample_unshared_resource.user == admin_user
def _populate_database(): User.objects.create_user(username='******', password='******') User.objects.create_superuser(username='******', password='******') image_name = 'test-image.jpg' image_bytes = None with open(CURRENT_DIR + '/' + image_name, 'rb') as image: image_bytes = image.read() resource = Resource(external_id='recpbs29kfas9i', url='https://test.test/resource.pdf') resource.image.save(image_name, ContentFile(image_bytes), save=True) resource.save() for external_id in ('recZxlcM61qaDoOkc', 'recYK5ljTyL3b18J3', 'recvSDrARAcmKogbD'): practice = Practice( external_id=external_id, main_resource=resource, ) practice.image.save(image_name, ContentFile(image_bytes), save=True) practice.save()
def make_reservation(resource: Resource, user: settings.AUTH_USER_MODEL, used_for: str): # If we are multi threaded there could be a race condition here between when grab and when used logger.info( f"Reservation being made user={user.username} used_for={used_for} resource={resource}" ) with transaction.atomic(): resource.user = user resource.used_for = used_for resource.use_password = token_urlsafe(nbytes=10) resource.last_check_in = now() resource.last_reserved = now() resource.save() for_all_devices(resource.device_set.all(), 'share')
def refresh_reservation(resource: Resource): logger.info(f"Reservation device shares being refresh resource={resource}") resource.last_check_in = now() for_all_devices(resource.device_set.all(), 'refresh') resource.save()
def update_reservation(resource: Resource): logger.info( f"Reservation being being updated user={resource.user.username} resource={resource}" ) resource.last_check_in = now() resource.save()
def update(): """ We completely replace whatever we have in the DB for the new information. Eventually we may want to only replace the changed ones. If there are errors, an array of them will be returned. """ errors = [] practices_base = settings.AIRTABLE_PRACTICES_BASE json_practices = _get_airtable_data('Pratiques?view=Grid%20view', practices_base) errors += validate_practices(json_practices) json_practice_types = _get_airtable_data('Types%20de%20pratique?view=Grid%20view', practices_base) errors += validate_practice_types(json_practice_types) json_weeds = _get_airtable_data('Adventices?view=Grid%20view', practices_base) errors += validate_weeds(json_weeds) json_pests = _get_airtable_data('Ravageurs?view=Grid%20view', practices_base) errors += validate_pests(json_pests) json_cultures = _get_airtable_data('Cultures?view=Grid%20view', practices_base) errors += validate_cultures(json_cultures) json_glyphosate = _get_airtable_data('Glyphosate?view=Grid%20view', practices_base) errors += validate_glyphosate_uses(json_glyphosate) json_resources = _get_airtable_data('Liens?view=Grid%20view', practices_base) errors += validate_resources(json_resources) json_resource_images = _get_airtable_data('logos?view=Grid%20view', practices_base) errors += validate_resource_images(json_resource_images) json_categories = _get_airtable_data('Categories?view=Grid%20view', practices_base) errors += validate_categories(json_categories) json_weed_practices = _get_airtable_data('Pratiques%2FAdventices?view=Grid%20view', practices_base) errors += validate_weed_practices(json_weed_practices) json_pest_practices = _get_airtable_data('Pratiques%2FRavageurs?view=Grid%20view', practices_base) errors += validate_pest_practices(json_pest_practices) json_culture_practices = _get_airtable_data('Pratiques%2FCultures?view=Grid%20view', practices_base) errors += validate_culture_practices(json_culture_practices) json_departments_practices = _get_airtable_data('Pratiques%2FDepartements?view=Grid%20view', practices_base) errors += validate_department_practices(json_departments_practices) json_departments = _get_airtable_data('Departements?view=Grid%20view', practices_base) errors += validate_departments(json_departments) json_glyphosate_practices = _get_airtable_data('Pratiques%2FGlyphosate?view=Grid%20view', practices_base) errors += validate_glyphosate_practices(json_glyphosate_practices) json_practice_groups = _get_airtable_data('Familles?view=Grid%20view', practices_base) errors += validate_practice_groups(json_practice_groups) json_mechanisms = _get_airtable_data('Marges%20de%20manoeuvre?view=Grid%20view', practices_base) errors += validate_mechanisms(json_mechanisms) has_fatal_errors = any(x.fatal for x in errors) if has_fatal_errors: return errors mechanisms = [Mechanism.create_from_airtable(x) for x in json_mechanisms] Mechanism.objects.all().delete() for mechanism in mechanisms: mechanism.save() categories = [Category.create_from_airtable(x) for x in json_categories] Category.objects.all().delete() for category in categories: category.save() json_resource_images.sort(key=lambda x: len(x['fields'].get('URL_principal')), reverse=True) resources = [Resource.create_from_airtable(x, json_resource_images) for x in json_resources] Resource.objects.all().delete() for resource in resources: resource.save() practice_groups = [PracticeGroup.create_from_airtable(x) for x in json_practice_groups] PracticeGroup.objects.all().delete() for practice_group in practice_groups: practice_group.save() practice_types = [PracticeType.create_from_airtable(x) for x in json_practice_types] PracticeType.objects.all().delete() for practice_type in practice_types: practice_type.save() weeds = [Weed.create_from_airtable(x) for x in json_weeds] Weed.objects.all().delete() for weed in weeds: weed.save() pests = [Pest.create_from_airtable(x) for x in json_pests] Pest.objects.all().delete() for pest in pests: pest.save() accepted_sectors = ['Grande culture'] cultures = [SimulatorCulture.create_from_airtable(x) for x in json_cultures if x['fields'].get('Filière') in accepted_sectors] SimulatorCulture.objects.all().delete() for culture in cultures: culture.save() practices = [Practice.create_from_airtable(x, json_culture_practices, json_departments_practices, json_departments, json_glyphosate, json_glyphosate_practices, mechanisms, resources, json_practice_types, json_weeds, json_weed_practices, json_pests, json_pest_practices) for x in json_practices] Practice.objects.all().delete() for practice in practices: practice.save() _link_practices_with_groups(practices, practice_groups) _link_practices_with_resources(practices, resources) _link_practices_with_types(practices, practice_types) _link_practices_with_categories(practices, categories) return errors