refactor: refactor the model
							parent
							
								
									82896de336
								
							
						
					
					
						commit
						3e35679a91
					
				|  | @ -1,7 +1,7 @@ | |||
| from pony.orm import * | ||||
| 
 | ||||
| db = Database() | ||||
| db.bind("sqlite", "./static/wordfreqapp.db", create_db=True)  # bind sqlite file | ||||
| db.bind("sqlite", "../static/wordfreqapp.db", create_db=True)  # bind sqlite file | ||||
| 
 | ||||
| 
 | ||||
| class User(db.Entity): | ||||
|  | @ -0,0 +1,34 @@ | |||
| from model import * | ||||
| from datetime import datetime | ||||
| 
 | ||||
| def add_article(content, source="manual_input", level="5", question="No question"): | ||||
|     with db_session: | ||||
|         # add one article to sqlite | ||||
|         Article( | ||||
|             text=content, | ||||
|             source=source, | ||||
|             date=datetime.now().strftime("%-d %b %Y"),  # format style of `5 Oct 2022` | ||||
|             level=level, | ||||
|             question=question, | ||||
|         ) | ||||
| 
 | ||||
| 
 | ||||
| def delete_article_by_id(article_id): | ||||
|     article_id &= 0xFFFFFFFF  # max 32 bits | ||||
|     with db_session: | ||||
|         article = Article.select(article_id=article_id) | ||||
|         if article: | ||||
|             article.first().delete() | ||||
| 
 | ||||
| 
 | ||||
| def get_number_of_articles(): | ||||
|     with db_session: | ||||
|         return len(Article.select()[:]) | ||||
| 
 | ||||
| 
 | ||||
| def get_page_articles(num, size): | ||||
|     with db_session: | ||||
|         return [ | ||||
|             x | ||||
|             for x in Article.select().order_by(desc(Article.article_id)).page(num, size) | ||||
|         ] | ||||
|  | @ -0,0 +1,19 @@ | |||
| from model import * | ||||
| from Login import md5 | ||||
| 
 | ||||
| def get_users(): | ||||
|     with db_session: | ||||
|         return User.select().order_by(User.name)[:] | ||||
| 
 | ||||
| 
 | ||||
| def update_password_by_username(username, password="123456"): | ||||
|     with db_session: | ||||
|         user = User.select(name=username) | ||||
|         if user: | ||||
|             user.first().password = md5(username + password) | ||||
| 
 | ||||
| def update_expiry_time_by_username(username, expiry_time="20230323"): | ||||
|     with db_session: | ||||
|         user = User.select(name=username) | ||||
|         if user: | ||||
|             user.first().expiry_date = expiry_time | ||||
		Loading…
	
		Reference in New Issue