db.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. # -*- coding: utf-8 -*-
  2. import time
  3. ## if SSL/HTTPS is properly configured and you want all HTTP requests to
  4. ## be redirected to HTTPS, uncomment the line below:
  5. # request.requires_https()
  6. ## app configuration made easy. Look inside private/appconfig.ini
  7. from gluon.contrib.appconfig import AppConfig
  8. ## once in production, remove reload=True to gain full speed
  9. myconf = AppConfig(reload=True)
  10. if not request.env.web2py_runtime_gae:
  11. ## if NOT running on Google App Engine use SQLite or other DB
  12. db = DAL(myconf.take('db.uri'), pool_size=myconf.take('db.pool_size', cast=int), check_reserved=['all'])
  13. else:
  14. ## connect to Google BigTable (optional 'google:datastore://namespace')
  15. db = DAL('google:datastore+ndb')
  16. ## store sessions and tickets there
  17. session.connect(request, response, db=db)
  18. ## or store session in Memcache, Redis, etc.
  19. ## from gluon.contrib.memdb import MEMDB
  20. ## from google.appengine.api.memcache import Client
  21. ## session.connect(request, response, db = MEMDB(Client()))
  22. ## by default give a view/generic.extension to all actions from localhost
  23. ## none otherwise. a pattern can be 'controller/function.extension'
  24. response.generic_patterns = ['*'] if request.is_local else []
  25. ## choose a style for forms
  26. response.formstyle = myconf.take('forms.formstyle') # or 'bootstrap3_stacked' or 'bootstrap2' or other
  27. response.form_label_separator = myconf.take('forms.separator')
  28. ## (optional) optimize handling of static files
  29. # response.optimize_css = 'concat,minify,inline'
  30. # response.optimize_js = 'concat,minify,inline'
  31. ## (optional) static assets folder versioning
  32. # response.static_version = '0.0.0'
  33. #########################################################################
  34. ## Here is sample code if you need for
  35. ## - email capabilities
  36. ## - authentication (registration, login, logout, ... )
  37. ## - authorization (role based authorization)
  38. ## - services (xml, csv, json, xmlrpc, jsonrpc, amf, rss)
  39. ## - old style crud actions
  40. ## (more options discussed in gluon/tools.py)
  41. #########################################################################
  42. from gluon.tools import Auth, Service, PluginManager
  43. auth = Auth(db)
  44. service = Service()
  45. plugins = PluginManager()
  46. ## create all tables needed by auth if not custom tables
  47. auth.define_tables(username=False, signature=False, enable_tokens=False)
  48. ## configure email
  49. mail = auth.settings.mailer
  50. mail.settings.server = 'logging' if request.is_local else myconf.take('smtp.server')
  51. mail.settings.sender = myconf.take('smtp.sender')
  52. mail.settings.login = myconf.take('smtp.login')
  53. ## configure auth policy
  54. auth.settings.registration_requires_verification = False
  55. auth.settings.registration_requires_approval = False
  56. auth.settings.reset_password_requires_verification = True
  57. #########################################################################
  58. ## Define your tables below (or better in another model file) for example
  59. ##
  60. ## >>> db.define_table('mytable',Field('myfield','string'))
  61. ##
  62. ## Fields can be 'string','text','password','integer','double','boolean'
  63. ## 'date','time','datetime','blob','upload', 'reference TABLENAME'
  64. ## There is an implicit 'id integer autoincrement' field
  65. ## Consult manual for more options, validators, etc.
  66. ##
  67. ## More API examples for controllers:
  68. ##
  69. ## >>> db.mytable.insert(myfield='value')
  70. ## >>> rows=db(db.mytable.myfield=='value').select(db.mytable.ALL)
  71. ## >>> for row in rows: print row.id, row.myfield
  72. #########################################################################
  73. ## Tabelas para Imagens
  74. db.define_table(
  75. 'imagem',
  76. Field(
  77. 'arquivo',
  78. 'upload',
  79. required=True,
  80. uploadseparate=True,
  81. notnull=True,
  82. ),
  83. Field(
  84. 'data_upload',
  85. default=int(time.strftime('%s', time.localtime())),
  86. update=int(time.strftime('%s', time.localtime())),
  87. writable=False,
  88. readable=False,
  89. notnull=True,
  90. ),
  91. Field(
  92. 'autor',
  93. default='Alguém',
  94. ),
  95. Field(
  96. 'fonte',
  97. ),
  98. Field(
  99. 'licenca',
  100. requires=IS_IN_SET([
  101. ('cr','Todos direitos reservados'),
  102. ('pd','Domínio público'),
  103. ('cc0','CC Zero'),
  104. ('cc-by','CC Atribuição'),
  105. ('cc-by-sa','CC Atribuição-CompartilhaIgual'),
  106. ('cc-by-nd','CC Atribuição-SemDerivações'),
  107. ('cc-by-nc','CC Atribuição-NãoComercial'),
  108. ('cc-by-nc-sa','CC Atribuição-NãoComercial-CompartilhaIgual'),
  109. ('cc-by-nc-nd','CC Atribuição-SemDerivações-SemDerivados'),
  110. ]),
  111. ),
  112. Field(
  113. 'votos',
  114. 'integer',
  115. default=0,
  116. writable=False,
  117. readable=False,
  118. ),
  119. )
  120. db.imagem.arquivo.requires = IS_NOT_EMPTY()
  121. ## /Tabelas para Imagens
  122. ## Tabelas para Textos
  123. db.define_table(
  124. 'texto',
  125. Field(
  126. 'conteudo',
  127. 'text',
  128. required=True,
  129. notnull=True,
  130. ),
  131. Field(
  132. 'data_upload',
  133. default=int(time.strftime('%s', time.localtime())),
  134. update=int(time.strftime('%s', time.localtime())),
  135. writable=False,
  136. readable=False,
  137. notnull=True,
  138. ),
  139. Field(
  140. 'autor',
  141. default='Alguém',
  142. ),
  143. Field(
  144. 'fonte',
  145. ),
  146. Field(
  147. 'licenca',
  148. requires=IS_IN_SET([
  149. ('cr','Todos direitos reservados'),
  150. ('pd','Domínio público'),
  151. ('cc0','CC Zero'),
  152. ('cc-by','CC Atribuição'),
  153. ('cc-by-sa','CC Atribuição-CompartilhaIgual'),
  154. ('cc-by-nd','CC Atribuição-SemDerivações'),
  155. ('cc-by-nc','CC Atribuição-NãoComercial'),
  156. ('cc-by-nc-sa','CC Atribuição-NãoComercial-CompartilhaIgual'),
  157. ('cc-by-nc-nd','CC Atribuição-SemDerivações-SemDerivados'),
  158. ]),
  159. ),
  160. Field(
  161. 'votos',
  162. 'integer',
  163. default=0,
  164. writable=False,
  165. readable=False,
  166. ),
  167. )
  168. db.texto.conteudo.requires = IS_NOT_EMPTY()
  169. ## /Tabelas para Textos
  170. ## Tabela para Tags
  171. db.define_table(
  172. 'tag',
  173. Field(
  174. 'tag',
  175. 'string',
  176. required=True,
  177. notnull=True,
  178. ),
  179. )
  180. db.tag.tag.requires = IS_NOT_EMPTY()
  181. ## /Tabela para Tags
  182. ## Tabela para Votos
  183. db.define_table(
  184. 'voto',
  185. Field(
  186. 'id_user',
  187. 'integer',
  188. ),
  189. Field(
  190. 'id_item',
  191. 'integer',
  192. ),
  193. )
  194. ## /Tabela para Votos
  195. ## after defining tables, uncomment below to enable auditing
  196. auth.enable_record_versioning(db)