add article and pubications
This commit is contained in:
BIN
server/models/publications/__pycache__/articles.cpython-313.pyc
Normal file
BIN
server/models/publications/__pycache__/articles.cpython-313.pyc
Normal file
Binary file not shown.
146
server/models/publications/articles.py
Normal file
146
server/models/publications/articles.py
Normal file
@@ -0,0 +1,146 @@
|
||||
import sqlite3
|
||||
from dataclasses import dataclass
|
||||
from typing import Optional, List
|
||||
|
||||
@dataclass
|
||||
class ArticleModel:
|
||||
id: Optional[int] = None
|
||||
title: Optional[str] = None
|
||||
content: Optional[str] = None
|
||||
author_id: Optional[int] = None
|
||||
author_name: Optional[str] = None # To hold joint first_name + last_name
|
||||
created_at: Optional[str] = None
|
||||
|
||||
class Articles:
|
||||
def __init__(self, db_path="instance/app_database.db"):
|
||||
self.db_path = db_path
|
||||
self._create_articles_table()
|
||||
|
||||
def _create_articles_table(self):
|
||||
with sqlite3.connect(self.db_path) as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(
|
||||
"""
|
||||
CREATE TABLE IF NOT EXISTS articles (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
title TEXT NOT NULL,
|
||||
content TEXT NOT NULL,
|
||||
author_id INTEGER NOT NULL,
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (author_id) REFERENCES users (id)
|
||||
);
|
||||
"""
|
||||
)
|
||||
conn.commit()
|
||||
|
||||
def add_article(self, title: str, content: str, author_id: int) -> Optional[int]:
|
||||
"""Insert a new article and return its ID."""
|
||||
try:
|
||||
with sqlite3.connect(self.db_path) as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(
|
||||
"""
|
||||
INSERT INTO articles (title, content, author_id)
|
||||
VALUES (?, ?, ?)
|
||||
""",
|
||||
(title, content, author_id),
|
||||
)
|
||||
conn.commit()
|
||||
return cursor.lastrowid
|
||||
except sqlite3.Error as e:
|
||||
print(f"Error adding article: {e}")
|
||||
return None
|
||||
|
||||
def get_article(self, article_id: int) -> Optional[ArticleModel]:
|
||||
"""Fetch a single article by ID, joining users to get the author's name."""
|
||||
try:
|
||||
with sqlite3.connect(self.db_path) as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(
|
||||
"""
|
||||
SELECT a.id, a.title, a.content, a.author_id, u.first_name, u.last_name, a.created_at
|
||||
FROM articles a
|
||||
JOIN users u ON a.author_id = u.id
|
||||
WHERE a.id = ?
|
||||
""",
|
||||
(article_id,),
|
||||
)
|
||||
row = cursor.fetchone()
|
||||
if not row:
|
||||
return None
|
||||
|
||||
author_name = f"{row[4] or ''} {row[5] or ''}".strip() or "Autor necunoscut"
|
||||
return ArticleModel(
|
||||
id=row[0],
|
||||
title=row[1],
|
||||
content=row[2],
|
||||
author_id=row[3],
|
||||
author_name=author_name,
|
||||
created_at=row[6],
|
||||
)
|
||||
except sqlite3.Error as e:
|
||||
print(f"Error getting article: {e}")
|
||||
return None
|
||||
|
||||
def get_all_articles(self) -> List[ArticleModel]:
|
||||
"""Fetch all articles ordered by created_at DESC (newest first)."""
|
||||
try:
|
||||
with sqlite3.connect(self.db_path) as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(
|
||||
"""
|
||||
SELECT a.id, a.title, a.content, a.author_id, u.first_name, u.last_name, a.created_at
|
||||
FROM articles a
|
||||
JOIN users u ON a.author_id = u.id
|
||||
ORDER BY a.created_at DESC
|
||||
"""
|
||||
)
|
||||
rows = cursor.fetchall()
|
||||
articles = []
|
||||
for row in rows:
|
||||
author_name = f"{row[4] or ''} {row[5] or ''}".strip() or "Autor necunoscut"
|
||||
articles.append(
|
||||
ArticleModel(
|
||||
id=row[0],
|
||||
title=row[1],
|
||||
content=row[2],
|
||||
author_id=row[3],
|
||||
author_name=author_name,
|
||||
created_at=row[6],
|
||||
)
|
||||
)
|
||||
return articles
|
||||
except sqlite3.Error as e:
|
||||
print(f"Error fetching all articles: {e}")
|
||||
return []
|
||||
|
||||
def update_article(self, article_id: int, title: str, content: str) -> bool:
|
||||
"""Update the title and content of an article."""
|
||||
try:
|
||||
with sqlite3.connect(self.db_path) as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(
|
||||
"""
|
||||
UPDATE articles
|
||||
SET title = ?, content = ?
|
||||
WHERE id = ?
|
||||
""",
|
||||
(title, content, article_id),
|
||||
)
|
||||
conn.commit()
|
||||
return cursor.rowcount > 0
|
||||
except sqlite3.Error as e:
|
||||
print(f"Error updating article: {e}")
|
||||
return False
|
||||
|
||||
def delete_article(self, article_id: int) -> bool:
|
||||
"""Delete an article by ID."""
|
||||
try:
|
||||
with sqlite3.connect(self.db_path) as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("DELETE FROM articles WHERE id = ?", (article_id,))
|
||||
conn.commit()
|
||||
return cursor.rowcount > 0
|
||||
except sqlite3.Error as e:
|
||||
print(f"Error deleting article: {e}")
|
||||
return False
|
||||
@@ -20,6 +20,7 @@ class UserModel:
|
||||
otp_code: Optional[str] = None
|
||||
otp_expiration: Optional[str] = None
|
||||
active: Optional[int] = None
|
||||
can_create_articles: Optional[int] = 0
|
||||
|
||||
class Users:
|
||||
def __init__(self, db_path="instance/app_database.db"):
|
||||
@@ -46,10 +47,15 @@ class Users:
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
otp_code TEXT,
|
||||
otp_expiration TIMESTAMPTZ,
|
||||
active INTEGER DEFAULT 1
|
||||
active INTEGER DEFAULT 1,
|
||||
can_create_articles INTEGER DEFAULT 0
|
||||
);
|
||||
"""
|
||||
)
|
||||
try:
|
||||
cursor.execute("ALTER TABLE users ADD COLUMN can_create_articles INTEGER DEFAULT 0;")
|
||||
except sqlite3.OperationalError:
|
||||
pass
|
||||
conn.commit()
|
||||
|
||||
def update_user_otp(self, user_id, otp_code, expiration):
|
||||
@@ -117,7 +123,8 @@ class Users:
|
||||
created_at=row[11],
|
||||
otp_code=row[12],
|
||||
otp_expiration=row[13],
|
||||
active=row[14]
|
||||
active=row[14],
|
||||
can_create_articles=row[15] if len(row) > 15 else 0
|
||||
)
|
||||
|
||||
def register_user(self, email, password, workspace_id):
|
||||
@@ -141,10 +148,10 @@ class Users:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(
|
||||
"""
|
||||
INSERT INTO users (workspace_id, first_name, last_name, email, password, address, profession, role, status, profile_pic)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
INSERT INTO users (workspace_id, first_name, last_name, email, password, address, profession, role, status, profile_pic, can_create_articles)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
""",
|
||||
(user.workspace_id, user.first_name, user.last_name, user.email, user.password, user.address, user.profession, user.role, user.status, user.profile_pic),
|
||||
(user.workspace_id, user.first_name, user.last_name, user.email, user.password, user.address, user.profession, user.role, user.status, user.profile_pic, user.can_create_articles or 0),
|
||||
)
|
||||
conn.commit()
|
||||
return cursor.lastrowid
|
||||
@@ -174,7 +181,8 @@ class Users:
|
||||
created_at=row[11],
|
||||
otp_code=row[12],
|
||||
otp_expiration=row[13],
|
||||
active=row[14]
|
||||
active=row[14],
|
||||
can_create_articles=row[15] if len(row) > 15 else 0
|
||||
)
|
||||
|
||||
def get_user_by_email(self, email: str) -> UserModel | None:
|
||||
@@ -201,7 +209,8 @@ class Users:
|
||||
created_at=row[11],
|
||||
otp_code=row[12],
|
||||
otp_expiration=row[13],
|
||||
active=row[14]
|
||||
active=row[14],
|
||||
can_create_articles=row[15] if len(row) > 15 else 0
|
||||
)
|
||||
|
||||
def get_users_by_workspace_id(self, workspace_id):
|
||||
@@ -225,7 +234,8 @@ class Users:
|
||||
created_at=row[11],
|
||||
otp_code=row[12],
|
||||
otp_expiration=row[13],
|
||||
active=row[14]
|
||||
active=row[14],
|
||||
can_create_articles=row[15] if len(row) > 15 else 0
|
||||
)
|
||||
for row in rows
|
||||
]
|
||||
@@ -251,13 +261,14 @@ class Users:
|
||||
created_at=row[11],
|
||||
otp_code=row[12],
|
||||
otp_expiration=row[13],
|
||||
active=row[14]
|
||||
active=row[14],
|
||||
can_create_articles=row[15] if len(row) > 15 else 0
|
||||
)
|
||||
for row in rows
|
||||
]
|
||||
|
||||
def update_user(self, user_id, first_name=None, last_name=None, email=None, password = None, address = None, profession = None, role = None, status = None, profile_pic=None, active=None):
|
||||
if first_name is None and last_name is None and email is None and password is None and address is None and profession is None and role is None and status is None and profile_pic is None and active is None:
|
||||
def update_user(self, user_id, first_name=None, last_name=None, email=None, password = None, address = None, profession = None, role = None, status = None, profile_pic=None, active=None, can_create_articles=None):
|
||||
if first_name is None and last_name is None and email is None and password is None and address is None and profession is None and role is None and status is None and profile_pic is None and active is None and can_create_articles is None:
|
||||
return False
|
||||
|
||||
fields = []
|
||||
@@ -293,6 +304,9 @@ class Users:
|
||||
if active is not None:
|
||||
fields.append("active = ?")
|
||||
params.append(active)
|
||||
if can_create_articles is not None:
|
||||
fields.append("can_create_articles = ?")
|
||||
params.append(can_create_articles)
|
||||
|
||||
params.append(user_id)
|
||||
query = f"UPDATE users SET {', '.join(fields)} WHERE id = ?"
|
||||
|
||||
Reference in New Issue
Block a user