Electronic stock management. -- not updated anymore
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

dbseed.py 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. from models import user_datastore, Tag, FootprintCategory, Manufacturer, \
  2. PartMeasurementUnit, Footprint, Unit, ManufacturerLogo
  3. from flask import current_app
  4. from posixpath import basename
  5. from datetime import datetime
  6. from hashlib import sha1
  7. from shutil import copyfile
  8. import yaml
  9. import json
  10. import os
  11. def make_db_seed(db):
  12. print("== Seeding database")
  13. seed_users(db)
  14. seed_manufacturers(db)
  15. seed_manufacturers_logos(db)
  16. seed_parts_units(db)
  17. seed_footprints(db)
  18. seed_units(db)
  19. def seed_users(db):
  20. print("++ Seeding users")
  21. user_datastore.create_user(
  22. email='dashie@sigpipe.me',
  23. password='fluttershy',
  24. name='toto'
  25. )
  26. db.session.commit()
  27. return
  28. def seed_parts_units(db):
  29. print("++ Seeding parts units")
  30. for i in [['Centimeters', 'cm'], ['Pieces', 'pcs']]:
  31. a = PartMeasurementUnit()
  32. a.name = i[0]
  33. a.short_name = i[1]
  34. db.session.add(a)
  35. db.session.commit()
  36. def seed_manufacturers(db):
  37. print("++ Seeding manufacturers")
  38. with open('setup-data/manufacturers/manufacturers.yaml', 'r') as stream:
  39. manlist = yaml.load(stream)
  40. for i in manlist:
  41. if db.session.query(Manufacturer).filter(Manufacturer.name == i).first():
  42. continue
  43. a = Manufacturer()
  44. a.name = i
  45. db.session.add(a)
  46. db.session.commit()
  47. def seed_footprints(db):
  48. print("++ Seeding footprints")
  49. with open('setup-data/footprints/footprints.yaml', 'r') as stream:
  50. z = yaml.load(stream)
  51. for i in z:
  52. # i = category
  53. cat = FootprintCategory()
  54. cat.name = i
  55. db.session.add(cat)
  56. for ii in z[i]:
  57. # ii = footprint name
  58. f = Footprint()
  59. f.name = ii
  60. f.footprint_category = cat
  61. db.session.add(f)
  62. db.session.commit()
  63. def seed_units(db):
  64. print("++ Seeding units")
  65. with open('setup-data/units.yaml', 'r') as stream:
  66. units = yaml.load(stream)
  67. for i in units:
  68. for ii in units[i]['prefixes']:
  69. a = Unit()
  70. a.name = i
  71. a.symbol = units[i]['symbol']
  72. a.prefix = ii
  73. a.slug = "%s%s" % (a.symbol, a.prefix)
  74. db.session.add(a)
  75. db.session.commit()
  76. def seed_manufacturers_logos(db):
  77. print("++ Seeding manufacturers logos")
  78. with open('setup-data/manufacturers/manufacturers.yaml', 'r') as stream:
  79. manlist = yaml.load(stream)
  80. for i in manlist:
  81. manufacturer = db.session.query(Manufacturer).filter(Manufacturer.name == i).first()
  82. if not manufacturer:
  83. continue
  84. for logo in manlist[i]:
  85. _file = os.path.join('setup-data/manufacturers/images', logo)
  86. if os.path.isfile(_file):
  87. print("[LOGO] Seeding %s" % _file)
  88. else:
  89. print("[LOGO] File referenced doesn't exists: %s" % _file)
  90. continue
  91. a = ManufacturerLogo()
  92. a.manufacturer_id = manufacturer.id
  93. # Calculate destination file hash
  94. _str = "{0} {1}".format(logo,
  95. str(datetime.now()))
  96. _hash = sha1()
  97. _hash.update(_str)
  98. _hashi = _hash.hexdigest()
  99. _hash_name = "{0}{1}".format(_hashi, os.path.splitext(logo)[1]) # dot included in splitext
  100. fname = os.path.join(current_app.config['UPLOADS_DEFAULT_DEST'],
  101. 'attachments',
  102. str(ManufacturerLogo.BARCODE_TYPE),
  103. _hash_name)
  104. copyfile(_file, fname)
  105. a.filesize = os.stat(fname).st_size
  106. a.filename = _hash_name
  107. a.orig_filename = logo
  108. db.session.add(a)
  109. db.session.commit()