-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindice.py
269 lines (224 loc) · 8.54 KB
/
indice.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
# -*- coding: utf-8 -*-
"""
Created on Thu Nov 2 15:52:54 2017
@author: Alberto
"""
import db
import string
import word
class Indice:
def __init__(self, recent_docs=[], current_protocol="",
max_recent_docs=20):
"""Loads the database."""
self.recent_docs = recent_docs
self.current_protocol = current_protocol
self.max_recent_docs = max_recent_docs
self.database = db.Archivo('Protocolo.db')
def new_protocol(self, protocol):
"""Creates a new protocol.
The name of the new protocol is passed as an argument.
Returns true is the protocol is created correctly, false otherwise.
"""
if self.database.create_table(protocol):
if self.current_protocol == "":
self.current_protocol = protocol
return True
else:
return False
def del_protocol(self, protocol):
"""Deletes a protocol.
The name of the protocol is passed as an argument.
Returns true is the protocol is deleted correctly, false otherwise.
"""
if self.database.delete_table(protocol):
# Deletes this protocol's documents from recent_docs
if self.recent_docs is not None:
reverse_list = self.recent_docs[-1::-1]
for line in reverse_list:
if str(line).count(protocol) > 0:
self.recent_docs.remove(line)
# Stipulates new current_protocol if the old one is removed
if protocol == self.current_protocol:
p = self.database.view_tables()
if p is None or p == []:
self.current_protocol = ""
else:
self.current_protocol, = p[0]
return True
else:
return False
def view_protocol(self, protocol):
"""Returns the contents of a protocol.
The name of the protocol is passed as an argument.
Returns a list where every element corresponds to a document of the
protocol. If an error occurs returns None.
"""
raw = self.database.view_table(protocol)
protocol_docs = []
if raw is not None:
for line in raw:
number, m_name, o_name, tramit = line
s = str(number) + ".- "
s += tramit + ". "
s += m_name
if o_name != "":
s += ", " + o_name
s += "."
protocol_docs.append(s)
protocol_docs.sort()
return protocol_docs
def add_doc(self, protocol, number, m_name, o_name, tramit):
"""Inserts a new document into a protocol.
The name of the protocol and all the necessary data are passed
as parameters.
Returns true if the document is inserted correctly, false otherwise.
"""
if self.database.insert_data(protocol, number, m_name, o_name, tramit):
self.add_to_recent_docs(protocol, number, m_name, o_name, tramit)
return True
else:
return False
def is_valid_number(self, protocol, number):
"""Checks if a document's number is valid.
The name of the protocol and the number are passed as parameter.
Returns true if there is no document in the protocol with the passed
number, false otherwise.
"""
doc = self.database.doc_by_number(protocol, number)
if doc is not None:
if not doc:
return True
else:
return False
else:
return "No pudo ejecutarse la orden"
def next_number(self, protocol):
"""Returns the next available number in a protocol.
The name of the protocol is passed as a parameter.
Finds the maximum number assigned to a document and returns it's
successor."""
number = self.database.max_number(protocol)
if number is not None:
n, = number
if n is None:
return 1
return n + 1
else:
return "No pudo ejecutarse la orden"
def add_to_recent_docs(self, protocol, number, m_name, o_name, tramit):
"""Add a document to the list of recent docs.
The protocol and the document data are passed as parameters.
If the amount of recent documents exceeds the maximum number permited,
the older one (first one) is removed."""
s = "En " + protocol + ": "
s += str(number) + ".- "
s += tramit + ". "
s += m_name
if o_name != "":
s += ", " + o_name
s += "."
if self.recent_docs is None:
self.recent_docs = [s]
else:
self.recent_docs.append(s)
if len(self.recent_docs) > self.max_recent_docs:
self.recent_docs.pop(0)
def view_protocols(self):
"""Returns a list of the protocols in the database.
If an error occur returns an error message.
"""
p = self.database.view_tables()
if p is not None:
protocols = []
for item in p:
prot, = item
protocols.append(prot)
protocols.sort()
return protocols
else:
return "No pudo ejecutarse la orden"
def make_index(self, doc_name, protocol, number_ini, number_fin):
"""Makes the index and print it in a docx document.
The name of the file is passed as a parameter.
The protocol and number range is passed too as a parameter.
"""
temp = string.ascii_uppercase
upper = temp.replace("N", "NÑ")
document = word.Documento(doc_name)
for letter in upper:
data = self.database.get_table_by_letter(protocol, letter,
number_ini, number_fin)
if data:
page = []
for doc in data:
number = doc[0]
body = doc[1]
if doc[2] == "":
body += ". "
else:
body += "; "
body += doc[2]
body += ". "
body += doc[3]
body += "."
page.append([number, body])
document.write_page(letter, page)
def get_doc_by_number(self, protocol, number):
doc = self.database.doc_by_number(protocol, number)
if doc is None:
return "Error al acceder a la base de datos."
else:
return doc
def delete_document(self, protocol, number):
status = self.database.delete_data(protocol, number)
if status is None:
return "Error al acceder a la base de datos."
else:
return True
def search_user(self, protocol, string):
p1 = self.database.search(protocol, "princCompareciente", string)
p2 = self.database.search(protocol, "otrosComparecientes", string)
set_results = set()
if p1 is not None and p2 is not None:
for item in p1:
number, m_name, o_name, tramit = item
s = str(number) + ".- "
s += tramit + ". "
s += m_name
if o_name != "":
s += ", " + o_name
s += "."
set_results.add(s)
for item in p2:
number, m_name, o_name, tramit = item
s = str(number) + ".- "
s += tramit + ". "
s += m_name
if o_name != "":
s += ", " + o_name
s += "."
set_results.add(s)
results = list(set_results)
results.sort()
return results
else:
return "No pudo ejecutarse la orden"
if __name__ == "__main__":
a = Indice(max_recent_docs=2)
print(a.search_user("Protocolo2017", "M"))
# a.make_index("Prueba.docx", "Protocolo2017", 1, 6)
p = "Protocolo2018"
# print(a.del_protocol(p))
# print(a.new_protocol(p))
# print(a.view_protocols())
# print(a.view_protocol(p))
# print(a.recent_docs)
# print(a.add_doc(p, 1, "Alberto", "","Permuta"))
# print(a.recent_docs)
# print(a.add_doc(p, 2, "Al", "Alexis","Compraventa"))
# print(a.recent_docs)
# print(a.add_doc(p, 3, "Daysi", "Felipe","Compraventa"))
# print(a.recent_docs)
# print(a.view_protocol(p))
# print(a.is_valid_number(p, 4))
# print(a.next_number(p))