import lowfat.models as models

"""
Explanation: Data Analyse
If you want to do your own analyse of the data on db.sqlite3 and are going to use Python you can take advantage of some Django code. This Jupyter Notebook will help you to enable the Django code.
Setup and run
To setup your environment to run this Jupyter notebook you need to install some packages. Our suggestion is to run
~~~
$ python -m pip install -r requirements.txt
$ python -m pip install -r requirements-jupyter.txt
~~~
from your terminal.
To start Jupyter server, run
~~~
$ python manage.py shell_plus --notebook
~~~
Basic (Django Part)
You can use all power of Django on the notebook. For example, to gain access to the models you can use
End of explanation
"""


fellows = models.Claimant.objects.filter(fellow=True)
fellows

"""
Explanation: To select all the fellows you can use
End of explanation
"""


from django_pandas.io import read_frame

fellows = read_frame(fellows.values())
fellows

"""
Explanation: Remember that the Claimant table can have entries that aren't fellows and because of it we need to use .filter(selected=True).
Basic (Pandas Part)
You can use Pandas with Django.
End of explanation
"""


expenses = read_frame(Expense.objects.all())
expenses

expenses.sum()

expenses["amount_authorized_for_payment"].sum()

"""
Explanation: When converting a Django QuerySet into a Pandas DataFrame you will need to as the previous example because so far Pandas can't process Django QuerySets by default.
End of explanation
"""


from base64 import b64encode

csv = fellows.to_csv(
    header=True,
    index=False
)

b64encode(csv.encode())

"""
Explanation: Pandas table as CSV and as Data URIs
For the report, we need to Pandas table as CSV encoded inside data URIs so users can download the CSV file without querying the server.
End of explanation
"""


funds = models.Fund.objects.all()
read_frame(funds)

"""
Explanation: The output of b64encode can be included in
&lt;a download="fellows.csv" href="data:application/octet-stream;charset=utf-16le;base64,{{ b64encode_output | safe }}"&gt;Download the data as CSV.&lt;/a&gt;
so that user can download the data.
Basic (Tagulous)
We use Tagulous as a tag library.
End of explanation
"""


funds[0].activity.all()

"""
Explanation: Get a list of all tags:
End of explanation
"""


for tag in funds[0].activity.all():
    print(tag.name)

"""
Explanation: You can loop over each tag:
End of explanation
"""


models.Fund.objects.filter(activity="ssi2/fellowship")

"""
Explanation: Filter for a specific tag:
End of explanation
"""


models.Fund.objects.filter(activity__name__contains="fellowship")

for fund in models.Fund.objects.filter(activity__name__contains="fellowship"):
    print("{} - {}".format(fund, fund.activity.all()))

"""
Explanation: You can query for part of the name of the tag:
End of explanation
"""


