From 590a67fbfc18e947655f9a84f2574da1d7045da6 Mon Sep 17 00:00:00 2001 From: Nikhil Kumar Date: Mon, 31 May 2021 22:50:32 +0530 Subject: [PATCH] [ADD] Load default libraries while ocntainer build [FIX] Load from gallery --- eda-frontend/src/utils/GallerySchSample.js | 12 +- .../commands/createsuperuser_noinput.py | 31 ++++ .../management/commands/load_default_libs.py | 140 ++++++++++++++++++ .../libAPI/management/commands/seed_libs.py | 106 ------------- esim-cloud-backend/migrations.sh | 5 +- 5 files changed, 179 insertions(+), 115 deletions(-) create mode 100644 esim-cloud-backend/libAPI/management/commands/createsuperuser_noinput.py create mode 100644 esim-cloud-backend/libAPI/management/commands/load_default_libs.py delete mode 100644 esim-cloud-backend/libAPI/management/commands/seed_libs.py diff --git a/eda-frontend/src/utils/GallerySchSample.js b/eda-frontend/src/utils/GallerySchSample.js index 768ddf473..06ffd49a5 100644 --- a/eda-frontend/src/utils/GallerySchSample.js +++ b/eda-frontend/src/utils/GallerySchSample.js @@ -3,7 +3,7 @@ const GallerySchSample = [ { save_id: 'gallery0', - data_dump: '', + data_dump: '', name: 'Voltage Divider', description: 'A voltage divider is a simple circuit which turns a large voltage into a smaller one. Using just two series resistors and an input voltage.', media: 'gallery0.png', @@ -11,7 +11,7 @@ const GallerySchSample = [ }, { save_id: 'gallery1', - data_dump: '', + data_dump: '', name: 'RC Circuit', description: 'An RC circuit is a circuit with both a resistor (R) and a capacitor (C). RC circuits are freqent element in electronic devices.', media: 'gallery1.png', @@ -19,7 +19,7 @@ const GallerySchSample = [ }, { save_id: 'gallery2', - data_dump: '', + data_dump: '', name: 'Dual RC Ladder', description: 'This is an dual RC ladder circuit with Passive components. The input is a voltage waveform (a pulse) versus time, and the output is a waveform as well. ', media: 'gallery2.png', @@ -27,7 +27,7 @@ const GallerySchSample = [ }, { save_id: 'gallery3', - data_dump: '', + data_dump: '', name: 'Bipolar Amplifier', description: 'A basic BJT amplifier has a very high gain that may vary widely from one transistor to the next. A NPN bipolar transistor is the used as amplifying device.', media: 'gallery3.png', @@ -35,7 +35,7 @@ const GallerySchSample = [ }, { save_id: 'gallery4', - data_dump: '', + data_dump: '', name: 'Shunt Clipper', description: 'A Clipper circuit in which the diode is connected in shunt to the input signal and that attenuates the positive portions of the waveform, is termed as Positive Shunt Clipper.', media: 'gallery4.png', @@ -43,7 +43,7 @@ const GallerySchSample = [ }, { save_id: 'gallery5', - data_dump: '', + data_dump: '', name: 'RC Circuit ( Parallel )', description: 'An RC circuit is a circuit with both a resistor (R) and a capacitor (C). RC circuits are freqent element in electronic devices.', media: 'gallery5.png', diff --git a/esim-cloud-backend/libAPI/management/commands/createsuperuser_noinput.py b/esim-cloud-backend/libAPI/management/commands/createsuperuser_noinput.py new file mode 100644 index 000000000..61bd154ed --- /dev/null +++ b/esim-cloud-backend/libAPI/management/commands/createsuperuser_noinput.py @@ -0,0 +1,31 @@ +from django.contrib.auth import get_user_model +from django.db.models import Q +from django.core.management.base import BaseCommand + + +class Command(BaseCommand): + help = "Create default admin user if not already present." + + def add_arguments(self, parser): + parser.add_argument( + '--username', type=str, + help="username of admin account" + ) + parser.add_argument( + '--password', type=str, + help="password of the admin account" + ) + + def handle(self, *args, **options): + if options['username'] and options['password']: + User = get_user_model() + user = User.objects.filter(username=options['username']) + if user.count() > 0: + raise Exception(f"User with same username exists") + user = User.objects.create_superuser( + username=options['username'], + email='', password=options['password'] + ) + user.save() + else: + raise Exception("Username or Password not present") diff --git a/esim-cloud-backend/libAPI/management/commands/load_default_libs.py b/esim-cloud-backend/libAPI/management/commands/load_default_libs.py new file mode 100644 index 000000000..3ee0781cb --- /dev/null +++ b/esim-cloud-backend/libAPI/management/commands/load_default_libs.py @@ -0,0 +1,140 @@ +from libAPI.helper.main import generate_svg_and_save_to_folder +import os +import glob + +from django.core.management import call_command +from django.contrib.admin import options +from django.contrib.auth import get_user_model +from django.core.files.uploadedfile import InMemoryUploadedFile +from django.core.management.base import BaseCommand + +from libAPI.models import Library, LibraryComponent, \ + ComponentAlternate, LibrarySet + +import logging +logger = logging.getLogger(__name__) + + +class Command(BaseCommand): + help = "Load default libraries if not already present." + + def add_arguments(self, parser): + parser.add_argument( + '--username', + help='input a user\'s username', type=str + ) + parser.add_argument( + '--location', type=self.dir_path, + help="Directory containing kicad library files" + ) + + def dir_path(self, path): + if os.path.isdir(path): + return path + else: + raise Exception(f"{path} is not a valid path") + + def handle(self, *args, **options): + User = get_user_model() + if options['username']: + user = User.objects.get(username=options['username']) + else: + raise Exception("Enter a superuser to associate libs") + library_set = LibrarySet.objects.filter( + user=user, default=True + ).first() + if not library_set: + library_set = LibrarySet( + user=user, + default=True, + name="esim-default" + ) + library_set.save() + seed_libraries(self, options['location'], library_set) + + +def seed_libraries(self, location, library_set): + logger.info(f"Reading libraries from {location}") + if not os.path.isdir(os.path.join(location, 'default')): + os.mkdir(os.path.join(location, 'default')) + for file in os.listdir(location): + if '.lib' in file: + self.stdout.write(f'Processing {file}') + lib_location = os.path.join(location, file) + lib_output_location = os.path.join( + location, 'default', 'symbol_svgs' + ) + component_details = generate_svg_and_save_to_folder( + lib_location, + lib_output_location + ) + library = Library.objects.filter( + library_name=file, library_set=library_set).first() + if not library: + library = Library( + library_name=file, + library_set=library_set + ) + library.save() + logger.info('Created Library Object') + library_svg_folder = os.path.join(lib_output_location, file[:-4]) + thumbnails = glob.glob(library_svg_folder+'/*_thumbnail.svg') + + # Seed Primary component + for component_svg in glob.glob(library_svg_folder+'/*-1-A.svg'): + thumbnail_path = component_svg[:-4]+'_thumbnail.svg' + if thumbnail_path not in thumbnails: + raise FileNotFoundError(f'Thumbnail Does not exist for {component_svg}') # noqa + + # Get Component name + component_svg = os.path.split(component_svg)[-1] + + # Get Corresponding Details + svg_desc = component_details[component_svg[:-4]] + + # Seed DB + try: + component = LibraryComponent.objects.get( + name=svg_desc['name'], + svg_path=os.path.join( + library_svg_folder, component_svg)[6:], + thumbnail_path=thumbnail_path, + symbol_prefix=svg_desc['symbol_prefix'], + full_name=svg_desc['full_name'], + keyword=svg_desc['keyword'], + description=svg_desc['description'], + data_link=svg_desc['data_link'], + component_library=library + ) + except LibraryComponent.DoesNotExist: + component = LibraryComponent( + name=svg_desc['name'], + svg_path=os.path.join( + library_svg_folder, component_svg)[6:], + thumbnail_path=thumbnail_path, + symbol_prefix=svg_desc['symbol_prefix'], + full_name=svg_desc['full_name'], + keyword=svg_desc['keyword'], + description=svg_desc['description'], + data_link=svg_desc['data_link'], + component_library=library + ) + component.save() + logger.info(f'Saved component {component_svg}') + + # Seed Alternate Components + for component_svg in glob.glob(library_svg_folder+'/*[B-Z].svg'): # noqa , EdgeCase here + component_svg = os.path.split(component_svg)[-1] + svg_desc = component_details[component_svg[:-4]] + alternate_component = ComponentAlternate( + part=svg_desc['part'], + dmg=svg_desc['dmg'], + full_name=svg_desc['full_name'], + svg_path=os.path.join( + library_svg_folder, component_svg), + parent_component=LibraryComponent.objects.get( + name=svg_desc['name'], + ) + ) + alternate_component.save() + logger.info(f'Saved alternate component {component_svg}') diff --git a/esim-cloud-backend/libAPI/management/commands/seed_libs.py b/esim-cloud-backend/libAPI/management/commands/seed_libs.py deleted file mode 100644 index bfd548e64..000000000 --- a/esim-cloud-backend/libAPI/management/commands/seed_libs.py +++ /dev/null @@ -1,106 +0,0 @@ -import os -from libAPI.models import Library, LibraryComponent, ComponentAlternate -from django.core.management.base import BaseCommand -from libAPI.helper.main import generate_svg_and_save_to_folder -import logging -import glob -logger = logging.getLogger(__name__) - - -class Command(BaseCommand): - help = "seed database for testing and development." - - def add_arguments(self, parser): - parser.add_argument('--clear', action='store_true', - help="True to clear all libraries from DB") - parser.add_argument('--location', type=self.dir_path, - help="Directory containing kicad library files") - - def dir_path(self, path): - if os.path.isdir(path): - return path - else: - raise Exception(f"{path} is not a valid path") - - def handle(self, *args, **options): - self.stdout.write('seeding data...') - if options['clear']: - self.stdout.write('Deleting Objects') - clear_data() - if not options['location'] and not options['clear']: - raise Exception('Argument location must be provided') - elif not options['clear'] and options['location']: - seed_libraries(self, options['location']) - self.stdout.write('done.') - - -def clear_data(): - """Deletes all the table data""" - Library.objects.all().delete() - LibraryComponent.objects.all().delete() - logger.info("Deleted All libraries and components") - - -def seed_libraries(self, location): - logger.info(f"Reading libraries from {location}") - for file in os.listdir(location): - if '.lib' in file: - self.stdout.write(f'Processing {file}') - lib_location = os.path.join(location, file) - lib_output_location = os.path.join(location, 'symbol_svgs') - component_details = generate_svg_and_save_to_folder( - lib_location, - lib_output_location - ) - library = Library( - library_name=file, - ) - library.save() - logger.info('Created Library Object') - library_svg_folder = os.path.join(lib_output_location, file[:-4]) - thumbnails = glob.glob(library_svg_folder+'/*_thumbnail.svg') - - # Seed Primary component - for component_svg in glob.glob(library_svg_folder+'/*-1-A.svg'): - thumbnail_path = component_svg[:-4]+'_thumbnail.svg' - if thumbnail_path not in thumbnails: - raise FileNotFoundError(f'Thumbnail Does not exist for {component_svg}') # noqa - - # Get Component name - component_svg = os.path.split(component_svg)[-1] - - # Get Corresponding Details - svg_desc = component_details[component_svg[:-4]] - - # Seed DB - component = LibraryComponent( - name=svg_desc['name'], - svg_path=os.path.join( - library_svg_folder, component_svg), - thumbnail_path=thumbnail_path, - symbol_prefix=svg_desc['symbol_prefix'], - full_name=svg_desc['full_name'], - keyword=svg_desc['keyword'], - description=svg_desc['description'], - data_link=svg_desc['data_link'], - component_library=library - ) - component.save() - logger.info(f'Saved component {component_svg}') - - # Seed Alternate Components - for component_svg in glob.glob(library_svg_folder+'/*[B-Z].svg'): # noqa , EdgeCase here - component_svg = os.path.split(component_svg)[-1] - svg_desc = component_details[component_svg[:-4]] - alternate_component = ComponentAlternate( - part=svg_desc['part'], - dmg=svg_desc['dmg'], - full_name=svg_desc['full_name'], - svg_path=os.path.join( - library_svg_folder, component_svg), - parent_component=LibraryComponent.objects.get( - name=svg_desc['name'], - ) - ) - alternate_component.save() - logger.info(f'Saved alternate component {component_svg}') diff --git a/esim-cloud-backend/migrations.sh b/esim-cloud-backend/migrations.sh index 9eda1ff95..8eda364ff 100644 --- a/esim-cloud-backend/migrations.sh +++ b/esim-cloud-backend/migrations.sh @@ -9,6 +9,5 @@ python manage.py migrate libAPI python manage.py makemigrations python manage.py migrate python manage.py collectstatic --noinput -# rm -r kicad-symbols/symbol_svgs/ -# python manage.py seed_libs --clear -# python manage.py seed_libs --location kicad-symbols/ +python manage.py createsuperuser_noinput --username=admin --password=admin +python manage.py load_default_libs --username=admin --location=kicad-symbols/