logic.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import os
  2. from flask import abort
  3. from nova import app, db, models
  4. def create_collection(name, user, description=None):
  5. collection = models.Collection(name=name, user=user, description=description)
  6. db.session.add(collection)
  7. db.session.commit()
  8. return collection
  9. def create_dataset(dtype, name, user, collection, **kwargs):
  10. # TODO: merge functionality with import_dataset
  11. root = app.config['NOVA_ROOT_PATH']
  12. path = os.path.join(root, user.name, collection.name, name)
  13. dataset = dtype(name=name, path=path, collection=collection, **kwargs)
  14. abspath = os.path.join(root, path)
  15. os.makedirs(abspath)
  16. access = models.Access(user=user, dataset=dataset, owner=True, writable=True, seen=True)
  17. db.session.add_all([dataset, access])
  18. db.session.commit()
  19. return dataset
  20. def import_sample_scan(name, user, path, description=None):
  21. collection = models.Collection(user=user, name=name, description=description)
  22. dataset = models.SampleScan(name=name, path=path, collection=collection,
  23. genus=None, family=None, order=None)
  24. access = models.Access(user=user, dataset=dataset, owner=True, writable=True, seen=True)
  25. db.session.add_all([collection, dataset, access])
  26. db.session.commit()
  27. return dataset
  28. def get_user(token):
  29. uid = int(token.split('.')[0])
  30. return db.session.query(models.User).filter(models.User.id == uid).first()
  31. def check_token(token):
  32. uid, signature = token.split('.')
  33. user = get_user(token)
  34. if not user.is_token_valid(token):
  35. abort(401)
  36. return user