Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export all survey tables to CSV #426

Merged
merged 7 commits into from
Jan 24, 2019
Merged

Export all survey tables to CSV #426

merged 7 commits into from
Jan 24, 2019

Conversation

fungjj92
Copy link
Contributor

@fungjj92 fungjj92 commented Jan 18, 2019

Overview

Site admins want the data as CSV to do analyses. This PR adds a view at http://localhost:8000/beekeepers/export for admins to download tables as a zip of CSVs. SQL queries are hardcoded.

Connects #384
Connects #385

Demo

The idea: (i fussed with the filename timestamp a little since this screenshot)
screen shot 2019-01-22 at 11 13 20 am

The date formatter doesn't allow colons in the time (only -). It's livable. It's main purpose is to give the files a unique ID.
screen shot 2019-01-22 at 11 25 34 am
Non-admins can't access:
screen shot 2019-01-22 at 11 14 42 am
screen shot 2019-01-22 at 3 14 22 pm

Testing Instructions

Visit the URL and sign in as an admin. Check that the zip downloads and the CSVs look good.

@rajadain
Copy link
Contributor

This also needs a management command, so it can run like this:

$ ./scripts/manage.sh beekeepers_export
+ ARGS=beekeepers_export
+ [[ 1 -eq 1 ]]
+ [[ beekeepers_export == runserver ]]
+ vagrant ssh app -c 'cd /opt/app && envdir /etc/icp.d/env ./manage.py beekeepers_export'
Export Complete
Connection to 127.0.0.1 closed.

I added a commit for that (since it was easier than writing it out in a comment).

Currently any new export overwrites the old files. Can we make it so that the exports are zipped into an archive with a timestamp, so that later exports do not overwrite the old ones?

@rajadain
Copy link
Contributor

Can we have the CSV export quote string and date fields? The current export looks like this:

id,inspection_date,colony_name,colony_alive,colony_loss_reason,num_bodies_supers_deep,num_bodies_supers_medium,num_bodies_supers_shallow,activity_since_last,queenright,same_queen,queen_stock,queen_source,varroa_count_performed,varroa_count_technique,varroa_count_result,varroa_treatment,survey_id
1,2018-10-09,A,t, ,0,0,3, ,f,YES, ,NON_LOCAL_COMMERCIAL,f, ,10, ,5
2,2018-11-12,A,t, ,0,0,3, ,f,YES, ,NON_LOCAL_COMMERCIAL,f, ,10, ,8
3,2018-12-27,A,t, ,0,0,3, ,f,YES, ,NON_LOCAL_COMMERCIAL,f, ,10,CHEMICAL_FORMIC_ACID_FORMIC_PRO;CHEMICAL_OXALIC_ACID_DRIBBLE;CHEMICAL_THYMOL_MENTHOL_APILIFE;CHEMICAL_SYNTHETIC_CHECKMITE_PLUS;MECHANICAL_QUEEN_MANIPULATION;OTHER-heat vision;CHEMICAL_ORGANIC_OTHER-freeze breath;MECHANICAL_OTHER-punching,7
4,2019-01-14,A,f,INADEQUETE_FOOD_STORES;COLONY_TOO_SMALL_IN_NOVEMBER,0,2,0,REMOVED_BROOD,f,YES,"",NON_LOCAL_COMMERCIAL,f,"",0,CHEMICAL_OXALIC_ACID_DRIBBLE;CHEMICAL_SYNTHETIC_CHECKMITE_PLUS;CHEMICAL_ORGANIC_OTHER-asdsadadas,21
5,2019-01-15,A,t,"",23,0,0,FED_POLLEN_PROTEIN,f,YES,"",NON_LOCAL_COMMERCIAL,f,"",0,CHEMICAL_OXALIC_ACID_VAPORIZATION;CHEMICAL_SYNTHETIC_APIVAR;CHEMICAL_ORGANIC_OTHER-asdsadadas,22
6,2019-01-16,A,t,"",0,0,0,"",f,YES,"",REQUEENED,f,"",0,CHEMICAL_THYMOL_MENTHOL_APILIFE;CHEMICAL_SYNTHETIC_APISTAN;MECHANICAL_QUEEN_MANIPULATION;CHEMICAL_ORGANIC_OTHER-asdsadadas,25

whereas it should look like this:

