Advertisement
Guest User

Untitled

a guest
Jul 17th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.78 KB | None | 0 0
  1. """
  2. Script to set news_term on organisations within a CSV file.
  3.  
  4. CSV format:
  5.    name,news_term
  6. """
  7. import csv
  8.  
  9. import click
  10.  
  11. from vizibl.helpers.factory import create_app
  12. from vizibl.models import db, Organisation
  13.  
  14.  
  15. def get_organisation(name):
  16.     org = db.session.query(Organisation).filter(Organisation.name == name)
  17.     return org.one_or_none()
  18.  
  19.  
  20. @click.command()
  21. @click.argument('input', type=click.Path(exists=True))
  22. def run(input):
  23.     with open(input, 'r') as csvfile:
  24.         _csv = csv.reader(csvfile)
  25.         for row in _csv:
  26.             org = get_organisation(row[0])
  27.             if org:
  28.                 org.news_term = row[1]
  29.         db.session.commit()
  30.  
  31. if __name__ == '__main__':
  32.     app = create_app()
  33.     with app.test_request_context():
  34.         run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement