Sfoglia il codice sorgente

Merge branch 'add-csv-export' of tati/voices-of-merseyside into master

tati 6 anni fa
parent
commit
39d30b8a6e
3 ha cambiato i file con 65 aggiunte e 2 eliminazioni
  1. 17 1
      backend/backend/api.py
  2. 4 1
      backend/backend/app.py
  3. 44 0
      backend/backend/voices_to_csv.py

+ 17 - 1
backend/backend/api.py

@@ -1,8 +1,12 @@
-from flask import request, make_response
+import csv
+import os
+
+from flask import request, make_response, send_from_directory
 from flask_restful import Resource
 
 from backend.database import db
 from backend.models import PayloadModel
+from backend.voices_to_csv import headers, map_entry_to_rows
 
 
 class Payload(Resource):
@@ -23,3 +27,15 @@ class Payload(Resource):
         db.session.commit()
 
         return make_response('', 201)
+
+
+class CSVExport(Resource):
+    def get(self):
+        fname = 'voices-of-merseyside.csv'
+        with open(fname, 'w') as csvfile:
+            writer = csv.writer(csvfile, delimiter=',')
+            writer.writerow(headers)
+            for entry in PayloadModel.query.order_by("id"):
+                for subentry in map_entry_to_rows(entry.data, entry.id):
+                    writer.writerow(subentry)
+        return send_from_directory(os.getcwd(), fname, as_attachment=True)

+ 4 - 1
backend/backend/app.py

@@ -1,11 +1,13 @@
+import csv
 import os
 
 from pathlib import Path
 
 import waitress
 
-from backend.api import Payload
+from backend.api import Payload, CSVExport
 from backend.database import db
+from backend.voices_to_csv import map_entry_to_rows, headers
 
 from flask import Flask
 from flask_restful import Api
@@ -19,6 +21,7 @@ def create_app():
 
     api = Api(app)
     api.add_resource(Payload, '/')
+    api.add_resource(CSVExport, '/csv')
 
     if not Path(DB_PATH).exists():
         with app.app_context():

+ 44 - 0
backend/backend/voices_to_csv.py

@@ -0,0 +1,44 @@
+headers = ['id', 'shape id', 'age', 'gender', 'education', 'birth place', 'current place', 'non native', 'area name', 'sound example', 'associations', 'correctness', 'friendliness', 'pleasantness', 'trustworthiness']
+EDUCATION = {
+        '1': 'High school or lower',
+        '2': 'Bachelors',
+        '3': 'Masters',
+        '4': 'Doctorate'
+        }
+NON_NATIVE = {
+        '1': 'Less than two years',
+        '2': '3-5 years',
+        '3': '6-10 years',
+        '4': '10+ years'
+        }
+AGE = {
+        '1': '16 - 17',
+        '2': '18 - 25',
+        '3': '26 - 45',
+        '4': '46 - 65',
+        '5': '66 - 75',
+        '6': '75+'
+        }
+
+def map_entry_to_rows(entry, _id):
+    pi = entry['personalInformation']
+    personalInformation = [
+            AGE[pi['age']],
+            pi['genderCustom'] if pi['gender'] == 'other' else pi['gender'],
+            ', '.join(list(map(lambda e: EDUCATION[e], pi['levelEducation']))),
+            pi['birthPlace'],
+            pi['currentPlace'],
+            NON_NATIVE.get(pi['nonNative'], '-'),
+            ]
+    for i, subentry in enumerate(entry['canvas']):
+        form = subentry['form']
+        ret = personalInformation + [
+                form['name'],
+                form['soundExample'] or '-',
+                ', '.join(form['associations']),
+                form['correctness'],
+                form['friendliness'],
+                form['pleasantness'],
+                form['trustworthiness']
+                ]
+        yield [_id, i] + ret