Skip to content

Instantly share code, notes, and snippets.

@Ibrahem3amer
Created March 2, 2018 14:35
Show Gist options
  • Select an option

  • Save Ibrahem3amer/1c0b3e9a4759fc3ed7b9877d16242f19 to your computer and use it in GitHub Desktop.

Select an option

Save Ibrahem3amer/1c0b3e9a4759fc3ed7b9877d16242f19 to your computer and use it in GitHub Desktop.
class DataResource(resources.ModelResource):
"""
Data Resource is a class to export the data into excel
using the import export library
"""
query_param = ''
file_category = FileCategory()
document = Doc()
class Meta:
model = FileData
fields = ('data',)
def __init__(self, filter_param, file_category=None, document=None, is_downloaded=None, start_date=None, end_date=None):
self.query_param = filter_param
self.file_category = file_category
self.document = document
self.start_date = start_date
self.end_date = end_date
self.is_downloaded = is_downloaded
def get_today_transaction(self):
"""
Returns today's transactions for specific user.
:param user: USER instance that is sent in request.
:return: QuerySet
"""
return self._meta.model.objects.today_transactions(self.file_category.user_created)
def get_doc_transactions(self):
"""
Returns all transactions that relates to some doc.
:return: QuerySet
"""
return self._meta.model.objects.transaction_in_doc(self.document)
def get_filtered_transactions(self, user, start_date, end_date):
"""
Returns all transactions that fit between two dates.
:return: QuerySet
"""
return self._meta.model.objects.transactions_in_range(user, start_date, end_date)
def get_unseen_transactions(self, user):
"""
Retruns all transactions that happened after user.transactions_last_seen (DateTimeField)
:param user: USER instance that is sent in request.
:return: QuerySet
"""
return self._meta.model.objects.unseen_transactions(user)
def get_export_headers(self):
headers = self.file_category.identifiers()
if headers is None:
return []
headers.extend([
"Transaction Ref",
"Transaction Amount",
"Credit",
"Remaining Other Amount",
"Transaction Date",
"Transaction Method",
"Type of Payment",
"Status",
"Dispute"
])
return headers
def dehydrate_data(self, obj):
"""
it will return data based of file_category identifiers
:param obj: FileData instance.
:return: Exportable data
"""
identifiers = self.get_export_headers()
data = []
transactions = obj.transactions.all()
for transaction in transactions:
type_of_payment = transaction.type_of_payment
type_str = "Full Payment"
if type_of_payment == 1:
type_str = "Partial Payment"
else:
type_str = "Over Payment"
for idn in identifiers[:-4]:
data.extend([obj.data[idn]])
try:
remaining_other_amount = obj.data[self.file_category.fine_field]
credit = obj.data["credit"]
data.extend([
transaction.rrn,
transaction.amount,
credit,
remaining_other_amount,
transaction.datetime,
transaction.status,
"Wallet" if transaction.status == 0 else "Cash",
type_str,
transaction.is_dispute
])
except:
data.extend(['', '', '', '', '', '', '', '', ''])
return data
def export_resource(self, obj):
"""
Returns the desired output of filtered data.
:param obj: Instance of FileData class.
:return: Redirection
"""
return self.dehydrate_data(obj)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment