-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
162 lines (110 loc) · 4.21 KB
/
main.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
import unittest
from flask import(
flash, make_response, redirect, render_template, url_for, request
)
from app import create_app
from flask_login import login_required, current_user
from app.forms import AccountForm, VaultForm
from app.sql_services import account_items, add_vault, all_account, get_account_by_id, get_account_by_name, get_accounts, get_vault_by_name, get_vault_name, get_vaults, put_account
app = create_app()
""" Comandos de testing """
@app.cli.command()
def test():
tests = unittest.TestLoader().discover('tests')
unittest.TextTestRunner().run(tests)
""" Manejos de errores"""
@app.errorhandler(404)
def not_found(error):
return render_template('404.html', error=error)
@app.errorhandler(500)
def not_found(error):
# se debe usar una variable abort o que nada funcione
return render_template('500.html', error=error)
"""Rutas"""
@app.route('/', methods=['GET', 'POST'])
def index():
return redirect(url_for('auth.login'))
@app.route('/vaults', methods=['GET', 'POST'])
@login_required
def home():
vault_form = VaultForm()
username = current_user.username
id_user = current_user.id
context = {
'vaults': get_vaults(id_user=id_user),
'vault_form': vault_form,
'username': username,
'items': account_items(id_user=id_user),
}
return render_template('home.html', **context)
@app.route('/vault/add', methods=['GET', 'POST'])
@login_required
def vault():
vault_form = VaultForm()
id_user = current_user.id
if vault_form.validate_on_submit():
vault_reference = get_vault_by_name(name=vault_form.vaultname.data, id_user=id_user)
if vault_reference is None:
add_vault(
name=vault_form.vaultname.data,
id_user=id_user,
description=vault_form.description.data
)
flash('Bóveda creada', 'info')
return redirect(url_for('home'))
flash('El nombre de la bóveda ya existe.')
return redirect(url_for('home'))
@app.route('/vaults/<id_vault>', methods=['GET', 'POST'])
@login_required
def account(id_vault):
account_form = AccountForm()
id_user = current_user.id
context = {
'username': current_user.username,
'items': account_items(id_user=id_user),
'accounts': get_accounts(id_vault=id_vault),
'vaults': get_vaults(id_user=id_user),
'id_vault': id_vault,
'vaultname': get_vault_name(id_vault=id_vault),
'account_form': account_form,
}
return render_template('account.html', **context)
@app.route('/account/add', methods=['POST', 'GET'])
@login_required
def add_account():
account_form = AccountForm()
id_user = current_user.id
id_vault_reference = account_form.id_vault.data
if account_form.validate_on_submit():
account_reference = get_account_by_name(account_form.name.data, id_user=id_user)
if account_reference is None:
put_account(
name=account_form.name.data,
id_user=id_user,
id_vault=id_vault_reference,
password=account_form.password.data,
page=account_form.page.data,
description=account_form.description.data
)
flash('Cuenta agregada', 'info')
return redirect(url_for('account', id_vault=id_vault_reference))
flash('El nombre de la cuenta ya existe.')
return redirect(url_for('account', id_vault=id_vault_reference))
@app.route('/vaults/<id_vault>/<id_account>', methods=['POST', 'GET'])
@login_required
def details_account(id_vault, id_account):
id_vault_reference = id_vault
details_account = get_account_by_id(id_account=id_account)
id_user = current_user.id
account_form = AccountForm()
context = {
'username': current_user.username,
'items': account_items(id_user=id_user),
'accounts': get_accounts(id_vault=id_vault),
'details_account': details_account,
'vaults': get_vaults(id_user=id_user),
'id_vault': id_vault_reference,
'vaultname': get_vault_name(id_vault=id_vault),
'account_form': account_form,
}
return render_template('account_detail.html', **context)