This repository was archived by the owner on May 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp_utils.py
More file actions
210 lines (154 loc) · 5.65 KB
/
p_utils.py
File metadata and controls
210 lines (154 loc) · 5.65 KB
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
# -*- coding: utf-8 -*-
from django.db import connection, transaction
from django.contrib import messages
import urllib2
import re
import time
from projectapps.mail_e.utils import send_mail2 as send
import page_messages_list as pml
#TODO : comment!
def send_mail(email_to, subject, content):
return send(email_to, subject, content)
def db_query_dict(query):
'''
принимает query - SQL запрос
выполняет запрос к базе и отдаёт ассоциативный массив
имена полей в query должны быть либо уникальными либо именоваными
'''
cursor = connection.cursor()
cursor.execute(query)
desc = cursor.description
q_list = list()
for i in range(cursor.rowcount):
q_dict = dict()
data = cursor.fetchone()
for name, value in zip(desc, data) :
q_dict[name[0]] = value
q_list.append(q_dict)
return q_list, cursor.rowcount
# return q_list
def page_message(request, code=None, text=None, type='ERROR'):
message_text = ''
if text:
message_text = text
else:
if code == '':
message_text = 'Blank'
else:
if code in pml.m_text:
message_text = pml.m_text[code]
elif code in pml.m_code :
message_text = pml.m_code[code]
if type == 'error':
messages.add_message(request, messages.ERROR, message_text, fail_silently=True)
elif type == 'info':
messages.add_message(request, messages.INFO, message_text, fail_silently=True)
elif type == 'success':
messages.add_message(request, messages.SUCCESS, message_text, fail_silently=True)
else:
messages.add_message(request, messages.ERROR, message_text, fail_silently=True)
def render_me_price(r_string):
'''
форматирование цены
'''
string = r_string
if type(r_string) == float:
string = "%0.2f" % (r_string)
elif type(r_string) == int:
string = str(r_string)
elif type(r_string) == str and len(r_string.split('.')) > 1:
string = "%0.2f" % (float(r_string))
if 'None' in str(type(r_string)):
data = r_string
else:
data = ''
if len(string.split('.')) > 1:
str_int, str_dec = string.split('.')
else:
str_int = string
for i in range(len(str_int)):
str_int_revert = str_int[::-1]
data += str_int_revert[len(str_int)-(len(str_int)-i)]
z = str((float(i)+1)/3).split('.')
if z[1] == '0':
data += ' '
data = data[::-1]
try:
data += '.%s' % str_dec
except UnboundLocalError:
pass
return data
def send_sms(r_to, r_str):
text = r_str.replace(' ', '%20')
theurl = 'http://gate.sms-start.ru/send/'
theurl2 = 'http://gate.sms-start.ru/send/?phone={phone}&text={text}&sender=STAKOS.RU'.format(
phone = r_to,
text = text
)
username = 'stakos'
password = 's13s11'
passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, theurl, username, password)
authhandler = urllib2.HTTPBasicAuthHandler(passman)
opener = urllib2.build_opener(authhandler)
urllib2.install_opener(opener)
pagehandle = urllib2.urlopen(theurl2)
# print pagehandle.read()
return pagehandle.read()
def sms_request_status(sms_id='57338207'):
theurl = 'http://gate.sms-start.ru/status/'
theurl2 = 'http://gate.sms-start.ru/status/?id={sms_id}'.format(
sms_id = sms_id,
)
username = 'stakos'
password = 's13s11'
passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, theurl, username, password)
authhandler = urllib2.HTTPBasicAuthHandler(passman)
opener = urllib2.build_opener(authhandler)
urllib2.install_opener(opener)
pagehandle = urllib2.urlopen(theurl2)
# print pagehandle.read()
return pagehandle.read()
def phone_validate_util(function):
'''
декоратор создаёт сообщение если моб.номер не подтверждён
'''
def new(request, *args, **kwargs):
# print request
if not request.user.phone_mobile_status:
page_message(request, 70, None, 'info')
return function(request, *args, **kwargs)
return new
def phone_formater(r_str):
'''
форматирует телефон
удаляет всё не цифровое
отдаёт только 10 первых цифр
'''
string = re.sub(r"\D", '', r_str)
string = string.encode()
if len(string) >= 10:
string = string[-10:]
return string
def timeit(method):
'''
декоратор принтит время выполнния метода
!!! не использовать в продакшене, или переписать принт !!!
'''
def timed(*args, **kw):
ts = time.time()
result = method(*args, **kw)
te = time.time()
print '%r (%r) %2.2f sec' % (method.__name__, kw, te-ts)
return result
return timed
def cleanup_util(r_str):
'''
очищает строку от всего лишнего
нужно для поиска
работает с кириллицей
'''
return re.sub(ur'[^а-яa-z0-9]', u'', r_str.lower(), re.UNICODE)
def lang_stub(text=''):
return text