-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit ca03fa8
Showing
13 changed files
with
541 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
env | ||
__pycache__/ | ||
database.db | ||
venv/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,187 @@ | ||
import sqlite3 | ||
from flask import Flask, render_template, request, url_for, flash, redirect | ||
from werkzeug.exceptions import abort | ||
import datetime | ||
import time | ||
|
||
|
||
def get_db_connection(): | ||
conn = sqlite3.connect('database.db') | ||
conn.row_factory = sqlite3.Row | ||
return conn | ||
|
||
|
||
def get_post(post_id): | ||
conn = get_db_connection() | ||
post = conn.execute('SELECT * FROM posts WHERE id = ?', | ||
(post_id,)).fetchone() | ||
conn.close() | ||
if post is None: | ||
abort(404) | ||
return post | ||
|
||
|
||
app = Flask(__name__) | ||
app.config['SECRET_KEY'] = 'your secret key' | ||
|
||
|
||
@app.route('/') | ||
def index(): | ||
conn = get_db_connection() | ||
posts = conn.execute('SELECT * FROM posts').fetchall() | ||
conn.close() | ||
posts.reverse() | ||
return render_template('index.html', posts=posts) | ||
|
||
|
||
@app.route('/<int:post_id>') | ||
def post(post_id): | ||
post = get_post(post_id) | ||
return render_template('post.html', post=post) | ||
|
||
|
||
# @app.route('/create', methods=('GET', 'POST')) | ||
# def create(): | ||
# if request.method == 'POST': | ||
# title = request.form['title'] | ||
# content = request.form['content'] | ||
|
||
# if not title: | ||
# flash('Title is required!') | ||
# else: | ||
# conn = get_db_connection() | ||
# conn.execute('INSERT INTO posts (title, content) VALUES (?, ?)', | ||
# (title, content)) | ||
# conn.commit() | ||
# conn.close() | ||
# return redirect(url_for('index')) | ||
|
||
# return render_template('create.html') | ||
@app.route('/create', methods=('GET', 'POST')) | ||
def create(): | ||
if request.method == 'POST': | ||
title = request.form['username'] | ||
content = request.form['reason'] | ||
whom_to_meet = request.form['whom_to_meet'] | ||
created = request.form['entryTime'] | ||
exitTime = request.form['exitTime'] | ||
# exitTime = time.strptime(str(exitTime),"%Y-%m-%d %H:%M") | ||
# created = time.strptime(str(created),"%Y-%m-%d %H:%M") | ||
# exitTime = DateTimeConvert(exitTime) | ||
# created = DateTimeConvert(created) | ||
|
||
if not title: | ||
flash('Title is required!') | ||
else: | ||
conn = get_db_connection() | ||
conn.execute('INSERT INTO posts (username, reason,whom_to_meet,exit,created) VALUES (?, ? ,?,?,?)', | ||
(title, content,whom_to_meet,exitTime,created)) | ||
conn.commit() | ||
conn.close() | ||
return redirect(url_for('index')) | ||
|
||
return render_template('create.html') | ||
|
||
@app.route('/info', methods=('GET', 'POST')) | ||
def info(): | ||
conn = get_db_connection() | ||
HighVisited = conn.execute('SELECT id,whom_to_meet,COUNT(whom_to_meet) FROM posts GROUP BY whom_to_meet ORDER BY whom_to_meet DESC LIMIT 1').fetchone() | ||
ReasonForFrequentVisits = conn.execute('SELECT id,reason,COUNT(reason) FROM posts GROUP BY reason ORDER BY reason DESC LIMIT 1').fetchone() | ||
conn.close() | ||
|
||
if request.method == 'POST': | ||
print("post") | ||
date = request.form['findDate'] | ||
period = request.form['period'] | ||
period = int(period) | ||
print(date) | ||
print(period) | ||
conn = get_db_connection() | ||
query = "select * from posts where DATE(created) == strftime('%Y-%m-%d','"+date+"')" | ||
posts = conn.execute(query).fetchall() | ||
conn.close() | ||
intimes = [] | ||
outtimes =[] | ||
for i in posts: | ||
print(i[1]) | ||
print(DateTimeConvert(i[1])) | ||
intimes.append(DateTimeConvert(i[1])) | ||
outtimes.append(DateTimeConvert(i[5])) | ||
|
||
first_entry = min((intimes)) | ||
last_exit = max((outtimes)) | ||
|
||
print('-----------------------') | ||
|
||
step = 60 | ||
period = period * 60 | ||
max_number = 0 | ||
startTime = 0 | ||
endTIme = 0 | ||
for i in range(first_entry,last_exit,step): | ||
count = 0 | ||
for out in outtimes: | ||
if out >= i and out <=i+period: | ||
count+=1 | ||
if count>max_number: | ||
max_number = count | ||
startTime = i | ||
endTIme = i+period | ||
|
||
print(max_number) | ||
print(startTime) | ||
print(endTIme) | ||
print(datetime.datetime.fromtimestamp(startTime / 1e3)) | ||
print(datetime.datetime.fromtimestamp(endTIme / 1e3)) | ||
st_date=datetime.datetime.fromtimestamp(startTime) | ||
end_date=datetime.datetime.fromtimestamp(endTIme) | ||
return render_template('info.html', post=HighVisited,postted=ReasonForFrequentVisits,startDate=st_date,endDate=end_date,MaxCount=max_number) | ||
|
||
|
||
return render_template('info.html', post=HighVisited,postted=ReasonForFrequentVisits) | ||
|
||
def DateTimeConvert(date_in): | ||
date_processing = date_in.replace('T', '-').replace(':', '-').split('-') | ||
date_processing = [int(v) for v in date_processing] | ||
return int(time.mktime(datetime.datetime(*date_processing).timetuple())) | ||
|
||
@app.route('/<int:id>/edit', methods=('GET', 'POST')) | ||
def edit(id): | ||
post = get_post(id) | ||
|
||
print(post[1]) | ||
if request.method == 'POST': | ||
title = request.form['username'] | ||
content = request.form['reason'] | ||
whom_to_meet = request.form['whom_to_meet'] | ||
exitTime = request.form['exitTime'] | ||
created = request.form['entryTime'] | ||
|
||
if not title: | ||
flash('Title is required!') | ||
else: | ||
conn = get_db_connection() | ||
#(username, reason,whom_to_meet,exit,created) VALUES (?, ? ,?,?,?) | ||
conn.execute('UPDATE posts SET username = ?, reason = ? ,whom_to_meet = ?,exit = ? ,created = ?' | ||
' WHERE id = ?', | ||
(title, content,whom_to_meet,exitTime,created,id)) | ||
conn.commit() | ||
conn.close() | ||
return redirect(url_for('index')) | ||
|
||
return render_template('edit.html', post=post) | ||
|
||
|
||
@app.route('/<int:id>/delete', methods=('POST',)) | ||
def delete(id): | ||
post = get_post(id) | ||
conn = get_db_connection() | ||
conn.execute('DELETE FROM posts WHERE id = ?', (id,)) | ||
conn.commit() | ||
conn.close() | ||
flash('"{}" was successfully deleted!'.format(post['username'])) | ||
return redirect(url_for('index')) | ||
|
||
if __name__ == '__main__': | ||
# app.run(host='0.0.0.0') | ||
app.run(debug=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from flask import Flask, request, escape | ||
|
||
app = Flask(__name__) | ||
|
||
|
||
@app.route('/') | ||
def hello(): | ||
return 'Hello, World!' | ||
|
||
|
||
@app.route('/greet') | ||
def greet(): | ||
name = request.args['name'] | ||
return ''' | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title></title> | ||
</head> | ||
<body> | ||
<h1>Hi {}</h1> | ||
</body> | ||
</html>'''.format(escape(name)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import sqlite3 | ||
|
||
connection = sqlite3.connect('database.db') | ||
|
||
|
||
with open('schema.sql') as f: | ||
connection.executescript(f.read()) | ||
|
||
cur = connection.cursor() | ||
|
||
cur.execute("INSERT INTO posts (username, reason) VALUES (?, ?)", | ||
('First Post', 'Content for the first post') | ||
) | ||
|
||
cur.execute("INSERT INTO posts (username, reason) VALUES (?, ?)", | ||
('Second Post', 'Content for the second post') | ||
) | ||
|
||
connection.commit() | ||
connection.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
DROP TABLE IF EXISTS posts; | ||
|
||
CREATE TABLE posts ( | ||
id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
created TIMESTAMP NOT NULL DEFAULT (datetime('now','localtime')), | ||
username TEXT NOT NULL, | ||
reason TEXT NOT NULL, | ||
whom_to_meet TEXT, | ||
exit TIMESTAMP | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
h1 { | ||
border: 2px #eee solid; | ||
color: brown; | ||
text-align: center; | ||
padding: 10px; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<!-- Required meta tags --> | ||
<meta charset="utf-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> | ||
|
||
<!-- Bootstrap CSS --> | ||
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"> | ||
|
||
<title>{% block title %} {% endblock %}</title> | ||
</head> | ||
<body> | ||
<nav class="navbar navbar-expand-md navbar-light bg-light"> | ||
<a class="navbar-brand" href="{{ url_for('index')}}">Visitors</a> | ||
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation"> | ||
<span class="navbar-toggler-icon"></span> | ||
</button> | ||
<div class="collapse navbar-collapse" id="navbarNav"> | ||
<ul class="navbar-nav"> | ||
<li class="nav-item"> | ||
<a class="nav-link" href="{{url_for('info')}}">info</a> | ||
</li> | ||
<li class="nav-item"> | ||
<a class="nav-link" href="{{url_for('create')}}">New Visitor</a> | ||
</li> | ||
</ul> | ||
</div> | ||
</nav> | ||
<div class="container"> | ||
{% for message in get_flashed_messages() %} | ||
<div class="alert alert-danger">{{ message }}</div> | ||
{% endfor %} | ||
{% block content %} {% endblock %} | ||
</div> | ||
|
||
<!-- Optional JavaScript --> | ||
<!-- jQuery first, then Popper.js, then Bootstrap JS --> | ||
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script> | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script> | ||
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script> | ||
</body> | ||
</html> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
{% extends 'base.html' %} | ||
|
||
{% block content %} | ||
<h1>{% block title %} Create a New Entry {% endblock %}</h1> | ||
|
||
<form method="post"> | ||
<div class="form-group"> | ||
<label for="title">Name</label> | ||
<input type="text" name="username" id="username" | ||
placeholder="Name" class="form-control" | ||
value="{{ request.form['username'] }}"></input> | ||
</div> | ||
|
||
<div class="form-group"> | ||
<label for="content">Reason</label> | ||
<textarea name="reason" placeholder="Reason for visit" | ||
class="form-control">{{ request.form['reason'] }}</textarea> | ||
</div> | ||
<div class="form-group"> | ||
<label for="title">Whom are you visiting</label> | ||
<input type="text" name="whom_to_meet" id="whom_to_meet" | ||
placeholder="Whom are you visiting ?" class="form-control" | ||
value="{{ request.form['whom_to_meet'] }}"></input> | ||
</div> | ||
<div class="form-group"> | ||
<label for="title">Entry Time</label> | ||
<input type="datetime-local" id="entryTime" name="entryTime" | ||
min="<?php echo $currenttime?>" required> | ||
</div> | ||
<div class="form-group"> | ||
<label for="title">Exit Time </label> | ||
<input type="datetime-local" id="exitTime" name="exitTime" | ||
min="<?php echo $currenttime?>" required> | ||
</div> | ||
|
||
<div class="form-group"> | ||
<button type="submit" class="btn btn-primary">Submit</button> | ||
</div> | ||
|
||
</form> | ||
{% endblock %} |
Oops, something went wrong.