Skip to content

Instantly share code, notes, and snippets.

@toughrogrammer
Created June 19, 2016 07:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toughrogrammer/8798542471d170be7e74dd154259f1a4 to your computer and use it in GitHub Desktop.
Save toughrogrammer/8798542471d170be7e74dd154259f1a4 to your computer and use it in GitHub Desktop.
from app import api_root
from flask_restful import Resource, marshal_with, fields
class IPField(fields.Raw):
def format(self, value):
parts = value.split('.')
parts[1] = '***'
return '.'.join(parts)
document_list_fields = {
'id': fields.Integer(attribute='id'),
'title': fields.String(attribute='title', default='title of document'),
'content': fields.String(attribute='content'),
'ip': IPField(attribute='ip')
}
@api_root.resource('/documents')
class DocumentListAPI(Resource):
@marshal_with(document_list_fields, envelope='items')
def get(self):
items = [
{'id': 1, 'title': 'title1', 'content': 'content1', 'ip': '1.2.3.4'},
{'id': 2, 'title': None, 'content': 'content2', 'ip': '5.6.7.8'},
{'id': 3, 'title': 'title3', 'content': 'content3', 'ip': '9.10.11.12'}
]
return items
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment