Parts Implemented by Emre EROĞLU¶
Database Initialization¶
Initialization operation is executing initialDB.sql file and committing it to database.
This class helps initialization of the database. Parameters: initiateDatabase takes app as parameter. It gives us to connection information.
import psycopg2
import sys
import os
class DataBaseSetup:
def initiateDataBase(self, app):
try:
connection = psycopg2.connect(app.config['dsn'])
#read sqlFile commands from SQLFile
with open("initialDB.sql") as SQLFile:
sqlFile = SQLFile.readlines()
cursor = connection.cursor()
#execute commands readed
for transaction in sqlFile:
if transaction != '\n':
cursor.execute(transaction)
connection.commit()
print("Database initialized.")
connection.close()
except:
print("Database could not initialized. Err : ")
print(sys.exc_info()[0])
def initiateDB():
**Calls initialize function from DataBaseSetup class.**
**Sends example data to browser.**
Login Management¶
flask-login appliacation is required for session management.
Database Design For Users¶
CREATE DOMAIN AUTH VARCHAR(10) CHECK (VALUE IN (‘u’, ‘m’, ‘a’));
CREATE TABLE USERS(ID SERIAL PRIMARY KEY, NAME TEXT, AGE DATE, EMAIL CHAR(50), PASSWORD CHAR(32), AUTH AUTH, COUNTRY_ID INT);
There is 7 columns in USERS table, it has a primary key named id and it’s type is SERIAL.
EMAIL is determiner of uniqueness of user. It checked in code. AUTH is shows user authentication levels. PASSWORDS are stored as MD5 hashes in database. AGE column was INT value I did not changed it when I made it DATE.
Has foreign key with countries.
Code Design For Users¶
User Managements consist of 2 html files.
AnonymousUser class is extended from Flasks AnonymousUserMixin class.
- class AnonymousUser(AnonymousUserMixin):
- pass
User class is extended from Flasks UserMixin class.
class User(UserMixin):
News Management¶
Database Design For News¶
CREATE TABLE NEWS(ID SERIAL PRIMARY KEY, TITLE CHAR(50), CONTENT TEXT, USER_ID INT);
It has PRIMARY KEY ID as SERIAL type.
It has foreign key with users.
Code Design For News¶
News consists of 2 html files.
- def newsManagement():
- Opens News Management Page. -> newsManagement.html This function is login required. You cannot use it without authorized user. Lists user list on the botton of page. This function can get GET and POST data, if it gets search variable as POST data it lists only search results. Normal users cannot manage News, Moderators can edit and add own News and Administrators manages all of News.
- def addNews():
- Validates and inserts news to database.
- def newsUpdate():
- Updates news on database.
- def updateNews():
- Fills to manage page for update.
- def deleteNews():
- Deletes news from database.
- def news():
- News read page. Every user can see.