common.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. demeter service
  5. name:common.py 通用业务
  6. author:rabin
  7. """
  8. from demeter import *
  9. class Common(object):
  10. # 获取某个model下的列表数据
  11. def list(self, name, state = True, search = None, page=False):
  12. model = self.model(name)
  13. model.state = state
  14. if search:
  15. for key, value in search.items():
  16. if value:
  17. key = key.split('-')
  18. if hasattr(model, key[0]):
  19. attr = getattr(model, key[0])
  20. if hasattr(attr, key[2]):
  21. method = key[2]
  22. else:
  23. method = 'assgin'
  24. func = getattr(attr, method)
  25. func(value)
  26. data = model.select(page=page)
  27. return data
  28. # 获取某个model下的数据
  29. def one(self, name, **kwd):
  30. model = self.model(name)
  31. if kwd:
  32. for key,value in kwd.items():
  33. if hasattr(model, key):
  34. attr = getattr(model, key)
  35. func = getattr(attr, 'assgin')
  36. func(value)
  37. data = model.select(type='fetchone')
  38. return data
  39. # 更新
  40. def update(self, name, id, data):
  41. model = self.model(name)
  42. if id:
  43. model.id = id
  44. if 'cdate' not in data:
  45. data['cdate'] = 'time'
  46. model.update(data)
  47. return id
  48. else:
  49. for key, value in data.items():
  50. if hasattr(model, key):
  51. attr = getattr(model, key)
  52. method = 'assgin'
  53. if 'date' in key:
  54. method = 'time'
  55. func = getattr(attr, method)
  56. func(value)
  57. return model.insert()
  58. # 删除
  59. def delete(self, name, id, state = False):
  60. model = self.model(name)
  61. model.id = id
  62. return model.update(state=state)
  63. def model(self, name):
  64. return Demeter.model(name)