Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions conf.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

115 changes: 115 additions & 0 deletions handle/DBProcess.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
# coding: utf-8
import csv


def checkDB():

DBResult = []

with open('list.csv', 'rb') as csvfile:
reader = csv.reader(csvfile, delimiter=' ')
for row in reader:
if row[1] == '0':
DBResult.append(row)
elif row[1] == '1':
DBResult.append(row)
elif row[1] == '*':
DBResult.append(row)
else:
return 2 #list.csv error,the status flag must be 1, 0 or *
return DBResult


def changeDB(arg, num): #Caution!!! arg and num are all String!

newCSVList = []
oldCSVList = checkDB()
if oldCSVList == 2:
return 2 #list.csv error,the status flag must be 1, 0 or *

for i in range(len(oldCSVList)):
if oldCSVList[i][0] == num:
newRow = oldCSVList[i]
newRow[1] = arg
newCSVList.append(newRow)
else:
newCSVList.append(oldCSVList[i])
#rewrite the list.csv
with open('list.csv', 'wb') as csvfile:
writer = csv.writer(csvfile, delimiter=' ')
writer.writerows(newCSVList)

return 0


def borrowEquipment(num):#Caution!!! num is a string!
DBList = checkDB()
if DBList == 2:
return 2

order = 999 #the order of the equipment in the DBList

for i in range(len(DBList)):
if DBList[i][0] == num:
order = i
break

if order == 999:
return 'The equipment number does not exsist'

if DBList[order][1] == '0':
return 'The equipment has been lent out.' #if the equipment has been lent out, return this to show the error
elif DBList[order][1] == '1':
if changeDB('0', num) == 0:
return 'Succeed!' # succeed
else:
return 2 #list.csv error(the status flag must be 1, 0 or *), return 2 to show the error


def returnEquipment(num):#Caution!!! num is a string!
DBList = checkDB()
if DBList == 2:
return 2 #list.csv error,the status flag must be 1, 0 or *

order = 999 #the order of the equipment in the DBList

for i in range(len(DBList)):
if DBList[i][0] == num:
order = i
break
if order == 999:
return 'The equipment number does not exsist'

if DBList[order][1] == '1':
return 'The equipment hasn\'t been lent out.' #if the equipment has not been lent out, return this to show the error
if DBList[order][1] == '0':
if changeDB('1', num) == 0:
return 'Succeed!' # succeed


def showEquipment():
DBList = checkDB()
if DBList == 2:
return 2 #list.csv error,the status flag must be 1, 0 or *

returnString = ''
for i in range(len(DBList)):
if DBList[i][1] == '1':
returnString += DBList[i][2]
returnString += ' Available\n'
elif DBList[i][1] == '0':
returnString += DBList[i][2]
returnString += ' Unavailable\n'
else:
returnString += '***'
returnString += DBList[i][2]
returnString += '***\n'

return returnString

# print(checkDB())
# print(borrowEquipment('101'))
# print(checkDB())
# print(returnEquipment('101'))
# print(checkDB())
# print(showEquipment())
30 changes: 30 additions & 0 deletions handle/list.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
*** * Camera
101 1 7D
102 1 6D
103 1 5D
104 1 280
*** * Lens
105 1 BMCC
201 1 16-35
202 1 24-105
203 1 70-200F4
204 1 70-200F2.8
205 1 35
*** * StorageCard
301 1 280-card-01
302 1 280-card-02
303 1 280-card-03
304 1 280-card-04
305 1 SD-01
306 1 SD-02
307 1 SD-03
308 1 SD-04
309 1 SD-05
*** * Battery
401 1 280-LargeBattery-01
402 1 280-LargeBattery-02
403 1 280-SmallBattery
*** * Tripod
501 1 Tripod-01
502 1 Tripod-02
503 1 Tripod-03
192 changes: 187 additions & 5 deletions handle/wx.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# -*- coding: utf-8 -*-
import tornado.escape
import tornado.web
import csv
import DBProcess
import datetime


from config import *
from wechat_sdk.messages import *
Expand All @@ -23,11 +27,80 @@ def wx_proc_msg(self, body):
content = wechat.message.content
if len(content.replace(' ', '')) == 0:
return wechat.response_none()
reply = auto_reply.reply(content)
if reply is not None:
return wechat.response_text(content=reply)
else:
return wechat.response_none()
li = content.lower().encode('utf-8').split()
print(li)
if li[0] == 'borrow' or li[0] == 'check':
if li[0] == 'check':
DBResult = []
with open('list.csv', 'rb') as csvfile:
reader = csv.reader(csvfile, delimiter=' ')
for row in reader:
if row[1] == '0':
DBResult.append(row)
elif row[1] == '1':
DBResult.append(row)
elif row[1] == '*':
DBResult.append(row)
else:
return wechat.response_text(content="list.csv error")#list.csv error,the status flag must be 1, 0 or *


