{ "info": { "author": "winkidney", "author_email": "winkidney@gmail.com", "bugtrack_url": null, "classifiers": [ "Programming Language :: Python", "Topic :: Internet :: WWW/HTTP", "Topic :: Software Development :: Libraries", "Topic :: Utilities" ], "description": "Web Utils\n------------\n\nWeb development utils classes and functions. \nCurrent status: Under development and works in my project. \nFind me in http://github.com/winkidney/web_utils. \n\nSummaries are listed below.Documents will be written later. \n\n##Install\n```\npip install \"web_utils[forms,security,sqlalchemy]\" # to install all requirements.\n```\n\n##Summaries\n\n+ code_loader - load code from str or unicode object and save it to cache or database\n+ forms - some custom validator for wtfomrs\n+ json_form - wrapper for jsonschema to validate json string like wtfomrs.\n+ Security - password generator by bcrypt.Simple url sign generator.\n+ _sqlalchemy - DBFieldConverter for convert alchemy's model instance to dict like string with white list support.\n+ _pyramid - tools form pyramid web framework\n+ extra - some other utils.\n\n\n##web_utils.code_loader\n\nCreate a Code loader instance to load and save code from Storage or str object. \nReturn a python module object that you can run any code within it. \n\n+ Methods\n + __init__(name, storage_backend=DummyStorageBackend, cache_backend=DummyCacheBackend)\n Create a loader with given storage_backend and cache_backend.\n + create_module(fullname, code_script, save_key=None)\n if `save_key` is given, use it instead of fullname as `accees_key` passed to storage backend's `set` method.\n + save(mod, cached=False, **kwargs)\n save a module object into storage backend and cache backend(optional).\n **kwargs will be passed to backend's `set` method.\n + load(fullname, save_key=None, **kwargs)\n Load a module by its name(if `save_key` is given, use it instead).\n\n##web_utils.forms\nWTFroms's custom field and other validator \n\n+ validators\n + uuid_validator\n + TimeAfterNow validator - return `True` if current datetime-field later than now.\n + MobilePhone validataor - Check if a number sequence is china's phone number\n+ custom field\n + UnixTimeField\n + TextArrayField - convert `list` like `a, b, c ,1 ,3 ,3` to python list.\n + JsonField - Check if the text is json format string and convert it into python data(with `json.loads`)\n + IntArrayField - Convert list like `1,2,3,4,5` into python list(consists of python's `int` object)\n+ utils\n + json2form - convert python dict into `MultiDict` which can be read by wtforms.\n\n##web_utils.json_forms\nValidate json string or dict object in wtforms's way.\nJust inherit `JsonForm` class and call `validate` method to do validation.\nValidation by `JsonSchema` , [Validation Quick Start](http://json-schema.org/latest/json-schema-validation.html).\nExample listed below:\n```python\nclass NewPMSSchema(JsonForm):\n schema = {\n \"type\": \"object\",\n \"properties\": {\n \"to_uid\": {\n \"type\": \"number\",\n },\n \"content\": {\n \"type\": \"string\",\n },\n \"test\": {\n \"type\": \"object\",\n \"properties\": {\n \"test1\": {\n \"type\": \"integer\",\n }\n }\n },\n },\n \"required\": ['to_uid', 'content'],\n }\n\nform = NewPMSSchema({'to_uid': 'a', 'content': 1})\n# result\nresult = form.validate()\n# errors\nif not result:\n print form.errors\n```\n\n##web_utils.security\nNot recommend to use it. \n\n##web_utils._sqlalchemy\nA sqlalchemy model to json data converter with white list and custom converter support. \n\n###DBFCMixin\nJust inherit it in your sqlalchemy model and call `as_dict` method to output python dict. \n`class._default_output` is required. \nExample listed below: \n\n```python\nclass APIStorage(Base, DBFCMixin, StorageBackendMixin):\n\n __tablename__ = \"api_storage\"\n # white list\n _default_output = ('id', 'category', 'resource_name', 'document')\n\n id = Column(Integer, primary_key=True, autoincrement=True)\n category = Column(Text, nullable=False)\n resource_name = Column(Text, nullable=False)\n body = Column(Text, nullable=False)\n ctime = Column(DateTime, default=datetime.datetime.utcnow)\n\n# call as_dict method\nmodel_instance = dbsession.query(APIStorage).first()\nmodel_instance.as_dict(pure=False) #False is default , with white list support.\n\n```\n\n###DBFieldConverter\nLow level API for DBFCMixin \nQuick example:\n\n```python\nfrom sqlalchemy.ext.declarative import declarative_base\n\nBase = declarative_base()\n\nclass APIStorage(Base):\n\n __tablename__ = \"api_storage\"\n _default_output = ('id', 'category', 'resource_name', 'document')\n\n id = Column(Integer, primary_key=True, autoincrement=True)\n category = Column(Text, nullable=False)\n resource_name = Column(Text, nullable=False)\n body = Column(Text, nullable=False)\n ctime = Column(DateTime, default=datetime.datetime.utcnow)\n\n# convert and output\n\nmodel_instance = dbsession.query(APIStorage).first()\n\nconverter = DBFC(model_instance, model_instance._default_output)\n\nconverter.as_dict()\n\n# as list\nconverter.as_list()\n\n# without white list(output all data field)\nconverter.as_dict(pure=True)\n```\n\n####Register a Converter\nDBFieldConverter supports Converter by sqlalchemy's field type.\n```python\nfrom sqlalchemy import Text\nDBFieldConverter.register(Text)\n```\nwill register Text type in DBFC.\n Note: without registered, DBFieldConverter will print `warning` information in console.\n\n```python\nDBFieldConverter.register(Text, lambda x:x[-1])\n```\nwill output the last char of the input field which type is `Text`.\n\nYou can also pass `registry` to DBFieldConverter `__init__` method to specify `field type converter`.\nFor example:\n```python\nfrom sqlalchemy import Text\nconverter = DBFC(model_instance, model_instance._default_output, registry={Text: lambda x: x[-1]})\n```\n**Note:** This registry will not affect other instance's registry of DBFieldConverter since it's `instance registry`. \nOtherwise, `DBFC.register` register class converter in it's registry, every instance will be affected.\n\n###form2model\nEasy set `wtfomrs` `Form` data to sqlalchemy's model field, name by name.\n\n```python\nform2model(form, model_instance, exclude=None)\n```\n\n\n##web_utils.extra\nSome utils about argument parse ,datetime format.\n+ GetSingleArgument \n + bool(cls, value, default=False) default value support, will not return None.\n + integer(cls, value, default, nmin=None, nmax=None) - parse integer from string, return default value if not in given range.\n + string(cls, value, default='') - default value support\n+ format_timestamp \n from tornado core, to format timestamp\n ```python\n >>> format_timestamp(1359312200)\n 'Sun, 27 Jan 2013 18:43:20 GMT'\n ```\n+ AttrDict \n Simple wrapper for `attr dict` \n ```\n d = AttrDict({'a': 1})\n d.a # output 1\n ``` \n Many problem when use it, pay attention before you really know what you are doing.\n\n\n\n2015-03-19 fix bug in `JsonForm` and add integer and string auto-converter in `JsonForm`\n2015-02-13 update readme.", "description_content_type": null, "docs_url": null, "download_url": "UNKNOWN", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "http://github.com/winkidney/web_utils", "keywords": "web development utils", "license": "GPLv3", "maintainer": null, "maintainer_email": null, "name": "web_utils", "package_url": "https://pypi.org/project/web_utils/", "platform": "UNKNOWN", "project_url": "https://pypi.org/project/web_utils/", "project_urls": { "Download": "UNKNOWN", "Homepage": "http://github.com/winkidney/web_utils" }, "release_url": "https://pypi.org/project/web_utils/0.0.5/", "requires_dist": null, "requires_python": null, "summary": "web_utils collection that used in web development process.", "version": "0.0.5" }, "last_serial": 1656190, "releases": { "0.0.2": [ { "comment_text": "", "digests": { "md5": "8f99b168b93083a88e518cac9c740919", "sha256": "ef8c9251201ebc776450927010754145ebe8a7c44e615da336a9b14af274eb5e" }, "downloads": -1, "filename": "web_utils-0.0.2-py2-none-any.whl", "has_sig": false, "md5_digest": "8f99b168b93083a88e518cac9c740919", "packagetype": "bdist_wheel", "python_version": "2.7", "requires_python": null, "size": 15105, "upload_time": "2015-02-13T14:26:04", "url": "https://files.pythonhosted.org/packages/c7/71/fe3d0abacbeaf1ef04dbc7b67fd1bafe5d4c927d11cc1f56e36f3c0427c7/web_utils-0.0.2-py2-none-any.whl" } ], "0.0.3": [ { "comment_text": "", "digests": { "md5": "17ff6c9dcf72c90b93a40940f90b5713", "sha256": "8a92ae583afc5efc3612b1cb6a7619404597261161704ed60f48baa61d130e0a" }, "downloads": -1, "filename": "web_utils-0.0.3-py2-none-any.whl", "has_sig": false, "md5_digest": "17ff6c9dcf72c90b93a40940f90b5713", "packagetype": "bdist_wheel", "python_version": "2.7", "requires_python": null, "size": 18933, "upload_time": "2015-02-26T13:52:27", "url": "https://files.pythonhosted.org/packages/e9/93/656b6a666581cf5e7cf913a0fab18af445a9565c07e4973cd49255ea29ee/web_utils-0.0.3-py2-none-any.whl" } ], "0.0.4": [ { "comment_text": "", "digests": { "md5": "15aa56053185a3e30b01692778b5b64f", "sha256": "33a8416adbfb376356af663d8778fb4961005dfe7ff1f07d4f509f00f0e0580e" }, "downloads": -1, "filename": "web_utils-0.0.4-py2-none-any.whl", "has_sig": false, "md5_digest": "15aa56053185a3e30b01692778b5b64f", "packagetype": "bdist_wheel", "python_version": "2.7", "requires_python": null, "size": 19107, "upload_time": "2015-03-19T12:53:14", "url": "https://files.pythonhosted.org/packages/6a/75/b3749dca6308113798821db3003aa1d5a5a4e54b797c91e54543946b85b6/web_utils-0.0.4-py2-none-any.whl" } ], "0.0.5": [ { "comment_text": "", "digests": { "md5": "b22e66e42663a1257c57094ea26eaebd", "sha256": "67d478f74d640ee10cf854b32b151389e2beaa82d87dfe10dfad17ffb613b4fb" }, "downloads": -1, "filename": "web_utils-0.0.5-py2-none-any.whl", "has_sig": false, "md5_digest": "b22e66e42663a1257c57094ea26eaebd", "packagetype": "bdist_wheel", "python_version": "2.6", "requires_python": null, "size": 19814, "upload_time": "2015-07-30T06:42:55", "url": "https://files.pythonhosted.org/packages/75/d3/461a1012bc42ff57d4bc2d80e66432052175213884739dc67de15534ae80/web_utils-0.0.5-py2-none-any.whl" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "b22e66e42663a1257c57094ea26eaebd", "sha256": "67d478f74d640ee10cf854b32b151389e2beaa82d87dfe10dfad17ffb613b4fb" }, "downloads": -1, "filename": "web_utils-0.0.5-py2-none-any.whl", "has_sig": false, "md5_digest": "b22e66e42663a1257c57094ea26eaebd", "packagetype": "bdist_wheel", "python_version": "2.6", "requires_python": null, "size": 19814, "upload_time": "2015-07-30T06:42:55", "url": "https://files.pythonhosted.org/packages/75/d3/461a1012bc42ff57d4bc2d80e66432052175213884739dc67de15534ae80/web_utils-0.0.5-py2-none-any.whl" } ] }