forked from Sanqui/prekapavac
-
Notifications
You must be signed in to change notification settings - Fork 0
/
references.py
49 lines (40 loc) · 1.69 KB
/
references.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from sys import argv
import csv
from tqdm import tqdm
import sys
import app
db = app.db
with app.app.app_context():
def export():
f = open('references.csv', 'w')
writer = csv.writer(f)
terms = db.session.query(db.Term).join(db.Category).join(db.Project).filter( \
db.Term.hidden == False, \
db.Category.hidden == False, \
).order_by(db.Term.id)
term_count = terms.count()
writer.writerow("term1_id,term0_id,term1_identifier,term0_identifier,term1,term0,valid".split(","))
for term1 in tqdm(terms, total=term_count):
for term0 in term1.potentially_referenced:
if not term1.text_en.strip(): continue
if not term0.text_en.strip(): continue
writer.writerow((term1.id, term0.id,
str(term1), str(term0),
term1.text_en, term0.text_en, 1))
f.close()
def import_():
f = open('references.csv', 'r')
reader = csv.reader(f)
next(reader)
for row in tqdm(reader):
if not len(row): continue
term1_id, term0_id, \
term1_identifier, term0_identifier,\
term1_text, term0_text, valid = row
existing_refs = db.session.query(db.Reference).filter_by(term0_id=term0_id, term1_id=term1_id)
for existing_ref in existing_refs:
db.session.delete(existing_ref)
reference = db.Reference(term0_id=term0_id, term1_id=term1_id, valid=int(valid))
db.session.add(reference)
db.session.commit()
{"export": export, "import": import_}[sys.argv[1]]()