mirror of
https://github.com/nottinghamtec/PyRIGS.git
synced 2026-01-24 17:02:18 +00:00
Create the Asset Database (#363)
This commit is contained in:
0
assets/management/__init__.py
Normal file
0
assets/management/__init__.py
Normal file
0
assets/management/commands/__init__.py
Normal file
0
assets/management/commands/__init__.py
Normal file
23
assets/management/commands/deleteSampleData.py
Normal file
23
assets/management/commands/deleteSampleData.py
Normal file
@@ -0,0 +1,23 @@
|
||||
from django.core.management.base import BaseCommand, CommandError
|
||||
|
||||
from assets import models
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = 'Deletes testing sample data'
|
||||
|
||||
def handle(self, *args, **kwargs):
|
||||
from django.conf import settings
|
||||
|
||||
if not (settings.DEBUG):
|
||||
raise CommandError('You cannot run this command in production')
|
||||
|
||||
self.delete_objects(models.AssetCategory)
|
||||
self.delete_objects(models.AssetStatus)
|
||||
self.delete_objects(models.Supplier)
|
||||
self.delete_objects(models.Connector)
|
||||
self.delete_objects(models.Asset)
|
||||
|
||||
def delete_objects(self, model):
|
||||
for object in model.objects.all():
|
||||
object.delete()
|
||||
114
assets/management/commands/generateSampleAssetsData.py
Normal file
114
assets/management/commands/generateSampleAssetsData.py
Normal file
@@ -0,0 +1,114 @@
|
||||
from django.core.management.base import BaseCommand, CommandError
|
||||
from django.utils import timezone
|
||||
|
||||
from assets import models
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = 'Creates some sample data for testing'
|
||||
|
||||
def handle(self, *args, **kwargs):
|
||||
from django.conf import settings
|
||||
|
||||
if not (settings.DEBUG or settings.STAGING):
|
||||
raise CommandError('You cannot run this command in production')
|
||||
|
||||
random.seed('Some object to see the random number generator')
|
||||
|
||||
self.create_categories()
|
||||
self.create_statuses()
|
||||
self.create_suppliers()
|
||||
self.create_assets()
|
||||
self.create_connectors()
|
||||
self.create_cables()
|
||||
|
||||
def create_categories(self):
|
||||
categories = ['Case', 'Video', 'General', 'Sound', 'Lighting', 'Rigging']
|
||||
|
||||
for cat in categories:
|
||||
models.AssetCategory.objects.create(name=cat)
|
||||
|
||||
def create_statuses(self):
|
||||
statuses = [('In Service', True), ('Lost', False), ('Binned', False), ('Sold', False), ('Broken', False)]
|
||||
|
||||
for stat in statuses:
|
||||
models.AssetStatus.objects.create(name=stat[0], should_show=stat[1])
|
||||
|
||||
def create_suppliers(self):
|
||||
suppliers = ["Acme, inc.", "Widget Corp", "123 Warehousing", "Demo Company", "Smith and Co.", "Foo Bars", "ABC Telecom", "Fake Brothers", "QWERTY Logistics", "Demo, inc.", "Sample Company", "Sample, inc", "Acme Corp", "Allied Biscuit", "Ankh-Sto Associates", "Extensive Enterprise", "Galaxy Corp", "Globo-Chem", "Mr. Sparkle", "Globex Corporation", "LexCorp", "LuthorCorp", "North Central Positronics", "Omni Consimer Products", "Praxis Corporation", "Sombra Corporation", "Sto Plains Holdings", "Tessier-Ashpool", "Wayne Enterprises", "Wentworth Industries", "ZiffCorp", "Bluth Company", "Strickland Propane", "Thatherton Fuels", "Three Waters", "Water and Power", "Western Gas & Electric", "Mammoth Pictures", "Mooby Corp", "Gringotts", "Thrift Bank", "Flowers By Irene", "The Legitimate Businessmens Club", "Osato Chemicals", "Transworld Consortium", "Universal Export", "United Fried Chicken", "Virtucon", "Kumatsu Motors", "Keedsler Motors", "Powell Motors", "Industrial Automation", "Sirius Cybernetics Corporation", "U.S. Robotics and Mechanical Men", "Colonial Movers", "Corellian Engineering Corporation", "Incom Corporation", "General Products", "Leeding Engines Ltd.", "Blammo", # noqa
|
||||
"Input, Inc.", "Mainway Toys", "Videlectrix", "Zevo Toys", "Ajax", "Axis Chemical Co.", "Barrytron", "Carrys Candles", "Cogswell Cogs", "Spacely Sprockets", "General Forge and Foundry", "Duff Brewing Company", "Dunder Mifflin", "General Services Corporation", "Monarch Playing Card Co.", "Krustyco", "Initech", "Roboto Industries", "Primatech", "Sonky Rubber Goods", "St. Anky Beer", "Stay Puft Corporation", "Vandelay Industries", "Wernham Hogg", "Gadgetron", "Burleigh and Stronginthearm", "BLAND Corporation", "Nordyne Defense Dynamics", "Petrox Oil Company", "Roxxon", "McMahon and Tate", "Sixty Second Avenue", "Charles Townsend Agency", "Spade and Archer", "Megadodo Publications", "Rouster and Sideways", "C.H. Lavatory and Sons", "Globo Gym American Corp", "The New Firm", "SpringShield", "Compuglobalhypermeganet", "Data Systems", "Gizmonic Institute", "Initrode", "Taggart Transcontinental", "Atlantic Northern", "Niagular", "Plow King", "Big Kahuna Burger", "Big T Burgers and Fries", "Chez Quis", "Chotchkies", "The Frying Dutchman", "Klimpys", "The Krusty Krab", "Monks Diner", "Milliways", "Minuteman Cafe", "Taco Grande", "Tip Top Cafe", "Moes Tavern", "Central Perk", "Chasers"] # noqa
|
||||
|
||||
for supplier in suppliers:
|
||||
models.Supplier.objects.create(name=supplier)
|
||||
|
||||
def create_assets(self):
|
||||
asset_description = ['Large cable', 'Shiny thing', 'New lights', 'Really expensive microphone', 'Box of fuse flaps', 'Expensive tool we didn\'t agree to buy', 'Cable drums', 'Boring amount of tape', 'Video stuff no one knows how to use', 'More amplifiers', 'Heatshrink']
|
||||
|
||||
categories = models.AssetCategory.objects.all()
|
||||
statuses = models.AssetStatus.objects.all()
|
||||
suppliers = models.Supplier.objects.all()
|
||||
|
||||
for i in range(100):
|
||||
asset = models.Asset.objects.create(
|
||||
asset_id='{}'.format(models.Asset.get_available_asset_id()),
|
||||
description=random.choice(asset_description),
|
||||
category=random.choice(categories),
|
||||
status=random.choice(statuses),
|
||||
date_acquired=timezone.now().date()
|
||||
)
|
||||
|
||||
if i % 4 == 0:
|
||||
asset.parent = models.Asset.objects.order_by('?').first()
|
||||
|
||||
if i % 3 == 0:
|
||||
asset.purchased_from = random.choice(suppliers)
|
||||
|
||||
asset.save()
|
||||
|
||||
def create_cables(self):
|
||||
asset_description = ['The worm', 'Harting without a cap', 'Heavy cable', 'Extension lead', 'IEC cable that we should remember to prep']
|
||||
|
||||
csas = [0.75, 1.00, 1.25, 2.5, 4]
|
||||
lengths = [1, 2, 5, 10, 15, 20, 25, 30, 50, 100]
|
||||
cores = [3, 5]
|
||||
circuits = [1, 2, 3, 6]
|
||||
categories = models.AssetCategory.objects.all()
|
||||
statuses = models.AssetStatus.objects.all()
|
||||
suppliers = models.Supplier.objects.all()
|
||||
connectors = models.Connector.objects.all()
|
||||
|
||||
for i in range(100):
|
||||
asset = models.Asset.objects.create(
|
||||
asset_id='{}'.format(models.Asset.get_available_asset_id()),
|
||||
description=random.choice(asset_description),
|
||||
category=random.choice(categories),
|
||||
status=random.choice(statuses),
|
||||
date_acquired=timezone.now().date(),
|
||||
|
||||
is_cable=True,
|
||||
plug=random.choice(connectors),
|
||||
socket=random.choice(connectors),
|
||||
csa=random.choice(csas),
|
||||
length=random.choice(lengths),
|
||||
circuits=random.choice(circuits),
|
||||
cores=random.choice(circuits)
|
||||
)
|
||||
|
||||
if i % 4 == 0:
|
||||
asset.parent = models.Asset.objects.order_by('?').first()
|
||||
|
||||
if i % 3 == 0:
|
||||
asset.purchased_from = random.choice(suppliers)
|
||||
|
||||
asset.save()
|
||||
|
||||
def create_connectors(self):
|
||||
connectors = [
|
||||
{"description": "13A UK", "current_rating": 13, "voltage_rating": 230, "num_pins": 3},
|
||||
{"description": "16A", "current_rating": 16, "voltage_rating": 230, "num_pins": 3},
|
||||
{"description": "32/3", "current_rating": 32, "voltage_rating": 400, "num_pins": 5},
|
||||
{"description": "Socapex", "current_rating": 23, "voltage_rating": 600, "num_pins": 19},
|
||||
]
|
||||
for connector in connectors:
|
||||
conn = models.Connector.objects.create(** connector)
|
||||
conn.save()
|
||||
229
assets/management/commands/import_old_db.py
Normal file
229
assets/management/commands/import_old_db.py
Normal file
@@ -0,0 +1,229 @@
|
||||
import os
|
||||
import datetime
|
||||
import xml.etree.ElementTree as ET
|
||||
from django.core.management.base import BaseCommand
|
||||
from django.conf import settings
|
||||
|
||||
from assets import models
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = 'Imports old db from XML dump'
|
||||
|
||||
epoch = datetime.date(1970, 1, 1)
|
||||
|
||||
def handle(self, *args, **options):
|
||||
self.import_categories()
|
||||
self.import_statuses()
|
||||
self.import_suppliers()
|
||||
self.import_collections()
|
||||
self.import_assets()
|
||||
self.import_cables()
|
||||
|
||||
@staticmethod
|
||||
def xml_path(file):
|
||||
return os.path.join(settings.BASE_DIR, 'data/DB_Dump/{}'.format(file))
|
||||
|
||||
@staticmethod
|
||||
def parse_xml(file):
|
||||
tree = ET.parse(file)
|
||||
|
||||
return tree.getroot()
|
||||
|
||||
def import_categories(self):
|
||||
# 0: updated, 1: created
|
||||
tally = [0, 0]
|
||||
root = self.parse_xml(self.xml_path('TEC_Asset_Categories.xml'))
|
||||
|
||||
for child in root:
|
||||
obj, created = models.AssetCategory.objects.update_or_create(
|
||||
pk=int(child.find('AssetCategoryID').text),
|
||||
name=child.find('AssetCategory').text
|
||||
)
|
||||
|
||||
if created:
|
||||
tally[1] += 1
|
||||
else:
|
||||
tally[0] += 1
|
||||
|
||||
print('Categories - Updated: {}, Created: {}'.format(tally[0], tally[1]))
|
||||
|
||||
def import_statuses(self):
|
||||
# 0: updated, 1: created
|
||||
tally = [0, 0]
|
||||
root = self.parse_xml(self.xml_path('TEC_Asset_Status_new.xml'))
|
||||
|
||||
for child in root:
|
||||
obj, created = models.AssetStatus.objects.update_or_create(
|
||||
pk=int(child.find('StatusID').text),
|
||||
name=child.find('Status').text
|
||||
)
|
||||
|
||||
if created:
|
||||
tally[1] += 1
|
||||
else:
|
||||
tally[0] += 1
|
||||
|
||||
print('Statuses - Updated: {}, Created: {}'.format(tally[0], tally[1]))
|
||||
|
||||
def import_suppliers(self):
|
||||
# 0: updated, 1: created
|
||||
tally = [0, 0]
|
||||
root = self.parse_xml(self.xml_path('TEC_Asset_Suppliers_new.xml'))
|
||||
|
||||
for child in root:
|
||||
obj, created = models.Supplier.objects.update_or_create(
|
||||
pk=int(child.find('Supplier_x0020_Id').text),
|
||||
name=child.find('Supplier_x0020_Name').text
|
||||
)
|
||||
|
||||
if created:
|
||||
tally[1] += 1
|
||||
else:
|
||||
tally[0] += 1
|
||||
|
||||
print('Suppliers - Updated: {}, Created: {}'.format(tally[0], tally[1]))
|
||||
|
||||
def import_assets(self):
|
||||
# 0: updated, 1: created
|
||||
tally = [0, 0]
|
||||
root = self.parse_xml(self.xml_path('TEC_Assets.xml'))
|
||||
|
||||
for child in root:
|
||||
defaults = dict()
|
||||
|
||||
# defaults['pk'] = int(child.find('ID').text)
|
||||
defaults['asset_id'] = child.find('AssetID').text
|
||||
|
||||
try:
|
||||
defaults['description'] = child.find('AssetDescription').text
|
||||
except AttributeError:
|
||||
defaults['description'] = 'None'
|
||||
|
||||
defaults['category'] = models.AssetCategory.objects.get(pk=int(child.find('AssetCategoryID').text))
|
||||
defaults['status'] = models.AssetStatus.objects.get(pk=int(child.find('StatusID').text))
|
||||
|
||||
try:
|
||||
defaults['serial_number'] = child.find('SerialNumber').text
|
||||
except AttributeError:
|
||||
pass
|
||||
|
||||
try:
|
||||
defaults['purchased_from'] = models.Supplier.objects.get(pk=int(child.find('Supplier_x0020_Id').text))
|
||||
except AttributeError:
|
||||
pass
|
||||
|
||||
try:
|
||||
defaults['date_acquired'] = datetime.datetime.strptime(child.find('DateAcquired').text, '%d/%m/%Y').date()
|
||||
except AttributeError:
|
||||
defaults['date_acquired'] = self.epoch
|
||||
|
||||
try:
|
||||
defaults['date_sold'] = datetime.datetime.strptime(child.find('DateSold').text, '%d/%m/%Y').date()
|
||||
except AttributeError:
|
||||
pass
|
||||
|
||||
try:
|
||||
defaults['purchase_price'] = float(child.find('Replacement_x0020_Value').text)
|
||||
except AttributeError:
|
||||
pass
|
||||
|
||||
try:
|
||||
defaults['salvage_value'] = float(child.find('SalvageValue').text)
|
||||
except AttributeError:
|
||||
pass
|
||||
|
||||
try:
|
||||
defaults['comments'] = child.find('Comments').text
|
||||
except AttributeError:
|
||||
pass
|
||||
|
||||
try:
|
||||
date = child.find('NextSchedMaint').text.split('T')[0]
|
||||
defaults['next_sched_maint'] = datetime.datetime.strptime(date, '%Y-%m-%d').date()
|
||||
except AttributeError:
|
||||
pass
|
||||
|
||||
print(defaults)
|
||||
|
||||
obj, created = models.Asset.objects.update_or_create(**defaults)
|
||||
|
||||
if created:
|
||||
tally[1] += 1
|
||||
else:
|
||||
tally[0] += 1
|
||||
|
||||
print('Assets - Updated: {}, Created: {}'.format(tally[0], tally[1]))
|
||||
|
||||
def import_collections(self):
|
||||
tally = [0, 0]
|
||||
root = self.parse_xml(self.xml_path('TEC_Cable_Collections.xml'))
|
||||
|
||||
for child in root:
|
||||
defaults = dict()
|
||||
|
||||
defaults['pk'] = int(child.find('ID').text)
|
||||
defaults['name'] = child.find('Cable_x0020_Trunk').text
|
||||
|
||||
obj, created = models.Collection.objects.update_or_create(**defaults)
|
||||
|
||||
if created:
|
||||
tally[1] += 1
|
||||
else:
|
||||
tally[0] += 1
|
||||
|
||||
print('Collections - Updated: {}, Created: {}'.format(tally[0], tally[1]))
|
||||
|
||||
def import_cables(self):
|
||||
tally = [0, 0]
|
||||
root = self.parse_xml(self.xml_path('TEC_Cables.xml'))
|
||||
|
||||
for child in root:
|
||||
defaults = dict()
|
||||
|
||||
defaults['asset_id'] = child.find('Asset_x0020_Number').text
|
||||
|
||||
try:
|
||||
defaults['description'] = child.find('Type_x0020_of_x0020_Cable').text
|
||||
except AttributeError:
|
||||
defaults['description'] = 'None'
|
||||
|
||||
defaults['is_cable'] = True
|
||||
defaults['category'] = models.AssetCategory.objects.get(pk=9)
|
||||
|
||||
try:
|
||||
defaults['length'] = child.find('Length_x0020__x0028_m_x0029_').text
|
||||
except AttributeError:
|
||||
pass
|
||||
|
||||
defaults['status'] = models.AssetStatus.objects.get(pk=int(child.find('Status').text))
|
||||
|
||||
try:
|
||||
defaults['comments'] = child.find('Comments').text
|
||||
except AttributeError:
|
||||
pass
|
||||
|
||||
try:
|
||||
collection_id = int(child.find('Collection').text)
|
||||
if collection_id != 0:
|
||||
defaults['collection'] = models.Collection.objects.get(pk=collection_id)
|
||||
except AttributeError:
|
||||
pass
|
||||
|
||||
try:
|
||||
defaults['purchase_price'] = float(child.find('Purchase_x0020_Price').text)
|
||||
except AttributeError:
|
||||
pass
|
||||
|
||||
defaults['date_acquired'] = self.epoch
|
||||
|
||||
print(defaults)
|
||||
|
||||
obj, created = models.Asset.objects.update_or_create(**defaults)
|
||||
|
||||
if created:
|
||||
tally[1] += 1
|
||||
else:
|
||||
tally[0] += 1
|
||||
|
||||
print('Collections - Updated: {}, Created: {}'.format(tally[0], tally[1]))
|
||||
110
assets/management/commands/update_old_db_file.py
Normal file
110
assets/management/commands/update_old_db_file.py
Normal file
@@ -0,0 +1,110 @@
|
||||
import os
|
||||
import datetime
|
||||
import xml.etree.ElementTree as ET
|
||||
from django.core.management.base import BaseCommand
|
||||
from django.conf import settings
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = 'Imports old db from XML dump'
|
||||
|
||||
epoch = datetime.date(1970, 1, 1)
|
||||
|
||||
def handle(self, *args, **options):
|
||||
# self.update_statuses()
|
||||
# self.update_suppliers()
|
||||
self.update_cable_statuses()
|
||||
|
||||
@staticmethod
|
||||
def xml_path(file):
|
||||
return os.path.join(settings.BASE_DIR, 'data/DB_Dump/{}'.format(file))
|
||||
|
||||
@staticmethod
|
||||
def parse_xml(file):
|
||||
tree = ET.parse(file)
|
||||
|
||||
return tree.getroot()
|
||||
|
||||
def update_statuses(self):
|
||||
file = self.xml_path('TEC_Assets.xml')
|
||||
tree = ET.parse(file)
|
||||
root = tree.getroot()
|
||||
|
||||
# map old status pk to new status pk
|
||||
status_map = {
|
||||
2: 2,
|
||||
3: 4,
|
||||
4: 3,
|
||||
5: 5,
|
||||
6: 1
|
||||
}
|
||||
|
||||
for child in root:
|
||||
status = int(child.find('StatusID').text)
|
||||
child.find('StatusID').text = str(status_map[status])
|
||||
|
||||
tree.write(file)
|
||||
|
||||
def update_suppliers(self):
|
||||
old_file = self.xml_path('TEC_Asset_Suppliers.xml')
|
||||
old_tree = ET.parse(old_file)
|
||||
old_root = old_tree.getroot()
|
||||
|
||||
new_file = self.xml_path('TEC_Asset_Suppliers_new.xml')
|
||||
new_tree = ET.parse(new_file)
|
||||
new_root = new_tree.getroot()
|
||||
|
||||
# map old supplier pk to new supplier pk
|
||||
supplier_map = dict()
|
||||
|
||||
def find_in_old(name, root):
|
||||
for child in root:
|
||||
found_id = child.find('Supplier_x0020_Id').text
|
||||
found_name = child.find('Supplier_x0020_Name').text
|
||||
|
||||
if found_name == name:
|
||||
return found_id
|
||||
|
||||
for new_child in new_root:
|
||||
new_id = new_child.find('Supplier_x0020_Id').text
|
||||
new_name = new_child.find('Supplier_x0020_Name').text
|
||||
|
||||
old_id = find_in_old(new_name, old_root)
|
||||
|
||||
supplier_map[int(old_id)] = int(new_id)
|
||||
|
||||
file = self.xml_path('TEC_Assets.xml')
|
||||
tree = ET.parse(file)
|
||||
root = tree.getroot()
|
||||
|
||||
for child in root:
|
||||
try:
|
||||
supplier = int(child.find('Supplier_x0020_Id').text)
|
||||
child.find('Supplier_x0020_Id').text = str(supplier_map[supplier])
|
||||
except AttributeError:
|
||||
pass
|
||||
|
||||
tree.write(file)
|
||||
|
||||
def update_cable_statuses(self):
|
||||
file = self.xml_path('TEC_Cables.xml')
|
||||
tree = ET.parse(file)
|
||||
root = tree.getroot()
|
||||
|
||||
# map old status pk to new status pk
|
||||
status_map = {
|
||||
0: 7,
|
||||
1: 3,
|
||||
3: 2,
|
||||
4: 5,
|
||||
6: 6,
|
||||
7: 1,
|
||||
8: 4,
|
||||
9: 2,
|
||||
}
|
||||
|
||||
for child in root:
|
||||
status = int(child.find('Status').text)
|
||||
child.find('Status').text = str(status_map[status])
|
||||
|
||||
tree.write(file)
|
||||
Reference in New Issue
Block a user