Commit 68025568 authored by Duscha's avatar Duscha

Projekt fertig: Username im Link, Subseiten funktionieren, Username im Titel,...

Projekt fertig: Username im Link, Subseiten funktionieren, Username im Titel, Usernametabelle auf der Startseite
parent 24375120
FROM python:3.7.3
FROM debian:10.4
COPY app /app
RUN apt-get update && apt-get install
RUN apt-get install python3-pip -y
RUN pip3 install Flask
RUN pip3 install jinja2
RUN pip3 install pyyaml
COPY . /website
CMD python3 sqlite1.py, python3 run.py
CMD python3 /app/sqlite.py, python3 /app/run.py
import sqlite3
from flask import Flask, render_template
myapp = Flask(__name__)
dic = {"nname" : "Ford", "vname" : "Harrison", "gebjahr" : 1942}
def ausgabe():
connect = sqlite3.connect("/website/test.db")
cursor = connect.cursor()
cursor.execute("SELECT COUNT(username) AS username FROM benutzer")
#anzahl = int(str(cursor.fetchone()))
a_user = []
for i in range(0,4):
cursor.execute("SELECT username FROM benutzer WHERE b_nr ="+ str(i))
temp_name = cursor.fetchone()
a_user.append(temp_name)
print(type(a_user))
return a_user
user = cursor.fetchall()
def attribute():
connect = sqlite3.connect("/website/test.db")
cursor = connect.cursor()
cursor.execute("SELECT username FROM benutzer")
user = cursor.fetchall()
print(type(user))
username = []
print(type(username))
for person in user:
username.append(person)
i = 1
return username
@myapp.route("/users")
def templ_test():
return render_template('users.html', mydict = dic, list=[3,6,2,"drei",4,5], user_list = attribute(), userarray = ausgabe())
@myapp.route("/users/('mduscha',)")
def getduscha():
connect = sqlite3.connect("/website/test.db")
cursor = connect.cursor()
cursor.execute("SELECT * FROM benutzer WHERE username = 'mduscha'")
mduscha = cursor.fetchall()
return str(mduscha)
if __name__ == '__main__':
#print(dic)
#ausgabe()
#print(attribute())
myapp.run(host = "0.0.0.0", debug = True)
\ No newline at end of file
#!/usr/bin/python3
import sqlite3
import yaml
connection = sqlite3.connect("test.db")
cursor = connection.cursor()
#Ab hier: ABSTRAKTION
cursor.execute("DROP TABLE IF EXISTS benutzer;")
sql_command = """
CREATE TABLE benutzer (
b_nr INTEGER PRIMARY KEY,
vname VARCHAR(40),
nname VARCHAR(40),
username VARCHAR(40),
aktiv INTEGER,
mail VARCHAR(60),
beitritt DATE
); """
cursor.execute(sql_command)
#Benutzer aus YAML-Datei lesen und in die Tabelle einfügen
with open('users.yaml') as f:
global users
users = yaml.safe_load(f)
#print(users)
def addPerson(vname, nname, username, aktiv, mail, beitritt):
cursor.execute("INSERT INTO benutzer(vname, nname, username, aktiv, mail, beitritt) VALUES (:vn, :nn, :un, :akt, :mail, :bei)", {'vn': vname, 'nn': nname, 'un': username, 'akt': aktiv, 'mail': mail, 'bei': beitritt})
for x in range (0, 3):
addPerson(users[x] ['vname'], users[x] ['nname'], users[x] ['username'], users[x] ['aktiv'], users[x] ['mail'], users[x] ['beitritt'])
connection.commit()
connection.close()
#sql_command = """
#SELECT *
#FROM benutzer;
#"""
#cursor.execute(sql_command)
#print(cursor.fetchall())
\ No newline at end of file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Meine Seite</title>
</head>
<body>
{{string_aus}}
</body>
</html>
\ No newline at end of file
-
b_nr: 0
vname: Miriam
nname: Duscha
username: mduscha
aktiv: 1
mail: mduscha@mail.de
beitritt: 2020-07-20
-
b_nr: 1
vname: Fabian
nname: Wandt
username: fwandt
aktiv: 1
mail: fwandt@mail.de
beitritt: 2020-07-20
-
b_nr: 2
vname: Maria
nname: Dalton-Beatle
username: mariadb
aktiv: 0
mail: mariadb@mail.de
beitritt: 2017-04-20
\ No newline at end of file
#!/usr/bin/python3
from flask import Flask, render_template
import sqlite3
DATABASE = "test.db"
app = Flask(__name__)
name = str(app.route)
def all_user():
connection = sqlite3.connect("test.db")
cursor = connection.cursor()
connection.row_factory = sqlite3.Row
cursor.execute("SELECT username FROM benutzer")
usernames = cursor.fetchall()
return usernames
def one_user(name):
connection = sqlite3.connect("test.db")
cursor = connection.cursor()
connection.row_factory = sqlite3.Row
cursor.execute("SELECT * FROM benutzer WHERE vname = " + str(name))
username = cursor.fetchone()
return username
@app.route('/user')
def aufruf():
return render_template("user.html", user_nms = all_user())
if __name__ == '__main__':
app.run(host = "0.0.0.0", debug = True)
\ No newline at end of file
#!/usr/bin/python3
from flask import Flask, render_template, request
import sqlite3
databse = "test.db"
app = Flask(__name__)
name = str(app.route)
def all_user():
connection = sqlite3.connect("test.db")
cursor = connection.cursor()
connection.row_factory = sqlite3.Row
cursor.execute("SELECT username FROM benutzer;")
all_i = cursor.fetchall()
return all_i
def one_user(name):
connection = sqlite3.connect("test.db")
cursor = connection.cursor()
connection.row_factory = sqlite3.Row
cursor.execute("SELECT * FROM benutzer WHERE username = '" + name + "';")
user_data = cursor.fetchone()
return user_data
def get_name(name):
connection = sqlite3.connect("test.db")
cursor = connection.cursor()
connection.row_factory = sqlite3.Row
cursor.execute("SELECT username FROM benutzer WHERE username = '" + name + "';")
user_id = cursor.fetchone()
return user_id
@app.route('/user', methods=['GET','POST'])
def username_ausgabe():
return render_template("users.html", all_ids = all_user())
@app.route('/user/<username>', methods=['GET','POST'])
def user_ausgabe(username):
return render_template("user.html", user = one_user(username), name=get_name(username))
if __name__ == '__main__':
app.run(host = "0.0.0.0", debug = True)
\ No newline at end of file
#!/usr/bin/python3
import sqlite3
import yaml
#Einlesen der User aus der yaml-Datei users
with open('users.yaml') as yaml_file:
users = yaml.load(yaml_file)
#Aufbauen einer Verbindung zur Datenbank test
connection = sqlite3.connect("test.db")
cursor = connection.cursor()
#Löscht Tabelle Benutzer, wenn sie exisitert
drop_benutzer = "DROP TABLE IF EXISTS benutzer; "
cursor.execute(drop_benutzer)
#Erstellt Tabelle Benutzer
create_benutzer = """
CREATE TABLE benutzer (
user_id INTEGER PRIMARY KEY,
vname VARCHAR(20),
nname VARCHAR(20),
username VARCHAR(20),
joining DATE,
aktiv VCHAR(4)
);"""
cursor.execute(create_benutzer)
#Methode, um User-Datensätze in die Tabelle einzutragen
def insert(user_id, vname, nname, username, joining, aktiv):
cursor.execute("INSERT INTO benutzer(user_id, vname, nname, username, joining, aktiv) VALUES (:user_id, :vname, :nname, :username, :joining, :aktiv)" , {'user_id' : user_id, 'vname' : vname, 'nname' : nname, 'username' : username, 'joining' :joining, 'aktiv' : aktiv})
#Eintragen der vorhandenen user (aus der Yaml-Datei) in die Tabelle
for p in users:
insert(p['user_id'], p['vname'], p['nname'], p['username'], p['joining'], p['aktiv'])
connection.commit()
connection.close()
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<br>
{{my_string}}
<br>
<br>
{{my_list}}
<br>
</body>
</html>
\ No newline at end of file
<!doctype html>
<html>
<head>
<title>Username: {% for n in name %} {{n}} {% endfor %}</title>
</head>
<body>
<table border = 1>
<thead>
<td>User_id</td>
<td>Nachname</td>
<td>Vorname</td>
<td>Nutzername</td>
<td>Beigetreten am</td>
<td>Aktiver Nutzer</td>
</thead>
<tr>
{% for attribute in user %}
<td>{{attribute}}</td>
{% endfor %}
</tr>
</table>
<br>
<br>
<a href="/user">Startseite</a>
<br>
</body>
</html>
\ No newline at end of file
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Startseite</title>
</head>
<body>
<table style="width:100px"; border=2>
<tr><thead> User: </thead></tr>
<tr>{% for person in all_ids %}
<tr>{% for id in person %}
<td><a href="user/{{id}}">{{id}} </a></td>
{% endfor %} </tr>
{% endfor %} </tr>
</table>
</body>
</html>
\ No newline at end of file
-
user_id: 1
vname: Miriam
nname: Duscha
username: dramalama
joining: 20.03.1990
aktiv: ja
-
user_id: 2
vname: Fabian
nname: Wandt
username: scriptkiddie
joining: 13.03.2005
aktiv: nein
-
user_id: 3
vname: Chris
nname: Linnemann
username: erdbeere
joining: 15.06.2000
aktiv: ja
-
user_id: 4
vname: Kilian
nname: Hassatzky
username: cptkiwi
joining: 15.06.2000
aktiv: ja
\ No newline at end of file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Meine Seite</title>
</head>
<body>
<br>
Dictionairy:
<ul>
{% for key,value in mydict.items() %}
<li> {{ key }} --&gt; {{ value }} </li>
{% endfor %}
</ul>
<br>
User:
<br>
<ul>
{% for user in user_list %}
<li> <a href="http://0.0.0.0:5000/users/{{user}}"> {{user}} </a> </li>
{% endfor %}
</ul>
<br>
Test:
<br>
{{userarray}}
</body>
</html>
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment