Spaces:
Configuration error
Configuration error
Upload admin_portal.py
Browse files- apps/admin_portal.py +158 -3
apps/admin_portal.py
CHANGED
@@ -1,3 +1,158 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
"""
|
3 |
+
Created on Tue Jan 25 10:35:00 2022
|
4 |
+
|
5 |
+
@author: bullm
|
6 |
+
"""
|
7 |
+
import streamlit as st
|
8 |
+
from datetime import datetime
|
9 |
+
from datetime import timedelta
|
10 |
+
import pandas as pd
|
11 |
+
import os
|
12 |
+
import boto3
|
13 |
+
import json
|
14 |
+
import io
|
15 |
+
from sqlalchemy import create_engine
|
16 |
+
import psycopg2
|
17 |
+
import hashlib
|
18 |
+
import numpy as np
|
19 |
+
from Data.credentials import credentials_s3 as creds3
|
20 |
+
from Data.credentials import credentials_postgresql as credpost
|
21 |
+
|
22 |
+
|
23 |
+
def run_query(query):
|
24 |
+
url = credpost["POSTGRESQL"]
|
25 |
+
conn = psycopg2.connect(url, sslmode='require')
|
26 |
+
cur = conn.cursor()
|
27 |
+
cur.execute(query)
|
28 |
+
conn.commit()
|
29 |
+
cur.close()
|
30 |
+
conn.close()
|
31 |
+
|
32 |
+
def hashing(passw):
|
33 |
+
return hashlib.sha256(passw.encode()).hexdigest()
|
34 |
+
|
35 |
+
|
36 |
+
|
37 |
+
def style_table():
|
38 |
+
style_table = """
|
39 |
+
<style>
|
40 |
+
tbody tr:hover {
|
41 |
+
color:#BB1114;}
|
42 |
+
thead {
|
43 |
+
background-color:#BB1114 ;
|
44 |
+
color: #E8E8E8;
|
45 |
+
}
|
46 |
+
tbody tr:nth-child(odd) {
|
47 |
+
background-color: #fff;
|
48 |
+
}
|
49 |
+
tbody tr:nth-child(even) {
|
50 |
+
background-color: #eee;
|
51 |
+
}
|
52 |
+
tbody tr:nth-child(odd)
|
53 |
+
stTable {
|
54 |
+
border-collapse: collapse;
|
55 |
+
margin: 25px 0;
|
56 |
+
font-size: 0.9em;
|
57 |
+
min-width: 400px;
|
58 |
+
box-shadow: 0 0 20px rgba(0, 0, 0, 0.15);
|
59 |
+
}
|
60 |
+
</style>
|
61 |
+
"""
|
62 |
+
st.markdown(style_table, unsafe_allow_html=True)
|
63 |
+
|
64 |
+
|
65 |
+
def read_excel_s3(key, secret_key, bucket, path):
|
66 |
+
s3_client = boto3.client('s3', aws_access_key_id = key, aws_secret_access_key= secret_key)
|
67 |
+
response = s3_client.get_object(Bucket=bucket, Key=path)
|
68 |
+
data = response["Body"].read()
|
69 |
+
df = pd.read_excel(io.BytesIO(data))
|
70 |
+
return df
|
71 |
+
|
72 |
+
|
73 |
+
def logs_portal():
|
74 |
+
style_table()
|
75 |
+
key = creds3["S3_KEY_ID"]
|
76 |
+
secret_key = creds3["S3_SECRET_KEY"]
|
77 |
+
bucket = creds3["S3_BUCKET"]
|
78 |
+
path ='Logs.xlsx'
|
79 |
+
|
80 |
+
df = read_excel_s3(key, secret_key, bucket, path)[["Analista", "Fecha",
|
81 |
+
"Vista", "Subvista"]]
|
82 |
+
update_data = pd.read_excel('Data/update_data.xlsx', engine='openpyxl')
|
83 |
+
update_data.index = update_data["View"]
|
84 |
+
|
85 |
+
col1, col2 = st.beta_columns((3,1))
|
86 |
+
fecha_inicio = col1.date_input("Fecha inicio")
|
87 |
+
st.write(fecha_inicio)
|
88 |
+
fecha_inicio = datetime.combine(fecha_inicio, datetime.min.time())
|
89 |
+
df=df[df["Fecha"]> fecha_inicio]
|
90 |
+
# col2.table(update_data[["Last_Update"]])
|
91 |
+
# col2.table(df.groupby("Analista").count()["Vista"])
|
92 |
+
# col1.table(df.groupby(["Vista"]).count())
|
93 |
+
# col2.table(df.groupby(["Vista", "Subvista"]).count())
|
94 |
+
df["Count"] =1
|
95 |
+
table = pd.pivot_table(df, values ='Count', index='Vista',
|
96 |
+
columns='Analista', aggfunc=np.sum)
|
97 |
+
table.drop(columns=["bull"], inplace=True)
|
98 |
+
col1.table(table.fillna(0))
|
99 |
+
col2.table(table.sum(axis=1))
|
100 |
+
col1.table(table.sum(axis=0).T)
|
101 |
+
# table = pd.pivot_table(df, values ='Count', index='Subvista',
|
102 |
+
# columns='Analista', aggfunc=np.sum)
|
103 |
+
# col1.table(table.fillna(0))
|
104 |
+
|
105 |
+
|
106 |
+
def edit_credentials():
|
107 |
+
url = credpost["POSTGRESQL"]
|
108 |
+
engine = create_engine(url, echo=False)
|
109 |
+
data = pd.read_sql_query("""select * from credenciales""", con=engine)
|
110 |
+
col1, col2, col3 = st.beta_columns(3)
|
111 |
+
with col1.form('New user'):
|
112 |
+
user = st.text_input("Usuario")
|
113 |
+
passw = st.text_input("Contraseña", type="password")
|
114 |
+
passw2 = st.text_input("Repetir Contraseña", type="password")
|
115 |
+
mail = st.text_input("Mail")
|
116 |
+
nombre = st.text_input("Nombre")
|
117 |
+
area = st.text_input("Area")
|
118 |
+
cargo = st.text_input("Cargo")
|
119 |
+
ingresar = st.form_submit_button(label='Ingresar')
|
120 |
+
if ingresar:
|
121 |
+
if passw == passw2:
|
122 |
+
var = "(usuario, passw, area, cargo, mail, nombre)"
|
123 |
+
varlist = [user, hashing(passw), area, cargo, mail, nombre]
|
124 |
+
query = "INSERT INTO credenciales {Var} VALUES %r; ".format(Var=var) % (tuple(varlist),)
|
125 |
+
run_query(query)
|
126 |
+
st.info("Usuario agregado")
|
127 |
+
with col2.form('Edit User'):
|
128 |
+
user = st.selectbox("Seleccionar un Usuario", list(data["usuario"]))
|
129 |
+
passw = st.text_input("Nueva Contraseña", type="password")
|
130 |
+
passw2 = st.text_input("Repetir Contraseña", type="password")
|
131 |
+
area = st.text_input("Area")
|
132 |
+
cambiar = st.form_submit_button(label='Ingresar')
|
133 |
+
if cambiar:
|
134 |
+
if ingresar:
|
135 |
+
if passw == passw:
|
136 |
+
h_passw = hashing(passw)
|
137 |
+
query='''UPDATE credenciales
|
138 |
+
SET passw = '{}',
|
139 |
+
area = '{}',
|
140 |
+
WHERE user = '{}';
|
141 |
+
|
142 |
+
'''.format(h_passw, area, user)
|
143 |
+
run_query(query)
|
144 |
+
st.info("Usuario editado")
|
145 |
+
else:
|
146 |
+
st.error("Las contraseñas no coinciden")
|
147 |
+
with col3.form('Delete User'):
|
148 |
+
user_d = st.selectbox("Seleccionar un Usuario", list(data["usuario"]))
|
149 |
+
user_d2 = st.text_input("Confirmar usuario")
|
150 |
+
delete = st.form_submit_button(label='Delete')
|
151 |
+
if delete:
|
152 |
+
if user_d == user_d2:
|
153 |
+
query = """delete from credenciales where usuario='{}';""".format(user_d)
|
154 |
+
run_query(query)
|
155 |
+
st.info("Usuario elemindo")
|
156 |
+
else:
|
157 |
+
st.info("Usuarios no coinciden")
|
158 |
+
st.table(data[["usuario", "nombre", "area", "cargo"]])
|