"id","inspection_date","colony_name","colony_alive","colony_loss_reason","num_bodies_supers_deep","num_bodies_supers_medium","num_bodies_supers_shallow","activity_since_last","queenright","same_queen","queen_stock","queen_source","varroa_count_performed","varroa_count_technique","varroa_count_result","varroa_treatment","survey_id"
1,"2018-10-09","A",TRUE," ",0,0,3," ",FALSE,"YES"," ","NON_LOCAL_COMMERCIAL",FALSE," ",10," ",5
2,"2018-11-12","A",TRUE," ",0,0,3," ",FALSE,"YES"," ","NON_LOCAL_COMMERCIAL",FALSE," ",10," ",8
3,"2018-12-27","A",TRUE," ",0,0,3," ",FALSE,"YES"," ","NON_LOCAL_COMMERCIAL",FALSE," ",10,"CHEMICAL_FORMIC_ACID_FORMIC_PRO;CHEMICAL_OXALIC_ACID_DRIBBLE;CHEMICAL_THYMOL_MENTHOL_APILIFE;CHEMICAL_SYNTHETIC_CHECKMITE_PLUS;MECHANICAL_QUEEN_MANIPULATION;OTHER-heat vision;CHEMICAL_ORGANIC_OTHER-freeze breath;MECHANICAL_OTHER-punching",7
4,"2019-01-14","A",FALSE,"INADEQUETE_FOOD_STORES;COLONY_TOO_SMALL_IN_NOVEMBER",0,2,0,"REMOVED_BROOD",FALSE,"YES","","NON_LOCAL_COMMERCIAL",FALSE,"",0,"CHEMICAL_OXALIC_ACID_DRIBBLE;CHEMICAL_SYNTHETIC_CHECKMITE_PLUS;CHEMICAL_ORGANIC_OTHER-asdsadadas",21
5,"2019-01-15","A",TRUE,"",23,0,0,"FED_POLLEN_PROTEIN",FALSE,"YES","","NON_LOCAL_COMMERCIAL",FALSE,"",0,"CHEMICAL_OXALIC_ACID_VAPORIZATION;CHEMICAL_SYNTHETIC_APIVAR;CHEMICAL_ORGANIC_OTHER-asdsadadas",22
6,"2019-01-16","A",TRUE,"",0,0,0,"",FALSE,"YES","","REQUEENED",FALSE,"",0,"CHEMICAL_THYMOL_MENTHOL_APILIFE;CHEMICAL_SYNTHETIC_APISTAN;MECHANICAL_QUEEN_MANIPULATION;CHEMICAL_ORGANIC_OTHER-asdsadadas",25

That'll protect from any commas in the content (which may be in the free-entry fields).

@fungjj92
Copy link
Contributor Author

fungjj92 commented Jan 22, 2019

The good news is that postgres or psycopg must add quotes when appropriate. Here is the output of april surveys when a string entry contains commas (last row).

survey_id,colony_loss_reason
55,varroa mites
60,varroa mites
63,INADEQUETE_FOOD_STORES;OTHER-super sad bee killer
64,VARROA_MITES;INADEQUETE_FOOD_STORES;OTHER-super sad bee killer
65,VARROA_MITES;OTHER-super sad bee killer2
66,COLONY_TOO_SMALL;PESTICIDE_EXPOSURE;OTHER-
67,VARROA_MITES;POOR_WEATHER_CONDITIONS;OTHER-
69,"VARROA_MITES;OTHER-super, sad, bee, killer"

@rajadain
Copy link
Contributor

Heads up that there's a merge conflict now.

Saves separate CSVs to the 'exports' folder

Refs #384
The CSVs are created in-memory and returned to the user so there are no artifacts left on the server. We don't expect the database to get large enough to warrant memory issues.

Refs #384
The task suggested adding this functionality to Django's admin interface. The admin view is however not easily customizable. Downloading the zip file from an endpoint was more possible and straightforward and is pretty flexible.

Refs #385
@fungjj92
Copy link
Contributor Author

Ready for a look

@rajadain
Copy link
Contributor

Taking a look now.

Copy link
Contributor

@rajadain rajadain left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 tested, this works well. Export had the right columns and consistent timestamp. With a few cleanup commits this should be ready.

src/icp/apps/beekeepers/views.py Outdated Show resolved Hide resolved
src/icp/apps/beekeepers/views.py Outdated Show resolved Hide resolved
src/icp/apps/beekeepers/views.py Outdated Show resolved Hide resolved
src/icp/apps/beekeepers/views.py Outdated Show resolved Hide resolved
src/icp/apps/beekeepers/views.py Outdated Show resolved Hide resolved
src/icp/apps/beekeepers/views.py Outdated Show resolved Hide resolved
src/icp/apps/beekeepers/views.py Show resolved Hide resolved
@fungjj92
Copy link
Contributor Author

Thanks! Helpful clean up.

Copy link
Contributor

@rajadain rajadain left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still needs a couple of fixes before it's green.

src/icp/apps/beekeepers/views.py Show resolved Hide resolved
src/icp/apps/beekeepers/views.py Outdated Show resolved Hide resolved
@fungjj92 fungjj92 force-pushed the jf/export-csv-fxn branch 2 times, most recently from 289d04c to fd5caec Compare January 23, 2019 21:16
It is better UX to have CSV download access via the front-end than to log into the django admin or remember a URL.

Refs #385
@fungjj92
Copy link
Contributor Author

Non-staff user:
screen shot 2019-01-23 at 1 50 04 pm

The button on web and on a screen reader:
screen shot 2019-01-23 at 4 12 35 pm
screen shot 2019-01-23 at 4 13 14 pm

@rajadain
Copy link
Contributor

Taking another look.

Copy link
Contributor

@rajadain rajadain left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 tested. Good work!

@rajadain rajadain assigned fungjj92 and unassigned rajadain Jan 24, 2019
@fungjj92
Copy link
Contributor Author

😅 Thanks for the reviews.

@fungjj92 fungjj92 merged commit 6204798 into develop Jan 24, 2019
@fungjj92 fungjj92 deleted the jf/export-csv-fxn branch January 24, 2019 17:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants