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.

crons.py 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import os
  2. import logging
  3. from logging.handlers import RotatingFileHandler
  4. from models import PartAttachment, Part, ManufacturerLogo
  5. import urllib
  6. from posixpath import basename
  7. import urlparse
  8. from datetime import datetime
  9. from hashlib import sha1
  10. import magic
  11. from PIL import Image
  12. logger = logging.getLogger()
  13. TRIANGLE = {
  14. "mini": (50, 50),
  15. "small": (200, 150),
  16. "medium": (400, 400),
  17. "preview": (800, 800)
  18. }
  19. def illuminatis(cfg, _file, size, barcode_id, _logger):
  20. if 'is_remote' in dir(_file) or 'remote_cached' in dir(_file):
  21. if _file.is_remote and _file.remote_cached:
  22. remote = True
  23. else:
  24. remote = False
  25. else:
  26. remote = False
  27. if remote:
  28. _logger.info("Cached file")
  29. infile = os.path.join(cfg['UPLOADS_DEFAULT_DEST'],
  30. 'cache',
  31. str(barcode_id),
  32. _file.filename)
  33. else:
  34. _logger.info("Else file is not remote OR not cached yet")
  35. infile = os.path.join(cfg['UPLOADS_DEFAULT_DEST'],
  36. 'attachments',
  37. str(barcode_id),
  38. _file.filename)
  39. outfile = os.path.join(cfg['UPLOADS_DEFAULT_DEST'],
  40. 'thumbs',
  41. str(barcode_id),
  42. "thumb_%s.%s" % (size, _file.filename))
  43. the_dir = os.path.join(cfg['UPLOADS_DEFAULT_DEST'], 'thumbs', str(barcode_id))
  44. if not os.path.isdir(the_dir):
  45. _logger.info("Creating dir %s" % the_dir)
  46. os.makedirs(the_dir)
  47. if not os.path.isfile(outfile) and os.path.isfile(infile):
  48. im = Image.open(infile)
  49. im.thumbnail(TRIANGLE[size], Image.ANTIALIAS)
  50. im.save(outfile)
  51. _logger.info("[cron_thumbs] Created %s(%s) thumb for %s" % (size, TRIANGLE[size], _file.filename))
  52. if not os.path.isfile(infile):
  53. _logger.error("Should exists: %s" % infile)
  54. if os.path.isfile(outfile):
  55. _logger.info("Nothing to do.")
  56. def c_cron_thumbs(config, db):
  57. formatter = logging.Formatter('%(asctime)s :: %(levelname)s :: %(message)s')
  58. logger.setLevel(logging.DEBUG)
  59. file_handler = RotatingFileHandler("%s/cron_thumbs.log" % os.getcwd(), 'a', 1000000, 1)
  60. file_handler.setLevel(logging.DEBUG)
  61. file_handler.setFormatter(formatter)
  62. logger.addHandler(file_handler)
  63. steam_handler = logging.StreamHandler()
  64. steam_handler.setLevel(logging.DEBUG)
  65. steam_handler.setFormatter(formatter)
  66. logger.addHandler(steam_handler)
  67. logger.info("Started creating thumbnails.")
  68. images = PartAttachment.query.filter(PartAttachment.simple_type == "IMAGE").all()
  69. for i in images:
  70. logger.info("[IMAGES][{0}]: {1}".format(i.id, i.orig_filename))
  71. illuminatis(config, i, "mini", Part.BARCODE_TYPE, logger)
  72. illuminatis(config, i, "small", Part.BARCODE_TYPE, logger)
  73. illuminatis(config, i, "medium", Part.BARCODE_TYPE, logger)
  74. illuminatis(config, i, "preview", Part.BARCODE_TYPE, logger)
  75. logos = ManufacturerLogo.query.all()
  76. for i in logos:
  77. logger.info("[LOGOS][{0}]: {1}".format(i.id, i.orig_filename))
  78. illuminatis(config, i, "mini", ManufacturerLogo.BARCODE_TYPE, logger)
  79. illuminatis(config, i, "small", ManufacturerLogo.BARCODE_TYPE, logger)
  80. logger.info("Finished creating thumbnails.")
  81. def c_cron_cache(config, db):
  82. formatter = logging.Formatter('%(asctime)s :: %(levelname)s :: %(message)s')
  83. logger.setLevel(logging.DEBUG)
  84. file_handler = RotatingFileHandler("%s/cron_cache.log" % os.getcwd(), 'a', 1000000, 1)
  85. file_handler.setLevel(logging.DEBUG)
  86. file_handler.setFormatter(formatter)
  87. logger.addHandler(file_handler)
  88. steam_handler = logging.StreamHandler()
  89. steam_handler.setLevel(logging.DEBUG)
  90. steam_handler.setFormatter(formatter)
  91. logger.addHandler(steam_handler)
  92. logger.info("Started creating locale cached files.")
  93. attachments = PartAttachment.query.filter(PartAttachment.is_remote.is_(True),
  94. PartAttachment.remote_processed.is_(False)).all()
  95. for i in attachments:
  96. logger.info("[{0}/{1}]: {2}".format(i.part.id, i.id, i.remote_uri))
  97. if not i.remote_uri:
  98. i.remote_processed = True
  99. continue
  100. if i.part.attachments.filter(PartAttachment.simple_type == 'PDF').count() <= 0:
  101. i.default = True
  102. else:
  103. i.default = False
  104. u = urllib.URLopener()
  105. temp_name = "__TEMP__{0}".format(basename(urlparse.urlparse(i.remote_uri).path))
  106. orig_name = basename(urlparse.urlparse(i.remote_uri).path)
  107. dir_dest = os.path.join(config['UPLOADS_DEFAULT_DEST'], 'cache', str(Part.BARCODE_TYPE))
  108. if not os.path.isdir(dir_dest):
  109. os.makedirs(dir_dest)
  110. try:
  111. u.retrieve(i.remote_uri, os.path.join(dir_dest, temp_name))
  112. except IOError as e:
  113. logger.error("Error downloading file %s" % i.remote_uri)
  114. logger.error(e.args)
  115. i.cache_error = "HTTP Error: {0}".format(e.args)
  116. continue # jump to next one
  117. _str = "{0} {1} {2}".format(i.part.name,
  118. orig_name,
  119. str(datetime.now()))
  120. _hash = sha1()
  121. _hash.update(_str)
  122. _hashi = _hash.hexdigest()
  123. new_name = "%s_%s" % (_hashi, orig_name)
  124. if os.path.isfile(os.path.join(dir_dest, temp_name)):
  125. os.rename(os.path.join(dir_dest, temp_name),
  126. os.path.join(dir_dest, new_name))
  127. i.orig_filename = orig_name
  128. i.filename = new_name
  129. filesize = os.stat(os.path.join(dir_dest, new_name)).st_size
  130. _magic = magic.from_file(os.path.join(dir_dest, new_name), mime=True).decode('unicode_escape')
  131. if _magic == "application/pdf":
  132. st = "PDF"
  133. elif _magic.startswith("image/"):
  134. st = "IMAGE"
  135. elif _magic.startswith("text/"):
  136. st = "TEXT"
  137. else:
  138. st = "UNKNOWN"
  139. i.simple_type = st
  140. i.mimetype = _magic
  141. i.hash = _hashi
  142. i.filesize = filesize
  143. if os.path.isfile(os.path.join(dir_dest, new_name)):
  144. i.remote_cached = True
  145. i.remote_processed = True
  146. logger.info("[{0}] Processed as {1}".format(
  147. i.id,
  148. os.path.join(dir_dest, new_name)
  149. ))
  150. db.session.commit()
  151. logger.info("Finished creating locale cached files.")