def handle(self, *args, **options): with open(filename, encoding='utf-8') as file: minerals = json.load(file) for mineral in minerals: Mineral(name=mineral['name'], img_filename=mineral['image filename'], img_caption=mineral['image caption'], category=mineral['category'], formula=mineral['formula'], group=mineral['group'], strunz_classification=mineral['strunz classification'], crystal_system=mineral['crystal system'], unit_cell=mineral['unit cell'], color=mineral['color'], crystal_symmetry=mineral['crystal symmetry'], cleavage=mineral['cleavage'], mohs_hardness=mineral['mohs scale hardness'], luster=mineral['luster'], streak=mineral['streak'], diaphaneity=mineral['diaphaneity'], optical_properties=mineral['optical properties'], refractive_index=mineral['refractive index'], crystal_habit=mineral['crystal habit'], specific_gravity=mineral['specific gravity']).save() self.stdout.write( self.style.SUCCESS( '\nMineral data has been successfully uploaded!'))
def handle(self, *args, **options): with open('minerals.json', encoding='utf-8') as file: minerals = json.load(file) for mineral in minerals: Mineral( name=mineral['name'], image_filename=mineral["name"] + '.jpg', image_caption=mineral['image caption'], category=mineral['category'], formula=mineral.get('formula'), strunz_classification=mineral.get('strunz classification'), crystal_system=mineral.get('crystal system'), unit_cell=mineral.get('unit cell'), color=mineral.get('color'), crystal_symmetry=mineral.get('crystal symmetry'), cleavage=mineral.get('cleavage'), mohs_scale_hardness=mineral.get('mohs scale hardness'), luster=mineral.get('luster'), streak=mineral.get('streak'), diaphaneity=mineral.get('diaphaneity'), optical_properties=mineral.get('optical properties'), refractive_index=mineral.get('refractive index'), crystal_habit=mineral.get('crystal habit'), specific_gravity=mineral.get('specific gravity'), group=mineral['group']).save() self.stdout.write(self.style.SUCCESS('Data Added"'))
def handle(self, *args, **kwargs): with open('data/minerals.json') as file: data = json.load(file) for mineral in data: data_with_underscore = rename_keys(mineral) Mineral(**data_with_underscore).save() # name=mineral['name'], # image_filename=mineral['image filename'], # image_caption=mineral['image caption'], # category=mineral['category'], # group=mineral['group'], # formula=mineral.get('formula', ''), # crystal_system=mineral.get('crystal system', ''), # strunz_classification=mineral.get('strunz classification', ''), # unit_cell=mineral.get('unit cell', ''), # crystal_symmetry=mineral.get('crystal symmetry', ''), # cleavage=mineral.get('cleavage', ''), # mohs_scale_hardness=mineral.get('mohs scale hardness', ''), # luster=mineral.get('luster', ''), # streak=mineral.get('streak', ''), # diaphaneity=mineral.get('diaphaneity', ''), # optical_properties=mineral.get('optical properties', ''), # refractive_index=mineral.get('refractive index', '') # ).save() self.stdout.write( self.style.SUCCESS('Successfully saved minerals "%s"' % str(len(data))))
def load_data(mineral_data): ''' loop through the daat and add to database ''' for mineral in mineral_data: try: Mineral( name=mineral.get('name', ''), img_filename=mineral.get('name', '') + '.jpg', img_caption=mineral.get('image caption', ''), category=mineral.get('category', ''), formula=mineral.get('formula', ''), strunz_classification=mineral.get('strunz classification', ''), crystal_system=mineral.get('crystal system', ''), unit_cell=mineral.get('unit cell', ''), color=mineral.get('color', ''), crystal_symmetry=mineral.get('crystal symmetry', ''), cleavage=mineral.get('cleavage', 'Empty'), mohs_hardness=mineral.get('mohs scale hardness', ''), luster=mineral.get('luster', ''), streak=mineral.get('streak', ''), diaphaneity=mineral.get('diaphaneity', ''), optical_properties=mineral.get('optical properties', ''), refractive_index=mineral.get('refractive index', ''), crystal_habit=mineral.get('crystal habit', ''), specific_gravity=mineral.get('specific gravity', ''), group=mineral.get('group', ''), ).save() except ValueError: print('There was a databae error... Exiting!') sys.exit() print('Mineral data has been successfully added!')
def load_json_to_db(): # Then we can finally import the model from the app from minerals.models import Mineral # Load minerals json as dictionary minerals = json.load(open('./minerals/fixtures/minerals.json')) for mineral in minerals: # Adds mineral to database, adding an empty string if the key isn't # in the particular mineral dictionary Mineral( name=mineral.get('name', ''), image_filename=mineral.get('image filename', ''), image_caption=mineral.get('image caption', ''), group=mineral.get('group', ''), category=mineral.get('category', ''), formula=mineral.get('formula', ''), strunz_classification=mineral.get('strunz classification', ''), color=mineral.get('color', ''), crystal_system=mineral.get('crystal system', ''), unit_cell=mineral.get('unit cell', ''), crystal_symmetry=mineral.get('crystal symmetry', ''), cleavage=mineral.get('cleavage', ''), mohs_scale_hardness=mineral.get('mohs scale hardness', ''), luster=mineral.get('luster', ''), streak=mineral.get('streak', ''), diaphaneity=mineral.get('diaphaneity', ''), optical_properties=mineral.get('optical properties', ''), refractive_index=mineral.get('refractive index', ''), crystal_habit=mineral.get('crystal habit', ''), specific_gravity=mineral.get('specific gravity', ''), ).save()
def add_json_to_db(request): csv_file = "minerals.json" with open(csv_file, encoding="utf-8") as csv_file: minerals = json.load(csv_file) for mineral in minerals: # create a blank dict full_mineral_dict = { "name": None, "image_filename": None, "image_caption": None, "category": None, "formula": None, "strunz_classification": None, "crystal_system": None, "unit_cell": None, "color": None, "crystal_symmetry": None, "cleavage": None, "mohs_scale_hardness": None, "luster": None, "streak": None, "diaphaneity": None, "optical_properties": None, "refractive_index": None, "crystal_habit": None, "specific_gravity": None} # populate blank dict when information available for key, value in mineral.items(): full_mineral_dict[key] = value # check if mineral already outstanding, create if not try: Mineral.objects.get(name=full_mineral_dict["name"]) continue except Mineral.DoesNotExist: Mineral( name=full_mineral_dict["name"], image_filename=full_mineral_dict["image_filename"], image_caption=full_mineral_dict["image_caption"], category=full_mineral_dict["category"], formula=full_mineral_dict["formula"], strunz_classification=full_mineral_dict["strunz_classification"], crystal_system=full_mineral_dict["crystal_system"], unit_cell=full_mineral_dict["unit_cell"], color=full_mineral_dict["color"], crystal_symmetry=full_mineral_dict["crystal_symmetry"], cleavage=full_mineral_dict["cleavage"], mohs_scale_hardness=full_mineral_dict["mohs_scale_hardness"], luster=full_mineral_dict["luster"], streak=full_mineral_dict["streak"], diaphaneity=full_mineral_dict["diaphaneity"], optical_properties=full_mineral_dict["optical_properties"], refractive_index=full_mineral_dict["refractive_index"], crystal_habit=full_mineral_dict["crystal_habit"], specific_gravity=full_mineral_dict["specific_gravity"] ).save() return HttpResponse("It is done.")
def mineral_fields(mineral): '''Returns fields of the mineral in order of occurence''' weigthed_attributes_list = Mineral.attributes_weighted() weigthed_field_list = [] for fieldname in weigthed_attributes_list: data = getattr(mineral, fieldname) label = mineral._meta.get_field(fieldname).verbose_name if data != '': weigthed_field_list.append({'label': label, 'data': data}) return {'mineral_field_list': weigthed_field_list}
def load_data(): try: with open('minerals.json', encoding="utf-8") as file: data = json.load(file) minerals = parse_colum_names(data) for mineral in minerals: Mineral(**mineral).save() print("\n> Fixtures loaded successfully!\n") except FileNotFoundError: print('Error: Fixtures file does not exits!')
def populate_db_with_minerals(): # We have to called load_context before importing the model !!!! from minerals.models import Mineral mineral_json_data = open('minerals.json') minerals = json.load(mineral_json_data) for mineral in minerals: # import pdb;pdb.set_trace() min_dict = dict_formatted(mineral) # import pdb;pdb.set_trace() Mineral(**min_dict).save()
def populate_db(): """Populates db database. This should be done outside of project files and only called once at first launch""" # Setting up django default environment so Django can find project files os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mineral_catalog.settings") get_wsgi_application() # Setting up json file local directory path with absolute path local_directory = os.path.dirname(os.path.abspath(__file__)) json_file = os.path.join(local_directory, 'assets/json/minerals.json') from minerals.models import Mineral with open(json_file, encoding='utf8') as js: fields = json.load(js) for field in fields: Mineral( name=field.get('name', ''), image_filename=field.get('image filename', ''), image_caption=field.get('image caption', ''), group=field.get('group', ''), category=field.get('category', ''), formula=field.get('formula', ''), strunz_classification=field.get('strunz classification', ''), color=field.get('color', ''), crystal_system=field.get('crystal system', ''), unit_cell=field.get('unit cell', ''), crystal_symmetry=field.get('crystal symmetry', ''), cleavage=field.get('cleavage', ''), mohs_scale_hardness=field.get('mohs scale hardness', ''), luster=field.get('luster', ''), streak=field.get('streak', ''), diaphaneity=field.get('diaphaneity', ''), optical_properties=field.get('optical properties', ''), refractive_index=field.get('refractive index', ''), crystal_habit=field.get('crystal habit', ''), specific_gravity=field.get('specific gravity', ''), ).save()
def mineral_all_search(request): ''' This function is the view for: the extra credit requirement that applys the search query to every field, not just the name (see DEPRECATED, prev function) ''' # developed from this helpful stack overflow post: # https://stackoverflow.com/questions/1866847/searching-all-fields-in-a-table-in-django if request.POST: # save the primary key query = request.POST['search'] else: # if this view is called without a query, show all minerals return redirect('all') or_query = None for field_name in Mineral.iter_attr(): # for every searchable field, add to the query # uses django's Q object to combine queries into one # individual queries are q, the combined query is or_query q = Q(**{"%s__icontains" % field_name: query}) if or_query is None: or_query = q else: or_query = or_query | q # get the query from the combined Q query object minerals = Mineral.objects.filter(or_query).annotate() # render the list template with the queried minerals return render( request, 'list.html', { 'minerals': minerals, 'count': len(minerals), 'query': query, }, )
""" WSGI config for mineralcatalog project. It exposes the WSGI callable as a module-level variable named ``application``. For more information on this file, see https://docs.djangoproject.com/en/2.1/howto/deployment/wsgi/ """ import os from django.core.wsgi import get_wsgi_application from django.db import utils from minerals.models import Mineral try: Mineral.ingest_data_from_json_file() except (utils.DatabaseError, utils.DataError) as e: print(e) os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mineralcatalog.settings') application = get_wsgi_application()
} for key, value in mineral.items(): full_mineral_dict[key] = value return full_mineral_dict with open('fixtures.json', 'w') as fixture: minerals = json.load(open('minerals.json')) for mineral in minerals: data = get_mineral_data(mineral=mineral) Mineral(name=data['name'], image_filename=data['image filename'], image_caption=data['image caption'], category=data['category'], formula=data['formula'], strunz_classification=data['strunz classification'], crystal_system=data['crystal system'], unit_cell=data['unit cell'], color=data['color'], crystal_symmetry=data['crystal symmetry'], cleavage=data['cleavage'], mohs_scale_hardness=data['mohs scale hardness'], luster=data['luster'], streak=data['streak'], diaphaneity=data['diaphaneity'], optical_properties=data['optical properties'], group=data['group'], refractive_index=data['refractive index'], crystal_habit=data['crystal habit'], specific_gravity=data['specific gravity']).save()
def populate_database(): '''This populates the database for each mineral in the json file.''' # I can use my app models now... mineral_json = minerals_dictionary() for item in mineral_json: alpha = Mineral() alpha.name = key_checker(item, 'name') alpha.image_filename = key_checker(item, 'image filename') alpha.image_caption = key_checker(item, 'image caption') alpha.category = key_checker(item, 'category') alpha.formula = key_checker(item, 'formula') alpha.strunz_classification = key_checker(item, 'strunz classification') alpha.color = key_checker(item, 'color') alpha.crystal_system = key_checker(item, 'crystal system') alpha.cleavage = key_checker(item, 'cleavage') alpha.mohs_scale_hardness = key_checker(item, 'mohs scale hardness') alpha.luster = key_checker(item, 'luster') alpha.streak = key_checker(item, 'streak') alpha.diaphaneity = key_checker(item, 'diaphaneity') alpha.optical_properties = key_checker(item, 'optical properties') alpha.refractive_index = key_checker(item, 'refractive index') alpha.crystal_habit = key_checker(item, 'crystal habit') alpha.specific_gravity = key_checker(item, 'specific gravity') alpha.group = key_checker(item, 'group') alpha.save()
def handle(self, *args, **options): with open('minerals.json', encoding='utf-8') as file: minerals = json.load(file) for mineral in minerals: category = "" formula = "" strunz_classification = "" unit_cell = "" color = "" crystal_habit = "" mohs_scale_hardness = "" crystal_system = "" crystal_symmetry = "" cleavage = "" luster = "" streak = "" diaphaneity = "" optical_properties = "" refractive_index = "" specific_gravity = "" group = "" if mineral['category']: category = mineral['category'] if 'formula' in mineral: formula = mineral['formula'] if 'strunz_classification' in mineral: strunz_classification = mineral['strunz_classification'] if 'unit_cell' in mineral: unit_cell = mineral['unit_cell'] if 'color' in mineral: color = mineral['color'] if 'crystal_habit' in mineral: crystal_habit = mineral['crystal_habit'] if 'mohs_scale_hardness' in mineral: mohs_scale_hardness = mineral['mohs_scale_hardness'] if 'crystal_system' in mineral: crystal_system = mineral['crystal_system'] if 'crystal_symmetry' in mineral: crystal_symmetry = mineral['crystal_symmetry'] if 'cleavage' in mineral: cleavage = mineral['cleavage'] if 'luster' in mineral: luster = mineral['luster'] if 'streak' in mineral: luster = mineral['streak'] if 'diaphaneity' in mineral: diaphaneity = mineral['diaphaneity'] if 'optical_properties' in mineral: optical_properties = mineral['optical_properties'] if 'refractive_index' in mineral: refractive_index = mineral['refractive_index'] if 'specific_gravity' in mineral: specific_gravity = mineral['specific_gravity'] if 'group' in mineral: group = mineral['group'] Mineral(name=mineral['name'], image_filename=mineral['image_filename'], image_caption=mineral['image_caption'], category=category, formula=formula, strunz_classification=strunz_classification, unit_cell=unit_cell, color=color, crystal_habit=crystal_habit, mohs_scale_hardness=mohs_scale_hardness, crystal_system=crystal_system, crystal_symmetry=crystal_symmetry, cleavage=cleavage, luster=luster, streak=streak, diaphaneity=diaphaneity, optical_properties=optical_properties, refractive_index=refractive_index, specific_gravity=specific_gravity, group=group).save() self.stdout.write( self.style.SUCCESS('Mineral data has been successfully added!'))
def mineral_loader(apps, schema_editor): '''This function loads all the minerals data from a file when project is first set up''' THIS_FOLDER = os.path.dirname(os.path.abspath(__file__)) my_file = os.path.join(THIS_FOLDER, 'minerals.json') minerals = json.loads(open(my_file).read()) for mineral in minerals: #Looping through each potential attribute, and if it exists, then set it to local variable, # if not, set that attribute as a blank string. THis was just a first pass attempt, there # is probably a simpler way to do this as a future improvement! try: name = mineral['name'] except: name = "" try: image_filename = mineral['name'] except: image_filename = "" try: image_caption = mineral['image_caption'] except: image_caption = "" try: category = mineral['category'] except: category = "" try: formula = mineral['formula'] except: formula = "" try: strunz_classification = mineral['strunz_classification'] except: strunz_classification = "" try: crystal_system = mineral['crystal_system'] except: crystal_system = "" try: unit_cell = mineral['unit_cell'] except: unit_cell = "" try: color = mineral['color'] except: color = "" try: crystal_symmetry = mineral['crystal_symmetry'] except: crystal_symmetry = "" try: cleavage = mineral['cleavage'] except: cleavage = "" try: mohs_scale_hardness = mineral['mohs_scale_hardness'] except: mohs_scale_hardness = "" try: luster = mineral['luster'] except: luster = "" try: streak = mineral['streak'] except: streak = "" try: diaphaneity = mineral['diaphaneity'] except: diaphaneity = "" try: optical_properties = mineral['optical_properties'] except: optical_properties = "" try: group = mineral['group'] except: group = "" try: refractive_index = mineral['refractive_index'] except: refractive_index = "" try: crystal_habit = mineral['crystal_habit'] except: crystal_habit = "" try: specific_gravity = mineral['specific_gravity'] except: specific_gravity = "" minEntry = Mineral(name=name, image_filename=image_filename, image_caption=image_caption, category=category, formula=formula, strunz_classification=strunz_classification, crystal_system=crystal_system, unit_cell=unit_cell, color=color, crystal_symmetry=crystal_symmetry, cleavage=cleavage, mohs_scale_hardness=mohs_scale_hardness, luster=luster, streak=streak, diaphaneity=diaphaneity, optical_properties=optical_properties, group=group, refractive_index=refractive_index, specific_gravity=specific_gravity, crystal_habit=crystal_habit).save()