def restore(): DB.drop() with open('data/portfolios.json', 'r') as f: portfolios = json.load(f) for portfolio in portfolios: try: Portfolio.new(portfolio) except Exception as e: return Result(success=False, message=str(e), severity='ERROR') with open('data/trades.json', 'r') as f: trades = json.load(f) trades.sort(key=lambda i: (i['port'], i['trade']['TradeDate'])) [TradeLog.commit_raw_trade(Raw.new(trade['trade']._id), trade['port']) for trade in trades] with open('data/raw.json', 'r') as f: raw = json.load(f) [Raw.new(r) for r in raw] return Result(success=True, message=f'Successfully restored {len(portfolios)} portfolios, {len(trades)} trades and \ {len(raw)} raw trades', severity='SUCCESS')
def read(cls, query, many=False): if many: return [ cls(record) for record in DB.read_many(cls.collection, query) ] else: record = (DB.read_one(cls.collection, query)) if record: return cls(record) return None
def delete(self): # return DB.delete(self.collection, {"_id": ObjectId(self._id)}) try: self._id = Object(self._id) except: pass return DB.delete(self.collection, {"_id": self._id})
def get(cls, _id): try: _id = ObjectId(_id) except Exception: pass # result = DB.read_one(cls.collection, {'_id': ObjectId(_id)}) result = DB.read_one(cls.collection, {'_id': _id}) if result: return cls(result) return None
@login_manager.user_loader def load_user(user_id): return User.get(user_id) app.config['DB_URL'] = environ.get('DB_URL') app.config['SECRET_KEY'] = environ.get('SECRET_KEY') or urandom(24) app.config['GOOGLE_CLIENT_ID'] = environ.get('GOOGLE_CLIENT_ID') app.config['GOOGLE_CLIENT_SECRET'] = environ.get('GOOGLE_CLIENT_SECRET') app.config['GOOGLE_DISCOVERY_URL'] = environ.get('GOOGLE_DISCOVERY_URL') app.config['ADMIN'] = environ.get('ADMIN') try: print('DB -> ', app.config['DB_URL']) DB.connect(app.config['DB_URL'], app.config['ENV']) except Exception as e: print('Failed to connect to database\n', e) sys.exit() else: print(f" * Succesfully connected to the {app.config['ENV']} database *") @app.template_filter('ftime') def _format_date(date): try: return datetime.strftime(date, "%d/%m/%Y") except: return " "
def setUpClass(cls): DB.connect(testing.DB_URL, db) DB.drop(db) print(f'\nSetting up test suite by deleting {db} database\n')
def create(self): """ Save a copy of the class in the DB, add the _id and return the object """ DB.create(self.collection, vars(self)) return self
def update(self, values=None): if not values: values = vars(self) # return DB.update(self.collection, # {"_id": ObjectId(self._id)}, {"$set": values}) return DB.update(self.collection, {"_id": self._id}, {"$set": values})