-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
158 lines (140 loc) · 5.99 KB
/
main.py
File metadata and controls
158 lines (140 loc) · 5.99 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
# coding=UTF-8
import cherrypy
import codecs
import mysql.connector
import settings
import os, os.path
import types
import json
from Cheetah.Template import Template
from datetime import date
from datetime import datetime, timedelta
from copy import copy
from functools import partial
server_host = settings.server_host
server_port = settings.server_port
def login_page(message):
login_page = os.path.join('html', 'login.html')
f = codecs.open(login_page, encoding='utf-8')
temp = f.read()
rend = Template(temp)
rend.message = message
return unicode(rend)
def error_page(message="Ошибка сервера. Обратитесь к администратору."):
return message
class Root(object):
@cherrypy.expose
def checklogin(self, username, passwd, action):
try:
cnx = mysql.connector.connect(user=settings.user,
password=settings.password,
host=settings.host,
database=settings.database)
cursor = cnx.cursor()
query = "SELECT password FROM spa.users WHERE user_name = %s "
cursor.execute(query, [username])
rows = cursor.fetchall()
if len(rows) > 0:
if (rows[0][0] == passwd):
cherrypy.session['sid'] = cherrypy.session.id
raise cherrypy.HTTPRedirect("/")
else:
return login_page("Отказано в доступе!")
else:
return login_page("Отказано в доступе! " + cursor._last_executed)
except cherrypy.HTTPRedirect:
raise
except Exception, e:
return login_page("Отказано в доступе!")
@cherrypy.expose
def index(self):
if 'sid' not in cherrypy.session:
return login_page("init")
else:
try:
login_page1 = os.path.join('html', 'main.html')
f = codecs.open(login_page1, encoding='utf-8')
temp = f.read()
rend = Template(temp)
rend.heading = u"База отдыха 'ПУНК'"
return unicode(rend)
except Exception, e:
cherrypy.log("Root. Template Render Failure!", traceback=True)
return error_page(str(e))
@cherrypy.expose
def logout(self):
cherrypy.session.delete()
return login_page("init")
@cherrypy.expose
@cherrypy.tools.json_out()
def room(self, id):
cnx = mysql.connector.connect(user=settings.user,
password=settings.password,
host=settings.host,
database=settings.database)
return active_room_attributes(cnx, id)
@cherrypy.expose
def map(self):
if 'sid' not in cherrypy.session:
return login_page("init")
try:
cnx = mysql.connector.connect(user=settings.user,
password=settings.password,
host=settings.host,
database=settings.database)
cursor = cnx.cursor()
query = "SELECT id_room, room_type FROM spa.rooms"
cursor.execute(query)
rows = cursor.fetchall()
f = codecs.open('html/map.html', encoding='utf-8')
temp = f.read()
rend = Template(temp)
rend.heading = u"База отдыха 'ПУНК'"
rend.rooms = [x[0] for x in rows]
return unicode(rend)
except Exception, e:
cherrypy.log("Root. Template Render Failure!", traceback=True)
return error_page(str(e))
@cherrypy.expose
def submit_dates(self, room_id):
#cherrypy.response.headers['Content-Type'] = 'application/json'
id = int(room_id)
try:
cnx = mysql.connector.connect(user=settings.user,
password=settings.password,
host=settings.host,
database=settings.database)
cursor = cnx.cursor()
query = ("SELECT id_room, start_date, end_date FROM spa.booking WHERE id_room = %d" % id)
cursor.execute(query)
rows = cursor.fetchall()
format = "%Y-%m-%d"
dates = []
for x in rows:
dates += [date.fromordinal(i).strftime(format) for i in range(x[1].toordinal(), x[2].toordinal() + 1)]
message = { "dates" : dates }
return json.dumps(message)
except Exception, e:
cherrypy.log("Some MySQL error", traceback=True)
return json.dumps(dict(room_info="Error"))
@cherrypy.expose
def book_dates(self, room_id, start_date, end_date):
try:
id = int(room_id)
format = "%d/%m/%Y"
values = (id, datetime.strptime(start_date, format).date(), datetime.strptime(end_date, format).date(), '', '')
cnx = mysql.connector.connect(user=settings.user,
password=settings.password,
host=settings.host,
database=settings.database)
cursor = cnx.cursor()
insert = ("INSERT INTO spa.booking (id_room, start_date, end_date, FULL_NAME, PHONE_NUMBER) VALUES(%s, %s, %s, %s, %s)")
cursor.execute(insert, values)
cnx.commit()
return json.dumps(dict(room_info="Success"))
except Exception, e:
cherrypy.log("MySQL insert problem", traceback = True)
return error_page(str(e))
if __name__ == '__main__':
cherrypy.config.update({'server.socket_host': server_host, 'server.socket_port': server_port,})
cherrypy.quickstart(Root(), '/', "app.conf")