Commits

Anonymous committed 336c94f

missing module

  • Participants
  • Parent commits 9544956

Comments (0)

Files changed (3)

 data
 build
 docs/output
+docs/_build
 docs/_themes
 
 .git

File docs/Makefile

+# Makefile for Sphinx documentation
+#
+
+# You can set these variables from the command line.
+SPHINXOPTS    =
+SPHINXBUILD   = sphinx-build
+PAPER         =
+
+# Internal variables.
+PAPEROPT_a4     = -D latex_paper_size=a4
+PAPEROPT_letter = -D latex_paper_size=letter
+ALLSPHINXOPTS   = -d _build/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) .
+
+.PHONY: help clean html web pickle htmlhelp latex changes linkcheck
+
+help:
+	@echo "Please use \`make <target>' where <target> is one of"
+	@echo "  html      to make standalone HTML files"
+	@echo "  pickle    to make pickle files (usable by e.g. sphinx-web)"
+	@echo "  htmlhelp  to make HTML files and a HTML help project"
+	@echo "  latex     to make LaTeX files, you can set PAPER=a4 or PAPER=letter"
+	@echo "  changes   to make an overview over all changed/added/deprecated items"
+	@echo "  linkcheck to check all external links for integrity"
+
+clean:
+	-rm -rf _build/*
+
+html: _themes/
+	mkdir -p _build/html _build/doctrees
+	$(SPHINXBUILD) -b html $(ALLSPHINXOPTS) _build/html
+	@echo
+	@echo "Build finished. The HTML pages are in _build/html."
+
+text:
+	mkdir -p _build/text _build/doctrees
+	$(SPHINXBUILD) -b text $(ALLSPHINXOPTS) _build/text
+	@echo
+	@echo "Build finished. The HTML pages are in _build/text."
+
+pickle:
+	mkdir -p _build/pickle _build/doctrees
+	$(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) _build/pickle
+	@echo
+	@echo "Build finished; now you can process the pickle files or run"
+	@echo "  sphinx-web _build/pickle"
+	@echo "to start the sphinx-web server."
+
+web: pickle
+
+htmlhelp: _themes
+	mkdir -p _build/htmlhelp _build/doctrees
+	$(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) _build/htmlhelp
+	@echo
+	@echo "Build finished; now you can run HTML Help Workshop with the" \
+	      ".hhp project file in _build/htmlhelp."
+
+latex:
+	mkdir -p _build/latex _build/doctrees
+	$(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) _build/latex
+	cp _static/*.png _build/latex
+	./convert_images.sh
+	cp _static/latex-warning.png _build/latex
+	cp _static/latex-note.png _build/latex
+	@echo
+	@echo "Build finished; the LaTeX files are in _build/latex."
+	@echo "Run \`make all-pdf' or \`make all-ps' in that directory to" \
+	      "run these through (pdf)latex."
+
+changes:
+	mkdir -p _build/changes _build/doctrees
+	$(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) _build/changes
+	@echo
+	@echo "The overview file is in _build/changes."
+
+linkcheck:
+	mkdir -p _build/linkcheck _build/doctrees
+	$(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) _build/linkcheck
+	@echo
+	@echo "Link check complete; look for any errors in the above output " \
+	      "or in _build/linkcheck/output.txt."
+
+epub:
+	$(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) _build/epub
+	@echo
+	@echo "Build finished. The epub file is in _build/epub."
+
+_themes:
+	git clone git://github.com/Pylons/pylons_sphinx_theme.git _themes

File pyramid_simpleform/form.py

+import colander
+
+
+class Form(object):
+
+
+    """
+    `request` : Pyramid request instance
+
+    `schema`  : FormEncode Schema class or instance
+
+    `validators` : a dict of FormEncode validators i.e. { field : validator }
+
+    `defaults`   : a dict of default values
+
+    `obj`        : instance of an object (e.g. SQLAlchemy model)
+
+    `method`        : HTTP method
+
+    Also note that values of ``obj`` supercede those of ``defaults``. Only
+    fields specified in your schema or validators will be taken from the 
+    object.
+    """
+
+
+    def __init__(self, request, schema=None, defaults=None, 
+                 obj=None, extra=None, include=None, exclude=None,
+                 method="POST", multipart=False):
+
+        self.request = request
+        self.schema = schema
+        self.method = method
+        self.multipart = multipart
+
+        self.is_validated = False
+
+        self.errors = {}
+        self.data = {}
+
+        if defaults:
+            self.data.update(defaults)
+
+        if obj:
+            fields = [node.name for node in self.schema]
+            for f in fields:
+                if hasattr(obj, f):
+                    self.data[f] = getattr(obj, f) 
+
+    def is_error(self, field):
+        """
+        Checks if individual field has errors.
+        """
+        return field in self.errors
+
+    def all_errors(self):
+        """
+        Returns all errors in a single list.
+        """
+        return self.errors.values()
+
+    def errors_for(self, field):
+        """
+        Returns any errors for a given field as a list.
+        """
+        errors = self.errors.get(field, [])
+        if isinstance(errors, basestring):
+            errors = [errors]
+        return errors
+
+    def validate(self, force_validate=False, params=None):
+        """
+        Runs validation and returns True/False whether form is 
+        valid.
+        
+        This will check if the form should be validated (i.e. the
+        request method matches) and the schema/validators validate.
+
+        Validation will only be run once; subsequent calls to 
+        validate() will have no effect, i.e. will just return
+        the original result.
+
+        The errors and data values will be updated accordingly.
+
+        `force_validate`  : will run validation regardless of request method.
+
+        `params`          : dict or MultiDict of params. By default 
+        will use **request.POST** (if HTTP POST) or **request.params**.
+        """
+
+        if self.is_validated:
+            return not(self.errors)
+
+        if not force_validate:
+            if self.method and self.method != self.request.method:
+                return False
+
+        if params is None:
+            if self.method == "POST":
+                params = self.request.POST
+            else:
+                params = self.request.params
+            
+        self.data.update(params)
+
+        if self.schema:
+            try:
+                self.data = self.schema.deserialize(params)
+            except colander.Invalid, e:
+                self.errors = e.asdict()
+
+        self.is_validated = True
+
+        return not(self.errors)
+
+    def bind(self, obj, include=None, exclude=None):
+        """
+        Binds validated field values to an object instance, for example a
+        SQLAlchemy model instance.
+
+        `include` : list of included fields. If field not in this list it 
+        will not be bound to this object.
+
+        `exclude` : list of excluded fields. If field is in this list it 
+        will not be bound to the object.
+
+        Returns the `obj` passed in.
+
+        Note that any properties starting with underscore "_" are ignored
+        regardless of ``include`` and ``exclude``. If you need to set these
+        do so manually from the ``data`` property of the form instance.
+
+        Calling bind() before running validate() will result in a RuntimeError
+        """
+
+        if not self.is_validated:
+            raise RuntimeError, \
+                    "Form has not been validated. Call validate() first"
+
+        if self.errors:
+            raise RuntimeError, "Cannot bind to object if form has errors"
+
+        items = [(k, v) for k, v in self.data.items() if not k.startswith("_")]
+        for k, v in items:
+
+            if include and k not in include:
+                continue
+
+            if exclude and k in exclude:
+                continue
+
+            setattr(obj, k, v)
+
+        return obj
+
+