def put(self, articleID): # Get the parameters included in the request title = request.get_json().get('title').strip() text = request.get_json().get('text').strip() author = request.get_json().get('author').strip() # Initialize response article = None succeed = False errorMessage = '' try: # Update the article on DataBase article = DbController.instance().getOne(Article, articleID) article.title = title article.text = text article.author = author DbController.instance().commitDB() # Response parameters succeed = True except Exception as e: logging.error( "There was an issue modifying the article: ({0}) : {1}".format( e, traceback.format_exc())) # Response parameters errorMessage = 'There was an issue modifying the article' return dict(data=article, succeed=succeed, errorMessage=errorMessage)
def post(self): # Get the parameters included in the request title = request.get_json().get('title').strip() text = request.get_json().get('text').strip() author = request.get_json().get('author').strip() # Initialize response newArticle = None succeed = False errorMessage = '' try: # Create article in the database newArticle = Article(title=title, text=text, author=author) DbController.instance().add(newArticle) # Response parameters succeed = True except Exception as e: logging.error("There was an issue crating a new article: ({0}) : {1}" .format(e, traceback.format_exc())) # Response parameters errorMessage = 'There was an issue creating a new the article' return dict(data=newArticle, succeed=succeed, errorMessage=errorMessage)
def delete(self, articleID): # Initialize response succeed = False errorMessage = '' try: # Delete article from database DbController.instance().delete(Article, articleID) # Response parameters succeed = True except Exception as e: logging.error( "There was an issue deleting the article: ({0}) : {1}".format( e, traceback.format_exc())) # Response parameters errorMessage = 'There was an issue deleting the article' return dict(succeed=succeed, errorMessage=errorMessage)
class Article(DbController.instance().db.Model): __tablename__ = 'article' __table_args__ = Settings.instance().DATABASE_TABLE_ARGS # Table fields id = Column(Integer, primary_key=True, autoincrement=True) date = Column(DateTime, nullable=False, default=datetime.utcnow) title = Column(String, nullable=False) text = Column(String, nullable=False) author = Column(String, nullable=False) # Relationships @staticmethod def getMainField(): return Article.title def __repr__(self): return '<Article: {0}>'.format(self.id)
def get(self): # Initialize response articles = [] numResults = 0 succeed = False errorMessage = '' try: # Get articles from database articles, numResults = DbController.instance().getAll(Article) # Response parameters succeed = True except Exception as e: logging.error( "There was an issue retrieving the list of articles: ({0}) : {1}".format(e, traceback.format_exc())) # Response parameters errorMessage = 'There was an issue retrieving the list of articles' return dict(data=articles, numResults=numResults, succeed=succeed, errorMessage=errorMessage)
def run(self, flaskHost, flaskPort): if not Settings.instance().FLASK_DEBUG: url = "http://{0}:{1}".format(flaskHost, flaskPort) print( "The Flask application is running on {0} (Press CTRL+C to quit)" .format(url)) # DataBase DbController.instance().initApp( self.flask.app) # Initialize DB on the application if Settings.instance().DATABASE_REBUILT: DbController.instance().createTables(self.flask.app) if Settings.instance().DATABASE_TEST_DATA: DbController.instance().createTestData( self.flask.app) # Add test data into de DataBase # Flask self.flask.app.run(flaskHost, port=flaskPort, threaded=True)