rabin 7 years ago
parent
commit
d8c339c455
3 changed files with 58 additions and 8 deletions
  1. 31 4
      core.py
  2. 25 3
      model.py
  3. 2 1
      web.py

+ 31 - 4
core.py

@@ -13,12 +13,14 @@ import getopt
 import json
 import ConfigParser
 import subprocess
+from tornado.web import Finish
 class Demeter(object):
 	path = ''
 	config = {}
 	serviceObj = {}
 	modelObj = {}
 	web = ''
+	request = False
 
 	def __new__(self, *args, **kwargs):
 		print 'error'
@@ -226,10 +228,15 @@ class Demeter(object):
 			return True
 		return False
 
-	@staticmethod
-	def error(string):
-		print string
-		os._exit(0)
+	@classmethod
+	def error(self, string):
+		if self.request:
+			self.request.out(string)
+			#self.request.finish()
+			raise Finish()
+		else:
+			print string
+			os._exit(0)
 
 class File(object):
 
@@ -301,4 +308,24 @@ class Shell(object):
 					break
 			return output
 
+class Check(object):
+	@staticmethod
+	def match(rule, value):
+		if not rule.match(value):
+			return False
+		return True
+
+	@staticmethod
+	def mobile(value):
+		rule = re.compile(r'1\d{10}')
+		return Check.match(rule, value)
+
+	@staticmethod
+	def number(value):
+		try:
+			int(value)
+			return True
+		except ValueError:
+			return False
+
 Demeter.initConfig()

+ 25 - 3
model.py

@@ -115,6 +115,7 @@ class Model(object):
 				self._attr[field] = fields[field]
 				self._key[field] = self._attr[field].getKey()
 				insert = (method == 'insert')
+				update = (insert or method == 'update')
 				if insert and self._attr[field].uuid:
 					self.setUuid(field, col)
 				bind = False
@@ -146,7 +147,8 @@ class Model(object):
 						val = self.createMd5(val)
 					if self._attr[field].type == 'boolean' and isinstance(val, (str, unicode)):
 						val = Demeter.bool(val)
-					#self.check(field, val, self._attr[field])
+					if update:
+						self.check(field, val, self._attr[field])
 					if type(val) == list:
 						val = tuple(val)
 					self._bind[field] = val
@@ -161,9 +163,20 @@ class Model(object):
 			if not val:
 				Demeter.error(field + ' not exists')
 		elif attr.match:
-			result = re.search(attr.match, val)
+			if '|' in attr.match:
+				temp = attr.match.split('|')
+				match = temp[0]
+				error = temp[1]
+			else:
+				match = attr.match
+				error = field + ' not match:' + match
+			if hasattr(Check, match):
+				method = getattr(Check, match)
+				result = method(val)
+			else:
+				result = re.search(match, val)
 			if not result:
-				Demeter.error(field + ' not match:' + attr.match)
+				Demeter.error(error)
 
 	def time(self):
 		return Demeter.time()
@@ -274,15 +287,21 @@ class Fields(object):
 		return self
 
 	def exp(self, value):
+		"""
 		if type(self.expValue) != list:
 			self.expValue = []
 		self.expValue.append(value)
+		"""
+		self.expValue = value
 		return self
 
 	def logic(self, value):
+		"""
 		if type(self.logicValue) != list:
 			self.logicValue = []
 		self.logicValue.append(value)
+		"""
+		self.logicValue = value
 		return self
 
 	def val(self, value, exp='=', logic='and'):
@@ -344,9 +363,12 @@ class Fields(object):
 		return self
 
 	def add(self, value):
+		"""
 		if not self.argv:
 			self.argv = []
 		self.argv.append(value)
+		"""
+		self.argv = value
 		return self
 
 	def ins(self, value):

+ 2 - 1
web.py

@@ -14,6 +14,7 @@ import tornado.httpserver
 
 class Base(tornado.web.RequestHandler):
 	def initialize(self):
+		Demeter.request = self
 		self.assign()
 		self.page()
 		self.cookie()
@@ -185,7 +186,7 @@ class Base(tornado.web.RequestHandler):
 		elif function:
 			result = '<script>parent.' + function + '(' + result + ')' + '</script>';
 		self.write(result)
-		#self.finish(result)
+		self.finish()
 
 class Web(object):
 	@staticmethod