SHOW:
|
|
- or go back to the newest paste.
1 | - | import utility |
1 | + | |
2 | - | import http_request |
2 | + | import psycopg2 |
3 | import time | |
4 | - | class Weather: |
4 | + | |
5 | - | def __init__(self, config, city_name): |
5 | + | |
6 | - | self.config = config |
6 | + | class DataBase: |
7 | - | self.city = city_name |
7 | + | def __init__(self, settings): |
8 | - | self.weather_all_info = self.http_answer() |
8 | + | |
9 | # if 'host' not in settings: | |
10 | - | def http_answer(self): |
10 | + | # settings["host"] = 'localhost' |
11 | if 'port' not in settings: | |
12 | - | if 'address' not in self.config: |
12 | + | settings["port"] = '' |
13 | - | address = "api.openweathermap.org" |
13 | + | # if 'dbuser' not in settings: |
14 | # settings["dbuser"] = 'postgres' | |
15 | - | address = self.config['http']["address"] |
15 | + | # try: |
16 | - | if 'uri' not in self.config: |
16 | + | # self.settings = ( |
17 | - | uri = "/data/2.5/weather" |
17 | + | # "dbname='{dbname}' user='{dbuser}' host={host} password='{password}' port = '{port}'").format( |
18 | # dbname=settings['dbname'], | |
19 | - | uri = self.config['http']['uri'] |
19 | + | # dbuser=settings['dbuser'], |
20 | # host=settings['host'], | |
21 | - | city_info = utility.get_city_info(self.city) |
21 | + | # password=settings['password'], |
22 | - | weather_all_info = http_request.HttpRequest(address, uri).get_info(city_info['API'], city_info['ID']) |
22 | + | # port=settings['port']) |
23 | - | return weather_all_info |
23 | + | # except KeyError as er: |
24 | # print er, "is required parameter" | |
25 | - | def get_weather(self, ): |
25 | + | # exit() |
26 | - | fahrenheit = int(self.weather_all_info['main']['temp']) |
26 | + | |
27 | - | celsius = utility.transfer_to_celsius(fahrenheit) |
27 | + | self.conn = None |
28 | - | return celsius |
28 | + | self.cursor = None |
29 | # self.connect() | |
30 | - | def get_info(self, ): |
30 | + | pass |
31 | - | return self.weather_all_info |
31 | + | |
32 | def start_transaction(self): | |
33 | answer = self.conn.autocommit | |
34 | if self.conn.autocommit: | |
35 | self.conn.autocommit = False | |
36 | return answer | |
37 | ||
38 | def end_transaction(self, transaction_started): | |
39 | if transaction_started: | |
40 | self.conn.commit() | |
41 | self.conn.autocommit = True | |
42 | print 'commit done' | |
43 | ||
44 | def roll_back(self, transaction_started): | |
45 | if transaction_started: | |
46 | self.conn.rollback() | |
47 | self.conn.commit() | |
48 | self.conn.autocommit = True | |
49 | ||
50 | def connect(self): | |
51 | ||
52 | try: | |
53 | self.conn = psycopg2.connect(self.settings) | |
54 | except psycopg2.Error as er: | |
55 | print er, "\n", | |
56 | exit() | |
57 | self.conn.autocommit = True | |
58 | self.cursor = self.conn.cursor() | |
59 | pass | |
60 | ||
61 | def disconnect(self): | |
62 | try: | |
63 | self.conn.close() | |
64 | self.conn = None | |
65 | except psycopg2.Error as er: | |
66 | print er | |
67 | exit() | |
68 | pass | |
69 | ||
70 | def get_info(self, table_name, wonted_data, search=None): | |
71 | # self.conn.autocommit = True | |
72 | self.cursor = self.conn.cursor() | |
73 | values = "" | |
74 | ||
75 | if isinstance(wonted_data, list): | |
76 | first = True | |
77 | for data in wonted_data: | |
78 | if first: | |
79 | values += data | |
80 | first = None | |
81 | else: | |
82 | values += ',' + data | |
83 | else: | |
84 | values += wonted_data | |
85 | ||
86 | sql_request = 'SELECT ' + values + ' FROM ' + str(table_name) + ' WHERE deleted_at = 0' | |
87 | ||
88 | if search is not None: | |
89 | values = () | |
90 | for key in search: | |
91 | first = True | |
92 | for val in search[key]: | |
93 | ||
94 | values += (val,) | |
95 | if first: | |
96 | sql_request += " and " + key + " IN (%s" | |
97 | first = None | |
98 | else: | |
99 | sql_request += ",%s" | |
100 | sql_request += ")" | |
101 | ||
102 | try: | |
103 | # print sql_request | |
104 | # print (self.cursor.mogrify(sql_request, values).decode('utf8')) | |
105 | self.cursor.execute(sql_request, values) | |
106 | return self.cursor.fetchall() | |
107 | except TypeError as er: | |
108 | print er | |
109 | exit() | |
110 | ||
111 | def insert(self, table_name, dic): | |
112 | # self.conn.autocommit = True | |
113 | self.cursor = self.conn.cursor() | |
114 | columns = [] | |
115 | values = () | |
116 | for param in dic: | |
117 | columns.append(param) | |
118 | values += (dic[param],) | |
119 | sql_request = "INSERT INTO {table} ({param}) VALUES ({s}) returning id".format( | |
120 | table=table_name, | |
121 | param=', '.join(columns), | |
122 | s=', '.join(["%s"]*len(values))) | |
123 | # print (self.cursor.mogrify(sql_request, values).decode('utf8')) | |
124 | self.cursor.execute(sql_request, values) | |
125 | return self.cursor.fetchone()[0] | |
126 | pass | |
127 | ||
128 | def update(self, table_name, dic, identifier): | |
129 | # self.conn.autocommit = True | |
130 | # self.cursor = self.conn.cursor() | |
131 | part_of_sql_request = [] | |
132 | values = () | |
133 | sql_request = "UPDATE " + table_name + " SET" | |
134 | ||
135 | for column in dic: | |
136 | part = " " + column + " = %s" | |
137 | values += (str(dic[column]),) | |
138 | part_of_sql_request.append(part) | |
139 | sql_request += ' ,'.join(part_of_sql_request) | |
140 | sql_request += " WHERE deleted_at = 0 and id IN( " | |
141 | # del part_of_sql_request[:] | |
142 | for id in identifier: | |
143 | sql_request += "%s," | |
144 | values += (id,) | |
145 | # sql_request += ")" | |
146 | # part_of_sql_request.append(part) | |
147 | sql_request += ')' | |
148 | print sql_request | |
149 | print values | |
150 | # print (self.cursor.mogrify(sql_request, values).decode('utf8')) | |
151 | ||
152 | try: | |
153 | self.cursor.execute(sql_request, values) | |
154 | return self.cursor.fetchone()[0] | |
155 | except psycopg2.Error as er: | |
156 | print er | |
157 | pass | |
158 | ||
159 | def delete(self, table_name, identifiers): | |
160 | ||
161 | delete_data = { | |
162 | "deleted_at": time.time() | |
163 | } | |
164 | return self.update(table_name, delete_data, identifiers) | |
165 | ||
166 | def full_delete(self, table_name, identifiers): | |
167 | self.conn.autocommit = True | |
168 | self.cursor = self.conn.cursor() | |
169 | sql_request = "DELETE FROM {table} where id = %s".format( | |
170 | table=table_name) | |
171 | val = () | |
172 | val += (identifiers,) | |
173 | try: | |
174 | self.cursor.execute(sql_request, val) | |
175 | return self.cursor.fetchone()[0] | |
176 | except psycopg2.Error as er: | |
177 | print er |