-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
422 lines (350 loc) · 11.8 KB
/
app.py
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
import psycopg2
from flask import Flask, request, render_template_string, jsonify
from werkzeug.security import generate_password_hash, check_password_hash
import random
import os
app = Flask(__name__)
# Function to read the config from the 'config.env' file
def load_config():
config = {}
with open('config.env', 'r') as f:
for line in f.readlines():
if line.strip() and not line.startswith('#'):
key, value = line.strip().split('=', 1)
config[key] = value
return config
# Load configuration from config.env
config = load_config()
# PostgreSQL database connection
def get_db_connection():
conn = psycopg2.connect(
dbname=config.get('DB_NAME', 'rjb'),
user=config.get('DB_USER', 'quinn'),
password=config.get('DB_PASSWORD', 'nivenly'),
host=config.get('DB_HOST', 'localhost')
)
return conn
# Initialize the database
def init_db():
conn = get_db_connection()
cursor = conn.cursor()
# Creating the tables if not exist
cursor.execute('''
CREATE TABLE IF NOT EXISTS users (
id SERIAL PRIMARY KEY,
username VARCHAR(100) UNIQUE NOT NULL,
email VARCHAR(100) NOT NULL,
password VARCHAR(100) NOT NULL
);
CREATE TABLE IF NOT EXISTS skills (
id SERIAL PRIMARY KEY,
name VARCHAR(100) UNIQUE NOT NULL,
need INT DEFAULT 0,
have INT DEFAULT 0
);
CREATE TABLE IF NOT EXISTS user_skills (
user_id INT REFERENCES users(id) ON DELETE CASCADE,
skill_id INT REFERENCES skills(id) ON DELETE CASCADE,
PRIMARY KEY (user_id, skill_id)
);
CREATE TABLE IF NOT EXISTS projects (
id SERIAL PRIMARY KEY,
name VARCHAR(100) UNIQUE NOT NULL,
description TEXT
);
CREATE TABLE IF NOT EXISTS project_skills (
project_id INT REFERENCES projects(id) ON DELETE CASCADE,
skill_id INT REFERENCES skills(id) ON DELETE CASCADE,
PRIMARY KEY (project_id, skill_id)
);
''')
conn.commit()
conn.close()
# Populate 'have' column with user skills
def update_have_column():
conn = get_db_connection()
cursor = conn.cursor()
# Reset the 'have' column to 0 for all skills
cursor.execute('UPDATE skills SET have = 0')
# Count how many users have each skill and update the 'have' column
cursor.execute('''
INSERT INTO skills (name, have)
SELECT s.name, COUNT(us.user_id)
FROM user_skills us
JOIN skills s ON us.skill_id = s.id
GROUP BY s.name
ON CONFLICT (name) DO UPDATE SET have = EXCLUDED.have;
''')
conn.commit()
conn.close()
# Populate 'need' column based on project skills
def update_need_column():
conn = get_db_connection()
cursor = conn.cursor()
# Reset the 'need' column to 0 for all skills
cursor.execute('UPDATE skills SET need = 0')
# Count how many projects require each skill and update the 'need' column
cursor.execute('''
INSERT INTO skills (name, need)
SELECT s.name, COUNT(ps.project_id)
FROM project_skills ps
JOIN skills s ON ps.skill_id = s.id
GROUP BY s.name
ON CONFLICT (name) DO UPDATE SET need = EXCLUDED.need;
''')
conn.commit()
conn.close()
# Display all users and their skills at /skills
@app.route('/skills', methods=['GET'])
def show_skills():
conn = get_db_connection()
cursor = conn.cursor()
cursor.execute('SELECT users.id, users.username, skills.name FROM users '
'JOIN user_skills ON users.id = user_skills.user_id '
'JOIN skills ON user_skills.skill_id = skills.id')
user_skills = cursor.fetchall()
skills_dict = {}
for user_id, username, skill in user_skills:
if user_id not in skills_dict:
skills_dict[user_id] = {"username": username, "skills": []}
skills_dict[user_id]["skills"].append(skill)
cursor.execute('SELECT name, need, have FROM skills')
skills_table = cursor.fetchall()
# Fetch all projects
cursor.execute('SELECT p.name, p.description, s.name FROM projects p '
'JOIN project_skills ps ON p.id = ps.project_id '
'JOIN skills s ON ps.skill_id = s.id')
project_skills = cursor.fetchall()
# Organize project skills
project_dict = {}
for project_name, project_desc, skill_name in project_skills:
if project_name not in project_dict:
project_dict[project_name] = {"description": project_desc, "skills": []}
project_dict[project_name]["skills"].append(skill_name)
conn.close()
inline_css = """
<style>
body { font-family: Arial, sans-serif; background-color: #f4f4f4; padding: 20px; }
h1 { font-size: 2.5em; color: #2c3e50; }
.user-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); gap: 20px; }
.user-card { background-color: #fff; padding: 15px; text-align: center; border-radius: 8px; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); }
.skills-list { list-style-type: none; padding: 0; }
.skills-list li { margin: 5px 0; }
table { width: 100%; margin-top: 40px; border-collapse: collapse; }
table, th, td { border: 1px solid #ddd; padding: 8px; text-align: center; }
th { background-color: #f2f2f2; }
a { text-decoration: none; color: #2980b9; font-weight: bold; }
a:hover { text-decoration: underline; }
/* Styling for projects */
.project-list {
margin-top: 40px;
}
.project-card {
background-color: #fff;
padding: 15px;
margin-bottom: 20px;
text-align: left;
border-radius: 8px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}
.project-card h3 {
font-weight: bold;
color: #2980b9;
}
.project-card p {
font-size: 1.1em;
color: #333;
}
.project-card ul {
list-style-type: none;
padding: 0;
}
.project-card ul li {
margin: 5px 0;
}
</style>
"""
html_content = """
<h1>User Skills</h1>
<div class="user-grid">
{% for user_id, user_data in users.items() %}
<div class="user-card">
<a href="{{ url_for('user_profile', user_id=user_id) }}" class="username">{{ user_data.username }}</a>
<ul class="skills-list">
{% for skill in user_data.skills %}
<li>{{ skill }}</li>
{% endfor %}
</ul>
</div>
{% endfor %}
</div>
<h2>Skills Overview</h2>
<table>
<thead>
<tr>
<th>Skill</th>
<th>Need</th>
<th>Have</th>
</tr>
</thead>
<tbody>
{% for skill in skills %}
<tr>
<td>{{ skill[0] }}</td>
<td>{{ skill[1] }}</td>
<td>{{ skill[2] }}</td>
</tr>
{% endfor %}
</tbody>
</table>
<h2>Projects</h2>
<div class="project-list">
{% for project_name, project_data in projects.items() %}
<div class="project-card">
<a href="{{ url_for('show_project', project_name=project_name) }}" class="project-name">
<h3>{{ project_name }}</h3>
</a>
<p>{{ project_data.description }}</p>
<h4>Required Skills:</h4>
<ul>
{% for skill in project_data.skills %}
<li>{{ skill }}</li>
{% endfor %}
</ul>
</div>
{% endfor %}
</div>
"""
return render_template_string(inline_css + html_content, users=skills_dict, skills=skills_table, projects=project_dict)
# User Profiles
@app.route('/user/<int:user_id>', methods=['GET'])
def user_profile(user_id):
conn = get_db_connection()
cursor = conn.cursor()
cursor.execute('SELECT users.username, skills.name FROM users '
'JOIN user_skills ON users.id = user_skills.user_id '
'JOIN skills ON user_skills.skill_id = skills.id '
'WHERE users.id = %s', (user_id,))
user_skills = cursor.fetchall()
conn.close()
if not user_skills:
return jsonify({"error": "User not found"}), 404
username = user_skills[0][0] # The username is the first element of the first record
skills = [skill[1] for skill in user_skills]
# Inline CSS for profile page
profile_css = """
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
margin: 0;
padding: 20px;
color: #333;
}
h1 {
font-size: 2.5em;
color: #2c3e50;
margin-bottom: 20px;
}
h2 {
color: #2980b9;
}
ul {
list-style-type: none;
padding: 0;
}
ul li {
background-color: #ecf0f1;
margin: 5px;
padding: 10px;
border-radius: 5px;
}
a {
text-decoration: none;
color: #2980b9;
font-weight: bold;
}
a:hover {
text-decoration: underline;
}
</style>
"""
profile_html = """
<h1>{{ username }}'s Profile</h1>
<h2>Skills</h2>
<ul>
{% for skill in skills %}
<li>{{ skill }}</li>
{% endfor %}
</ul>
<a href="{{ url_for('show_skills') }}">Back to All Users</a>
"""
return render_template_string(profile_css + profile_html, username=username, skills=skills)
# Show individual project details
@app.route('/projects/<project_name>', methods=['GET'])
def show_project(project_name):
conn = get_db_connection()
cursor = conn.cursor()
cursor.execute('SELECT p.name, p.description, s.name FROM projects p '
'JOIN project_skills ps ON p.id = ps.project_id '
'JOIN skills s ON ps.skill_id = s.id '
'WHERE p.name = %s', (project_name,))
project_skills = cursor.fetchall()
conn.close()
if not project_skills:
return jsonify({"error": "Project not found"}), 404
description = project_skills[0][1] # The project description is the second element
skills = [skill[2] for skill in project_skills]
# Inline CSS for project page
project_css = """
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
padding: 20px;
color: #333;
}
h1 {
font-size: 2.5em;
color: #2c3e50;
margin-bottom: 20px;
}
h2 {
color: #2980b9;
}
ul {
list-style-type: none;
padding: 0;
}
ul li {
background-color: #ecf0f1;
margin: 5px;
padding: 10px;
border-radius: 5px;
}
a {
text-decoration: none;
color: #2980b9;
font-weight: bold;
}
a:hover {
text-decoration: underline;
}
</style>
"""
project_html = """
<h1>{{ project_name }}</h1>
<h2>Description</h2>
<p>{{ description }}</p>
<h2>Required Skills</h2>
<ul>
{% for skill in skills %}
<li>{{ skill }}</li>
{% endfor %}
</ul>
<a href="{{ url_for('show_skills') }}">Back to All Projects</a>
"""
return render_template_string(project_css + project_html, project_name=project_name, description=description, skills=skills)
if __name__ == '__main__':
init_db()
app.run(debug=True, host='127.0.0.1', port=8000)