Skip to content

Commit

Permalink
Merge branch 'master' into PyAr#137-pending-sponsoring
Browse files Browse the repository at this point in the history
  • Loading branch information
samsagaz committed Feb 29, 2020
2 parents e01fa94 + aaf2e3c commit 3611299
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
44 changes: 35 additions & 9 deletions website/events/management/commands/upload_gdrive_invoices.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,35 @@
# https://drive.google.com/drive/u/0/folders/1Kvfmz1eTNd9y2ZAqosaN3Cn2ydUKjtN_
BASE_FOLDER = '1Kvfmz1eTNd9y2ZAqosaN3Cn2ydUKjtN_'

# folder names in function of invoice type
FOLDER_TYPE_NAMES = {
Expense.INVOICE_TYPE_A: 'Facturas A',
Expense.INVOICE_TYPE_B: 'Facturas B',
Expense.INVOICE_TYPE_C: 'Facturas C',
Expense.INVOICE_TYPE_TICKET: 'Tickets',
Expense.INVOICE_TYPE_OTHER: 'Otros',
}

# to avoid messing with gdrive every time
DIR_CACHE = {}


def ensure_directory(explorer, parent_id, dirname):
"""Ensure 'dirname' directory is present in 'parent'."""
cache_key = (parent_id, dirname)
if cache_key in DIR_CACHE:
return DIR_CACHE[cache_key]

for folder in explorer.list_folder(parent_id):
if folder['name'] == dirname:
folder_id = folder['id']
break
else:
print("Creating folder {!r} in parent {}".format(dirname, parent_id))
folder_id = explorer.create_folder(dirname, parent_id)
DIR_CACHE[cache_key] = folder_id
return folder_id


class Command(BaseCommand):
help = "Upload those invoices type A to gdrive"
Expand All @@ -25,7 +54,6 @@ def handle(self, *args, **options):
month = int(options['month'])

expenses = Expense.objects.filter(
invoice_type=Expense.INVOICE_TYPE_A,
invoice_date__year=year,
invoice_date__month=month,
).all()
Expand All @@ -38,7 +66,8 @@ def handle(self, *args, **options):
dest_filename = "{:%Y%m%d} - {}: {} [${}] ({}).{}".format(
exp.invoice_date, exp.event.name, exp.description,
exp.amount, orig_name, extension)
dest_foldername = exp.invoice_date.strftime("%Y%m")
dest_foldername_ym = exp.invoice_date.strftime("%Y%m")
dest_foldername_inv_type = FOLDER_TYPE_NAMES[exp.invoice_type]
print("Processing", repr(dest_filename))

# download the invoice content to a local temp file (flush at the end so all content is
Expand All @@ -51,13 +80,10 @@ def handle(self, *args, **options):
# upload to google drive
explorer = gdrive.Explorer()

# get the id of the month folder (or create it)
for folder in explorer.list_folder(BASE_FOLDER):
if folder['name'] == dest_foldername:
folder_id = folder['id']
break
else:
folder_id = explorer.create_folder(dest_foldername, BASE_FOLDER)
# ensure both dirs are present
parent_id = ensure_directory(explorer, BASE_FOLDER, dest_foldername_ym)
folder_id = ensure_directory(explorer, parent_id, dest_foldername_inv_type)

# upload
explorer.upload(local_temp_fh.name, folder_id, filename=dest_filename)
local_temp_fh.close()
4 changes: 4 additions & 0 deletions website/utils/gdrive.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
APPLICATION_NAME = 'AutoFacturador'
SETTINGS_FILE = "/tmp/gdrive_settings.json"

DEFAULT_MIMETYPE = 'application/octet-stream'

# turn off overdetailed debugging
httplib2.debuglevel = 0
logger = logging.getLogger()
Expand Down Expand Up @@ -67,6 +69,8 @@ def upload(self, filepath, folder, filename=None):
filename = os.path.basename(filepath)

mime_type, _ = mimetypes.guess_type(filename)
if mime_type is None:
mime_type = DEFAULT_MIMETYPE
media = http.MediaFileUpload(filepath, mimetype=mime_type, resumable=True)
metadata = {
'name': filename,
Expand Down

0 comments on commit 3611299

Please sign in to comment.