-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
151 lines (135 loc) · 4.94 KB
/
Copy pathdatabase.py
File metadata and controls
151 lines (135 loc) · 4.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import sqlite3
import os
from flask import g
from werkzeug.security import generate_password_hash
DATABASE = os.path.join(os.path.dirname(__file__), 'placement.db')
def ensure_column(db, table, column, definition):
columns = {row['name'] for row in db.execute(f'PRAGMA table_info({table})').fetchall()}
if column not in columns:
db.execute(f'ALTER TABLE {table} ADD COLUMN {column} {definition}')
def get_db():
if 'db' not in g:
g.db = sqlite3.connect(DATABASE)
g.db.row_factory = sqlite3.Row
g.db.execute('PRAGMA foreign_keys = ON')
return g.db
def init_db():
db = sqlite3.connect(DATABASE)
db.row_factory = sqlite3.Row
db.execute('PRAGMA foreign_keys = ON')
# admin
db.execute('''
CREATE TABLE IF NOT EXISTS admin (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT UNIQUE NOT NULL,
password TEXT NOT NULL,
email TEXT,
is_active INTEGER DEFAULT 1,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
''')
# company
db.execute('''
CREATE TABLE IF NOT EXISTS company (
id INTEGER PRIMARY KEY AUTOINCREMENT,
company_name TEXT NOT NULL,
email TEXT UNIQUE NOT NULL,
password TEXT NOT NULL,
hr_name TEXT,
hr_contact TEXT,
website TEXT,
industry TEXT,
description TEXT,
address TEXT,
approval_status TEXT DEFAULT 'pending',
is_active INTEGER DEFAULT 1,
is_blacklisted INTEGER DEFAULT 0,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
''')
# student
db.execute('''
CREATE TABLE IF NOT EXISTS student (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
email TEXT UNIQUE NOT NULL,
password TEXT NOT NULL,
roll_number TEXT UNIQUE,
phone TEXT,
department TEXT,
year_of_study INTEGER,
cgpa REAL,
skills TEXT,
resume_path TEXT,
address TEXT,
is_active INTEGER DEFAULT 1,
is_blacklisted INTEGER DEFAULT 0,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
''')
# placement drives
db.execute('''
CREATE TABLE IF NOT EXISTS placement_drive (
id INTEGER PRIMARY KEY AUTOINCREMENT,
company_id INTEGER NOT NULL,
job_title TEXT NOT NULL,
job_description TEXT,
required_skills TEXT,
experience_required TEXT,
package TEXT,
location TEXT,
job_type TEXT DEFAULT 'Full-time',
eligibility_criteria TEXT,
min_cgpa REAL DEFAULT 0.0,
eligible_departments TEXT,
application_deadline TEXT,
drive_date TEXT,
max_positions INTEGER DEFAULT 1,
status TEXT DEFAULT 'pending',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (company_id) REFERENCES company(id)
)
''')
ensure_column(db, 'placement_drive', 'required_skills', 'TEXT')
ensure_column(db, 'placement_drive', 'experience_required', 'TEXT')
# applications (unique constraint prevents double applying)
db.execute('''
CREATE TABLE IF NOT EXISTS application (
id INTEGER PRIMARY KEY AUTOINCREMENT,
student_id INTEGER NOT NULL,
drive_id INTEGER NOT NULL,
application_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
status TEXT DEFAULT 'applied',
remarks TEXT,
FOREIGN KEY (student_id) REFERENCES student(id),
FOREIGN KEY (drive_id) REFERENCES placement_drive(id),
UNIQUE(student_id, drive_id)
)
''')
db.execute('''
CREATE TABLE IF NOT EXISTS notification (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL,
user_role TEXT NOT NULL,
title TEXT NOT NULL,
message TEXT NOT NULL,
is_read INTEGER DEFAULT 0,
link TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
''')
ensure_column(db, 'notification', 'user_id', 'INTEGER')
ensure_column(db, 'notification', 'user_role', "TEXT DEFAULT 'student'")
ensure_column(db, 'notification', 'title', "TEXT DEFAULT 'Update'")
ensure_column(db, 'notification', 'link', 'TEXT')
db.execute('CREATE INDEX IF NOT EXISTS idx_notification_user ON notification(user_id, user_role)')
# seed admin if first run
existing = db.execute('SELECT id FROM admin WHERE username = ?', ('admin',)).fetchone()
if not existing:
hashed = generate_password_hash('admin123')
db.execute(
'INSERT INTO admin (username, password, email) VALUES (?, ?, ?)',
('admin', hashed, 'admin@placewise.com')
)
db.commit()
db.close()