returnString = ''
for i in range(len(DBResult)):
if DBResult[i][1] == '1':
returnString += DBResult[i][2]
returnString += ':'
returnString += DBResult[i][0]
returnString += ' Available\n'
elif DBResult[i][1] == '0':
returnString += DBResult[i][2]
returnString += ':'
returnString += DBResult[i][0]
returnString += ' Unavailable\n'
else:
returnString += '***'
returnString += DBResult[i][2]
returnString += '***\n'
return wechat.response_text(content=returnString.decode('utf-8'))
elif len(li) == 3:
text = DBProcess.borrowEquipment(li[1])
if text == 2:
return wechat.response_text(content="list.csv error")
with open('log.csv', 'ab') as csvfile:
writer = csv.writer(csvfile, delimiter=' ')
writer.writerow([source, li[2], 'borrow', li[1], datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")])
return wechat.response_text(content=text)
else:
return wechat.response_text(content="Wrong Command!")
if li[0] == 'return':
if len(li) == 3:
text = DBProcess.returnEquipment(li[1])
if text == 2:
return wechat.response_text(content="list.csv error")
with open('log.csv', 'ab') as csvfile:
writer = csv.writer(csvfile, delimiter=' ')
writer.writerow([source, li[2], 'return', li[1], datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")])
return wechat.response_text(content=text)
else:
return wechat.response_text(content="Wrong Command!")
if li[0] == 'log':
strList = []
str = ''
i = 0
with open('log.csv', 'rb') as csvfile:
reader = csv.reader(csvfile, delimiter=' ')
for row in reader:
strList.append(row)
if len(strList) > 16:
strList = strList[-15:-1]

for s in strList:
str += '-'.join(s[1:])
str += '\n'

return wechat.response_text(content=str)

return wechat.response_text(content="Wrong Command!")
if isinstance(wechat.message, ImageMessage):
picurl = wechat.message.picurl # PicUrl
media_id = wechat.message.media_id # MediaId
Expand Down Expand Up @@ -126,3 +199,112 @@ def post(self):
self.write(result)
except IOError, e:
return

def checkDB():
DBResult = []
with open('list.csv', 'rb') as csvfile:
reader = csv.reader(csvfile, delimiter=' ')
for row in reader:
if row[1] == '0':
DBResult.append(row)
elif row[1] == '1':
DBResult.append(row)
elif row[1] == '*':
DBResult.append(row)
else:
return 2
return DBResult

def changeDB(arg, num): #Caution!!! arg and num are all String!

newCSVList = []
oldCSVList = checkDB()
if oldCSVList == 2:
return 2 #list.csv error,the status flag must be 1, 0 or *

for i in range(len(oldCSVList)):
if oldCSVList[i][0] == num:
newRow = oldCSVList[i]
newRow[1] = arg
newCSVList.append(newRow)
else:
newCSVList.append(oldCSVList[i])
#rewrite the list.csv
with open('list.csv', 'wb') as csvfile:
writer = csv.writer(csvfile, delimiter=' ')
writer.writerows(newCSVList)

return 0

def borrowEquipment(num):#Caution!!! num is a string!
DBList = checkDB()
if DBList == 2:
return 2

order = 999 #the order of the equipment in the DBList

for i in range(len(DBList)):
if DBList[i][0] == num:
order = i
break

if order == 999:
return 'The equipment number does not exsist'

if DBList[order][1] == '0':
return 'The equipment has been lent out.' #if the equipment has been lent out, return this to show the error
elif DBList[order][1] == '1':
if changeDB('0', num) == 0:
return 'Succeed!' # succeed
else:
return 2 #list.csv error(the status flag must be 1, 0 or *), return 2 to show the error

def returnEquipment(num):#Caution!!! num is a string!
DBList = checkDB()
if DBList == 2:
return 2 #list.csv error,the status flag must be 1, 0 or *

order = 999 #the order of the equipment in the DBList

for i in range(len(DBList)):
if DBList[i][0] == num:
order = i
break
if order == 999:
return 'The equipment number does not exsist'

if DBList[order][1] == '1':
return 'The equipment hasn\'t been lent out.' #if the equipment has not been lent out, return this to show the error
if DBList[order][1] == '0':
if changeDB('1', num) == 0:
return 'Succeed!' # succeed

def showEquipment():
DBResult = []
with open('list.csv', 'rb') as csvfile:
reader = csv.reader(csvfile, delimiter=' ')
for row in reader:
if row[1] == '0':
DBResult.append(row)
elif row[1] == '1':
DBResult.append(row)
elif row[1] == '*':
DBResult.append(row)
else:
DBResult = [2]

if DBResult == [2]:
return 2 #list.csv error,the status flag must be 1, 0 or *

returnString = ''
for i in range(len(DBResult)):
if DBList[i][1] == '1':
returnString += DBResult[i][2]
returnString += ' Available\n'
elif DBList[i][1] == '0':
returnString += DBResult[i][2]
returnString += ' Unavailable\n'
else:
returnString += '***'
returnString += DBResult[i][2]
returnString += '***\n'
30 changes: 30 additions & 0 deletions list.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
*** * Camera
101 1 7D
102 1 6D
103 1 5D
104 1 280
*** * Lens
105 1 BMCC
201 1 16-35
202 1 24-105
203 1 70-200F4
204 1 70-200F2.8
205 1 35
*** * StorgeCard
301 1 280-card-01
302 1 280-card-02
303 1 280-card-03
304 1 280-card-04
305 1 SD-01
306 1 SD-02
307 1 SD-03
308 1 SD-04
309 1 SD-05
*** * Battery
401 1 280-LargeBattery-01
402 1 280-LargeBattery-02
403 1 280-SmallBattery
*** * Tripod
501 1 Tripod-01
502 1 Tripod-02
503 1 Tripod-03