52 Generates the compressed csv file according to sales which matches with 'accounting_date' field
55 event (dict): It contains necessary info related to sales and client
59 bucket = event[
"bucket"]
61 s3_path =
"{}/monthly_reports/{}/".format(event[
"path"], date)
63 filename = f
"{file}.csv"
64 print(f
"Creating {filename}")
65 date = datetime.strptime(date,
"%Y-%m-%d")
66 csv_columns = [
"quantity",
"isrc_id",
"release_id",
"release_title",
"track_title",
"artists",
"label_id",
"territory_code",
"type",
"service_id",
"date",
67 "accounting_date",
"foreign_currency",
"total_foreign",
"exchange_rate",
"currency_period",
"local_currency",
"total_local"]
69 with open(path+filename,
'w', encoding=
'utf-8')
as csvfile:
70 writer = csv.DictWriter(csvfile, fieldnames=csv_columns)
72 fields = {
"_id":0,
"quantity": 1,
"isrc_id": 1,
"release_id": 1,
"release_title": 1,
"track_title": 1,
"artists": 1,
"label_id": 1,
"territory_code": 1,
73 "type": 1,
"service_id": 1,
"date": 1,
"accounting_date": 1,
"foreign_currency": 1,
"total_foreign": 1,
"exchange_rate": 1,
"currency_period":1,
"local_currency": 1,
"total_local":1}
74 template_sales = collection.find({
"accounting_date": {
"$in": [date]},
"client_id": event[
"client_id"],
"file": {
"$in":event[
"files"]}},fields)
75 for post
in template_sales:
78 if os.path.isfile(f
"{path+filename}.gz"):
79 os.system(
"rm {}.gz".format(path+filename))
80 command = f
"gzip {path+filename}"
82 filename = filename+
".gz"
83 url_to_download, final_path = upload_files_s3(bucket, s3_path, path, filename)
84 create_snapshot(event, final_path)
85 print(f
"{filename} is ready!")
86 if url_to_download ==
None:
87 raise Exception(
"Link not generated")
89 os.system(
"rm -r {}".format(path+filename))
90 return url_to_download
91 except Exception
as e:
92 raise Exception(
"An error occurred: {}".format(e))