Advertisement
Guest User

Untitled

a guest
Aug 29th, 2013
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.82 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2.  
  3. """
  4. eve-demo settings
  5. ~~~~~~~~~~~~~~~~~
  6.  
  7. Settings file for our little demo.
  8.  
  9. PLEASE NOTE: We don't need to create the two collections in MongoDB.
  10. Actually, we don't even need to create the database: GET requests on an
  11. empty/non-existant DB will be served correctly ('200' OK with an empty
  12. collection); DELETE/PATCH will receive appropriate responses ('404' Not
  13. Found), and POST requests will create database and collections when needed.
  14. Keep in mind however that such an auto-managed database will most likely
  15. perform poorly since it lacks any sort of optimized index.
  16.  
  17. :copyright: (c) 2012 by Nicola Iarocci.
  18. :license: BSD, see LICENSE for more details.
  19. """
  20.  
  21. import os
  22.  
  23. # We want to seamlessy run our API both locally and on Heroku so:
  24. if os.environ.get('PORT'):
  25. # We're hosted on Heroku! Use the MongoHQ sandbox as our backend.
  26. MONGO_HOST = 'localhost'
  27. MONGO_PORT = 27017
  28. MONGO_USERNAME = 'user'
  29. MONGO_PASSWORD = 'user'
  30. MONGO_DBNAME = 'test'
  31.  
  32. # also, correctly set the API entry point
  33. SERVER_NAME = 'localhost'
  34. else:
  35. # Running on local machine. Let's just use the local mongod instance.
  36. MONGO_HOST = '192.168.1.149'
  37. MONGO_PORT = 27017
  38. MONGO_USERNAME = 'user'
  39. MONGO_PASSWORD = 'user'
  40. MONGO_DBNAME = 'test'
  41.  
  42. # let's not forget the API entry point
  43. SERVER_NAME = 'http://192.168.1.149:5000'
  44.  
  45.  
  46. # Enable reads (GET), inserts (POST) and DELETE for resources/collections
  47. # (if you omit this line, the API will default to ['GET'] and provide
  48. # read-only access to the endpoint).
  49. RESOURCE_METHODS = ['GET', 'POST', 'DELETE']
  50.  
  51. # Enable reads (GET), edits (PATCH) and deletes of individual items
  52. # (defaults to read-only item access).
  53. ITEM_METHODS = ['GET', 'PATCH', 'DELETE']
  54.  
  55. # We enable standard client cache directives for all resources exposed by the
  56. # API. We can always override these global settings later.
  57. CACHE_CONTROL = 'max-age=20'
  58. CACHE_EXPIRES = 20
  59.  
  60. # Our API will expose two resources (MongoDB collections): 'people' and
  61. # 'works'. In order to allow for proper data validation, we define beaviour
  62. # and structure.
  63. people = {
  64. # 'title' tag used in item links.
  65. 'item_title': 'person',
  66.  
  67. # by default the standard item entry point is defined as
  68. # '/people/<ObjectId>/'. We leave it untouched, and we also enable an
  69. # additional read-only entry point. This way consumers can also perform GET
  70. # requests at '/people/<lastname>/'.
  71. 'additional_lookup': {
  72. 'url': '[\w]+',
  73. 'field': 'lastname'
  74. },
  75.  
  76. # Schema definition, based on Cerberus grammar. Check the Cerberus project
  77. # (https://github.com/nicolaiarocci/cerberus) for details.
  78. 'schema': {
  79. 'firstname': {
  80. 'type': 'string',
  81. 'minlength': 1,
  82. 'maxlength': 10,
  83. },
  84. 'lastname': {
  85. 'type': 'string',
  86. 'minlength': 1,
  87. 'maxlength': 15,
  88. 'required': True,
  89. # talk about hard constraints! For the purpose of the demo
  90. # 'lastname' is an API entry-point, so we need it to be unique.
  91. 'unique': True,
  92. },
  93. # 'role' is a list, and can only contain values from 'allowed'.
  94. 'role': {
  95. 'type': 'list',
  96. 'allowed': ["author", "contributor", "copy"],
  97. },
  98. # An embedded 'strongly-typed' dictionary.
  99. 'location': {
  100. 'type': 'dict',
  101. 'schema': {
  102. 'address': {'type': 'string'},
  103. 'city': {'type': 'string'}
  104. },
  105. },
  106. 'born': {
  107. 'type': 'datetime',
  108. },
  109. }
  110. }
  111.  
  112. works = {
  113. # if 'item_title' is not provided Eve will just strip the final
  114. # 's' from resource name, and use it as the item_title.
  115. #'item_title': 'work',
  116.  
  117. # We choose to override global cache-control directives for this resource.
  118. 'cache_control': 'max-age=10,must-revalidate',
  119. 'cache_expires': 10,
  120.  
  121. 'schema': {
  122. 'title': {
  123. 'type': 'string',
  124. 'required': True,
  125. },
  126. 'description': {
  127. 'type': 'string',
  128. },
  129. 'owner': {
  130. 'type': 'objectid',
  131. 'required': True,
  132. # referential integrity constraint: value must exist in the
  133. # 'people' collection. Since we aren't declaring a 'field' key,
  134. # will default to `people._id` (or, more precisely, to whatever
  135. # ID_FIELD value is).
  136. 'data_relation': {
  137. 'collection': 'people'
  138. }
  139. },
  140. }
  141. }
  142.  
  143. # The DOMAIN dict explains which resources will be available and how they will
  144. # be accessible to the API consumer.
  145. DOMAIN = {
  146. 'people': people,
  147. 'works': works,
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement