From 36835b78d310a01f6f765549025662dafe4b65f6 Mon Sep 17 00:00:00 2001 From: Rafael Ferreira Date: Thu, 21 Nov 2013 23:34:28 -0200 Subject: [PATCH] added to Git --- app.yaml | 37 ++ deliveryservice.py | 39 ++ djangoforms.py | 989 ++++++++++++++++++++++++++++++++++++++ favicon.ico | Bin 0 -> 1150 bytes forms.py | 32 ++ index.yaml | 11 + main.py | 46 ++ models.py | 21 + services.py | 14 + settings.py | 66 +++ templates/deliverfee.html | 17 + templates/index.html | 14 + templates/signup.html | 17 + templates/template.html | 42 ++ templates/welcome.html | 9 + urls.py | 33 ++ views.py | 180 +++++++ 17 files changed, 1567 insertions(+) create mode 100644 app.yaml create mode 100644 deliveryservice.py create mode 100644 djangoforms.py create mode 100644 favicon.ico create mode 100644 forms.py create mode 100644 index.yaml create mode 100644 main.py create mode 100644 models.py create mode 100644 services.py create mode 100644 settings.py create mode 100644 templates/deliverfee.html create mode 100644 templates/index.html create mode 100644 templates/signup.html create mode 100644 templates/template.html create mode 100644 templates/welcome.html create mode 100644 urls.py create mode 100644 views.py diff --git a/app.yaml b/app.yaml new file mode 100644 index 0000000..1975954 --- /dev/null +++ b/app.yaml @@ -0,0 +1,37 @@ +application: vaifreteapp +version: 1 +runtime: python27 +api_version: 1 +threadsafe: no + +libraries: +- name: django + version: "1.4" + +builtins: +- django_wsgi: on + +handlers: + +- url: /css + static_dir: css + +- url: /js + static_dir: js + +- url: /images + static_dir: images + +- url: /admin/.* + script: main.py + login: admin + +- url: /app/.* + script: main.py + login: required + +- url: /DeliveryService.* + script: services.py + +- url: .* + script: main.py \ No newline at end of file diff --git a/deliveryservice.py b/deliveryservice.py new file mode 100644 index 0000000..9737f50 --- /dev/null +++ b/deliveryservice.py @@ -0,0 +1,39 @@ +import datetime +import time +import logging +from protorpc import remote +from protorpc import messages +from models import * + +class Delivery(messages.Message): + destination_address = messages.StringField(1, required=True) + dest_lat = messages.StringField(2) + dest_lng = messages.StringField(3) + source_address = messages.StringField(4) + source_lat = messages.StringField(5) + source_lng =messages.StringField(6) + request_user = messages.StringField(7) + +class Deliveries(messages.Message): + deliveries = messages.MessageField(Delivery, 1, repeated=True) + +class GetDeliveriesRequest(messages.Message): + limit = messages.IntegerField(1, default=10) + user = messages.StringField(2) + +class DeliveryService(remote.Service): + @remote.method(GetDeliveriesRequest, Deliveries) + def get_deliveries(self, request): + logging.debug('Received request ' + str(request)) + + query = DeliverFee.all().order('-request_date_time') + + #if request.user: + #query.filter('date <=', when) + + deliveries = [] + for delivery_model in query.fetch(request.limit): + delivery = Delivery(destination_address=delivery_model.destination_address, dest_lat=delivery_model.dest_lat, dest_lng=delivery_model.dest_lng, source_address=delivery_model.source_address, source_lat=delivery_model.source_lat, source_lng=delivery_model.source_lng, request_user=delivery_model.request_user.email()) + deliveries.append(delivery) + + return Deliveries(deliveries=deliveries) \ No newline at end of file diff --git a/djangoforms.py b/djangoforms.py new file mode 100644 index 0000000..fbbc0bc --- /dev/null +++ b/djangoforms.py @@ -0,0 +1,989 @@ +#!/usr/bin/env python +# +# Copyright 2007 Google Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + + + + +"""Support for creating Django (new) forms from Datastore data models. + +This is our best shot at supporting as much of Django as possible: you +won't be able to use Django's db package, but you can use our +db package instead, and create Django forms from it, either fully +automatically, or with overrides. + +Note, you should not import these classes from this module. Importing +this module patches the classes in place, and you should continue to +import them from google.appengine.db. + +Some of the code here is strongly inspired by Django's own ModelForm +class (new in Django 0.97). Our code also supports Django 0.96 (so as +to be maximally compatible). Note that our API is always similar to +Django 0.97's API, even when used with Django 0.96 (which uses a +different API, chiefly form_for_model()). + +Terminology notes: + - forms: always refers to the Django newforms subpackage + - field: always refers to a Django forms.Field instance + - property: always refers to a db.Property instance + +Mapping between properties and fields: + ++====================+===================+==============+====================+ +| Property subclass | Field subclass | datatype | widget; notes | ++====================+===================+==============+====================+ +| StringProperty | CharField | unicode | Textarea | +| | | | if multiline | ++--------------------+-------------------+--------------+--------------------+ +| TextProperty | CharField | unicode | Textarea | ++--------------------+-------------------+--------------+--------------------+ +| BlobProperty | FileField | str | skipped in v0.96 | ++--------------------+-------------------+--------------+--------------------+ +| DateTimeProperty | DateTimeField | datetime | skipped | +| | | | if auto_now[_add] | ++--------------------+-------------------+--------------+--------------------+ +| DateProperty | DateField | date | ditto | ++--------------------+-------------------+--------------+--------------------+ +| TimeProperty | TimeField | time | ditto | ++--------------------+-------------------+--------------+--------------------+ +| IntegerProperty | IntegerField | int or long | | ++--------------------+-------------------+--------------+--------------------+ +| FloatProperty | FloatField | float | CharField in v0.96 | ++--------------------+-------------------+--------------+--------------------+ +| BooleanProperty | BooleanField | bool | | ++--------------------+-------------------+--------------+--------------------+ +| UserProperty | CharField | users.User | | ++--------------------+-------------------+--------------+--------------------+ +| StringListProperty | CharField | list of str | Textarea | ++--------------------+-------------------+--------------+--------------------+ +| LinkProperty | URLField | str | | ++--------------------+-------------------+--------------+--------------------+ +| ReferenceProperty | ModelChoiceField* | db.Model | | ++--------------------+-------------------+--------------+--------------------+ +| _ReverseReferenceP.| None | | always skipped | ++====================+===================+==============+====================+ + +Notes: +*: this Field subclasses is defined by us, not in Django. +""" + + + + + + + + +import itertools +import logging + + + + + +import django.core.exceptions +import django.utils.datastructures + + + +try: + from django import newforms as forms + have_uploadedfile = False +except ImportError: + from django import forms + from django.core.files import uploadedfile + have_uploadedfile = True + + + +try: + from django.utils.translation import ugettext_lazy as _ +except ImportError: + pass + + +from google.appengine.api import users +from google.appengine.ext import db + + + + + + +def monkey_patch(name, bases, namespace): + """A 'metaclass' for adding new methods to an existing class. + + This shouldn't be used to override existing methods. However, + because loading this module (like loading any module) should be + idempotent, we don't assert that. + + Usage example: + + class PatchClass(TargetClass): + __metaclass__ = monkey_patch + def foo(self, ...): ... + def bar(self, ...): ... + + This is equivalent to: + + def foo(self, ...): ... + def bar(self, ...): ... + TargetClass.foo = foo + TargetClass.bar = bar + PatchClass = TargetClass + + Note that PatchClass becomes an alias for TargetClass; by convention + it is recommended to give PatchClass the same name as TargetClass. + """ + + + assert len(bases) == 1, 'Exactly one base class is required' + base = bases[0] + for name, value in namespace.iteritems(): + if name not in ('__metaclass__', '__module__'): + setattr(base, name, value) + return base + + + + + + + +class Property(db.Property): + __metaclass__ = monkey_patch + + def get_form_field(self, form_class=forms.CharField, **kwargs): + """Return a Django form field appropriate for this property. + + Args: + form_class: a forms.Field subclass, default forms.CharField + + Additional keyword arguments are passed to the form_class constructor, + with certain defaults: + required: self.required + label: prettified self.verbose_name, if not None + widget: a forms.Select instance if self.choices is non-empty + initial: self.default, if not None + + Returns: + A fully configured instance of form_class, or None if no form + field should be generated for this property. + """ + defaults = {'required': self.required} + if self.verbose_name: + defaults['label'] = self.verbose_name.capitalize().replace('_', ' ') + if self.choices: + choices = [] + if not self.required or (self.default is None and + 'initial' not in kwargs): + choices.append(('', '---------')) + for choice in self.choices: + choices.append((str(choice), unicode(choice))) + defaults['widget'] = forms.Select(choices=choices) + if self.default is not None: + defaults['initial'] = self.default + defaults.update(kwargs) + return form_class(**defaults) + + def get_value_for_form(self, instance): + """Extract the property value from the instance for use in a form. + + Override this to do a property- or field-specific type conversion. + + Args: + instance: a db.Model instance + + Returns: + The property's value extracted from the instance, possibly + converted to a type suitable for a form field; possibly None. + + By default this returns the instance attribute's value unchanged. + """ + return getattr(instance, self.name) + + def make_value_from_form(self, value): + """Convert a form value to a property value. + + Override this to do a property- or field-specific type conversion. + + Args: + value: the cleaned value retrieved from the form field + + Returns: + A value suitable for assignment to a model instance's property; + possibly None. + + By default this converts the value to self.data_type if it + isn't already an instance of that type, except if the value is + empty, in which case we return None. + """ + if value in (None, ''): + return None + if not isinstance(value, self.data_type): + value = self.data_type(value) + return value + + +class UserProperty(db.UserProperty): + __metaclass__ = monkey_patch + + def get_form_field(self, **kwargs): + """Return a Django form field appropriate for a User property. + + This defaults to a forms.EmailField instance, except if auto_current_user or + auto_current_user_add is set, in which case None is returned, as such + 'auto' fields should not be rendered as part of the form. + """ + if self.auto_current_user or self.auto_current_user_add: + return None + defaults = {'form_class': forms.EmailField} + defaults.update(kwargs) + return super(UserProperty, self).get_form_field(**defaults) + + def get_value_for_form(self, instance): + """Extract the property value from the instance for use in a form. + + This returns the email address of the User. + """ + value = super(UserProperty, self).get_value_for_form(instance) + if not value: + return None + return value.email() + + +class StringProperty(db.StringProperty): + __metaclass__ = monkey_patch + + def get_form_field(self, **kwargs): + """Return a Django form field appropriate for a string property. + + This sets the widget default to forms.Textarea if the property's + multiline attribute is set. + """ + defaults = {} + if self.multiline: + defaults['widget'] = forms.Textarea + defaults.update(kwargs) + return super(StringProperty, self).get_form_field(**defaults) + + +class TextProperty(db.TextProperty): + __metaclass__ = monkey_patch + + def get_form_field(self, **kwargs): + """Return a Django form field appropriate for a text property. + + This sets the widget default to forms.Textarea. + """ + defaults = {'widget': forms.Textarea} + defaults.update(kwargs) + return super(TextProperty, self).get_form_field(**defaults) + + +class BlobProperty(db.BlobProperty): + __metaclass__ = monkey_patch + + def __init__(self, *args, **kwargs): + super(BlobProperty, self).__init__(*args, **kwargs) + self.form_value = None + + def get_form_field(self, **kwargs): + """Return a Django form field appropriate for a blob property. + + This defaults to a forms.FileField instance when using Django 0.97 + or later. For 0.96 this returns None, as file uploads are not + really supported in that version. + """ + if not hasattr(forms, 'FileField'): + return None + defaults = {'form_class': forms.FileField} + defaults.update(kwargs) + return super(BlobProperty, self).get_form_field(**defaults) + + def get_value_for_form(self, instance): + """Extract the property value from the instance for use in a form. + + There is no way to convert a Blob into an initial value for a file + upload, so we always return None. + """ + return None + + def make_value_from_form(self, value): + """Convert a form value to a property value. + + This extracts the content from the UploadedFile instance returned + by the FileField instance. + """ + if have_uploadedfile and isinstance(value, uploadedfile.UploadedFile): + if not self.form_value: + self.form_value = value.read() + b = db.Blob(self.form_value) + return b + return super(BlobProperty, self).make_value_from_form(value) + + +class DateTimeProperty(db.DateTimeProperty): + __metaclass__ = monkey_patch + + def get_form_field(self, **kwargs): + """Return a Django form field appropriate for a date-time property. + + This defaults to a DateTimeField instance, except if auto_now or + auto_now_add is set, in which case None is returned, as such + 'auto' fields should not be rendered as part of the form. + """ + if self.auto_now or self.auto_now_add: + return None + defaults = {'form_class': forms.DateTimeField} + defaults.update(kwargs) + return super(DateTimeProperty, self).get_form_field(**defaults) + + +class DateProperty(db.DateProperty): + __metaclass__ = monkey_patch + + def get_form_field(self, **kwargs): + """Return a Django form field appropriate for a date property. + + This defaults to a DateField instance, except if auto_now or + auto_now_add is set, in which case None is returned, as such + 'auto' fields should not be rendered as part of the form. + """ + if self.auto_now or self.auto_now_add: + return None + defaults = {'form_class': forms.DateField} + defaults.update(kwargs) + return super(DateProperty, self).get_form_field(**defaults) + + +class TimeProperty(db.TimeProperty): + __metaclass__ = monkey_patch + + def get_form_field(self, **kwargs): + """Return a Django form field appropriate for a time property. + + This defaults to a TimeField instance, except if auto_now or + auto_now_add is set, in which case None is returned, as such + 'auto' fields should not be rendered as part of the form. + """ + if self.auto_now or self.auto_now_add: + return None + defaults = {'form_class': forms.TimeField} + defaults.update(kwargs) + return super(TimeProperty, self).get_form_field(**defaults) + + +class IntegerProperty(db.IntegerProperty): + __metaclass__ = monkey_patch + + def get_form_field(self, **kwargs): + """Return a Django form field appropriate for an integer property. + + This defaults to an IntegerField instance. + """ + defaults = {'form_class': forms.IntegerField} + defaults.update(kwargs) + return super(IntegerProperty, self).get_form_field(**defaults) + + +class FloatProperty(db.FloatProperty): + __metaclass__ = monkey_patch + + def get_form_field(self, **kwargs): + """Return a Django form field appropriate for an integer property. + + This defaults to a FloatField instance when using Django 0.97 or + later. For 0.96 this defaults to the CharField class. + """ + defaults = {} + if hasattr(forms, 'FloatField'): + defaults['form_class'] = forms.FloatField + defaults.update(kwargs) + return super(FloatProperty, self).get_form_field(**defaults) + + +class BooleanProperty(db.BooleanProperty): + __metaclass__ = monkey_patch + + def get_form_field(self, **kwargs): + """Return a Django form field appropriate for a boolean property. + + This defaults to a BooleanField. + """ + defaults = {'form_class': forms.BooleanField} + defaults.update(kwargs) + return super(BooleanProperty, self).get_form_field(**defaults) + + def make_value_from_form(self, value): + """Convert a form value to a property value. + + This is needed to ensure that False is not replaced with None. + """ + if value is None: + return None + if isinstance(value, basestring) and value.lower() == 'false': + + + return False + return bool(value) + + +class StringListProperty(db.StringListProperty): + __metaclass__ = monkey_patch + + + + + + def get_form_field(self, **kwargs): + """Return a Django form field appropriate for a StringList property. + + This defaults to a Textarea widget with a blank initial value. + """ + defaults = {'widget': forms.Textarea, + 'initial': ''} + defaults.update(kwargs) + return super(StringListProperty, self).get_form_field(**defaults) + + def get_value_for_form(self, instance): + """Extract the property value from the instance for use in a form. + + This joins a list of strings with newlines. + """ + value = super(StringListProperty, self).get_value_for_form(instance) + if not value: + return None + if isinstance(value, list): + value = '\n'.join(value) + return value + + def make_value_from_form(self, value): + """Convert a form value to a property value. + + This breaks the string into lines. + """ + if not value: + return [] + if isinstance(value, basestring): + value = value.splitlines() + return value + + +class LinkProperty(db.LinkProperty): + __metaclass__ = monkey_patch + + def get_form_field(self, **kwargs): + """Return a Django form field appropriate for a URL property. + + This defaults to a URLField instance. + """ + defaults = {'form_class': forms.URLField} + defaults.update(kwargs) + return super(LinkProperty, self).get_form_field(**defaults) + + +class _WrapIter(object): + """Helper class whose iter() calls a given function to get an iterator.""" + + def __init__(self, function): + self._function = function + + def __iter__(self): + return self._function() + + +class ModelChoiceField(forms.Field): + + default_error_messages = { + 'invalid_choice': _(u'Please select a valid choice. ' + u'That choice is not one of the available choices.'), + } + + def __init__(self, reference_class, query=None, choices=None, + empty_label=u'---------', + required=True, widget=forms.Select, label=None, initial=None, + help_text=None, *args, **kwargs): + """Constructor. + + Args: + reference_class: required; the db.Model subclass used in the reference + query: optional db.Query; default db.Query(reference_class) + choices: optional explicit list of (value, label) pairs representing + available choices; defaults to dynamically iterating over the + query argument (or its default) + empty_label: label to be used for the default selection item in + the widget; this is prepended to the choices + required, widget, label, initial, help_text, *args, **kwargs: + like for forms.Field.__init__(); widget defaults to forms.Select + """ + assert issubclass(reference_class, db.Model) + if query is None: + query = db.Query(reference_class) + assert isinstance(query, db.Query) + super(ModelChoiceField, self).__init__(required, widget, label, initial, + help_text, *args, **kwargs) + self.empty_label = empty_label + self.reference_class = reference_class + + self._query = query + self._choices = choices + self._update_widget_choices() + + def _update_widget_choices(self): + """Helper to copy the choices to the widget.""" + self.widget.choices = self.choices + + + + def _get_query(self): + """Getter for the query attribute.""" + return self._query + + def _set_query(self, query): + """Setter for the query attribute. + + As a side effect, the widget's choices are updated. + """ + self._query = query + self._update_widget_choices() + + query = property(_get_query, _set_query) + + def _generate_choices(self): + """Generator yielding (key, label) pairs from the query results.""" + + + yield ('', self.empty_label) + + + for inst in self._query: + yield (inst.key(), unicode(inst)) + + + + def _get_choices(self): + """Getter for the choices attribute. + + This is required to return an object that can be iterated over + multiple times. + """ + if self._choices is not None: + return self._choices + return _WrapIter(self._generate_choices) + + def _set_choices(self, choices): + """Setter for the choices attribute. + + As a side effect, the widget's choices are updated. + """ + self._choices = choices + self._update_widget_choices() + + choices = property(_get_choices, _set_choices) + + def clean(self, value): + """Override Field.clean() to do reference-specific value cleaning. + + This turns a non-empty value into a model instance. + """ + + value = super(ModelChoiceField, self).clean(value) + if not value: + return None + instance = db.get(value) + if instance is None: + raise db.BadValueError(self.error_messages['invalid_choice']) + return instance + + +class ReferenceProperty(db.ReferenceProperty): + __metaclass__ = monkey_patch + + def get_form_field(self, **kwargs): + """Return a Django form field appropriate for a reference property. + + This defaults to a ModelChoiceField instance. + """ + defaults = {'form_class': ModelChoiceField, + 'reference_class': self.reference_class} + defaults.update(kwargs) + return super(ReferenceProperty, self).get_form_field(**defaults) + + def get_value_for_form(self, instance): + """Extract the property value from the instance for use in a form. + + This return the key object for the referenced object, or None. + """ + value = super(ReferenceProperty, self).get_value_for_form(instance) + if value is not None: + value = value.key() + return value + + def make_value_from_form(self, value): + """Convert a form value to a property value. + + This turns a key string or object into a model instance. + """ + if value: + if not isinstance(value, db.Model): + value = db.get(value) + return value + + +class _ReverseReferenceProperty(db._ReverseReferenceProperty): + __metaclass__ = monkey_patch + + def get_form_field(self, **kwargs): + """Return a Django form field appropriate for a reverse reference. + + This always returns None, since reverse references are always + automatic. + """ + return None + + +def property_clean(prop, value): + """Apply Property level validation to value. + + Calls .make_value_from_form() and .validate() on the property and catches + exceptions generated by either. The exceptions are converted to + forms.ValidationError exceptions. + + Args: + prop: The property to validate against. + value: The value to validate. + + Raises: + forms.ValidationError if the value cannot be validated. + """ + if value is not None: + try: + + + + prop.validate(prop.make_value_from_form(value)) + except (db.BadValueError, ValueError), e: + raise forms.ValidationError(unicode(e)) + + +class ModelFormOptions(object): + """A simple class to hold internal options for a ModelForm class. + + Instance attributes: + model: a db.Model class, or None + fields: list of field names to be defined, or None + exclude: list of field names to be skipped, or None + + These instance attributes are copied from the 'Meta' class that is + usually present in a ModelForm class, and all default to None. + """ + + + + + def __init__(self, options=None): + self.model = getattr(options, 'model', None) + self.fields = getattr(options, 'fields', None) + self.exclude = getattr(options, 'exclude', None) + + +class ModelFormMetaclass(type): + """The metaclass for the ModelForm class defined below. + + This is our analog of Django's own ModelFormMetaclass. (We + can't conveniently subclass that class because there are quite a few + differences.) + + See the docs for ModelForm below for a usage example. + """ + + def __new__(cls, class_name, bases, attrs): + """Constructor for a new ModelForm class instance. + + The signature of this method is determined by Python internals. + + All Django Field instances are removed from attrs and added to + the base_fields attribute instead. Additional Field instances + are added to this based on the Datastore Model class specified + by the Meta attribute. + """ + + fields = sorted(((field_name, attrs.pop(field_name)) + for field_name, obj in attrs.items() + if isinstance(obj, forms.Field)), + key=lambda obj: obj[1].creation_counter) + + + + for base in bases[::-1]: + if hasattr(base, 'base_fields'): + fields = base.base_fields.items() + fields + declared_fields = django.utils.datastructures.SortedDict() + for field_name, obj in fields: + declared_fields[field_name] = obj + + opts = ModelFormOptions(attrs.get('Meta', None)) + attrs['_meta'] = opts + + + + + base_models = [] + for base in bases: + base_opts = getattr(base, '_meta', None) + base_model = getattr(base_opts, 'model', None) + if base_model is not None: + base_models.append(base_model) + if len(base_models) > 1: + raise django.core.exceptions.ImproperlyConfigured( + "%s's base classes define more than one model." % class_name) + + + + if opts.model is not None: + + + + if base_models and base_models[0] is not opts.model: + raise django.core.exceptions.ImproperlyConfigured( + '%s defines a different model than its parent.' % class_name) + + model_fields = django.utils.datastructures.SortedDict() + for name, prop in sorted(opts.model.properties().iteritems(), + key=lambda prop: prop[1].creation_counter): + if opts.fields and name not in opts.fields: + continue + if opts.exclude and name in opts.exclude: + continue + form_field = prop.get_form_field() + if form_field is not None: + model_fields[name] = form_field + + + model_fields.update(declared_fields) + attrs['base_fields'] = model_fields + + + + props = opts.model.properties() + for name, field in model_fields.iteritems(): + prop = props.get(name) + if prop: + + + + if hasattr(forms, 'FileField') and isinstance(field, forms.FileField): + def clean_for_property_field(value, initial, prop=prop, + old_clean=field.clean): + value = old_clean(value, initial) + property_clean(prop, value) + return value + else: + def clean_for_property_field(value, prop=prop, + old_clean=field.clean): + value = old_clean(value) + property_clean(prop, value) + return value + field.clean = clean_for_property_field + else: + attrs['base_fields'] = declared_fields + + return super(ModelFormMetaclass, cls).__new__(cls, + class_name, bases, attrs) + + +class BaseModelForm(forms.BaseForm): + """Base class for ModelForm. + + This overrides the forms.BaseForm constructor and adds a save() method. + + This class does not have a special metaclass; the magic metaclass is + added by the subclass ModelForm. + """ + + def __init__(self, data=None, files=None, auto_id=None, prefix=None, + initial=None, error_class=None, label_suffix=None, + instance=None): + """Constructor. + + Args (all optional and defaulting to None): + data: dict of data values, typically from a POST request) + files: dict of file upload values; Django 0.97 or later only + auto_id, prefix: see Django documentation + initial: dict of initial values + error_class, label_suffix: see Django 0.97 or later documentation + instance: Model instance to be used for additional initial values + + Except for initial and instance, these arguments are passed on to + the forms.BaseForm constructor unchanged, but only if not None. + Some arguments (files, error_class, label_suffix) are only + supported by Django 0.97 or later. Leave these blank (i.e. None) + when using Django 0.96. Their default values will be used with + Django 0.97 or later even when they are explicitly set to None. + """ + opts = self._meta + self.instance = instance + object_data = {} + if instance is not None: + for name, prop in instance.properties().iteritems(): + if opts.fields and name not in opts.fields: + continue + if opts.exclude and name in opts.exclude: + continue + object_data[name] = prop.get_value_for_form(instance) + if initial is not None: + + object_data.update(initial) + + + + + kwargs = dict(data=data, files=files, auto_id=auto_id, + prefix=prefix, initial=object_data, + error_class=error_class, label_suffix=label_suffix) + kwargs = dict((name, value) + for name, value in kwargs.iteritems() + if value is not None) + super(BaseModelForm, self).__init__(**kwargs) + + def save(self, commit=True): + """Save this form's cleaned data into a model instance. + + Args: + commit: optional bool, default True; if true, the model instance + is also saved to the datastore. + + Returns: + A model instance. If a model instance was already associated + with this form instance (either passed to the constructor with + instance=... or by a previous save() call), that same instance + is updated and returned; if no instance was associated yet, one + is created by this call. + + Raises: + ValueError if the data couldn't be validated. + """ + if not self.is_bound: + raise ValueError('Cannot save an unbound form') + opts = self._meta + instance = self.instance + if instance is None: + fail_message = 'created' + else: + fail_message = 'updated' + if self.errors: + raise ValueError("The %s could not be %s because the data didn't " + 'validate.' % (opts.model.kind(), fail_message)) + cleaned_data = self._cleaned_data() + converted_data = {} + propiter = itertools.chain( + opts.model.properties().iteritems(), + iter([('key_name', StringProperty(name='key_name'))]) + ) + for name, prop in propiter: + value = cleaned_data.get(name) + if value is not None: + converted_data[name] = prop.make_value_from_form(value) + try: + if instance is None: + instance = opts.model(**converted_data) + self.instance = instance + else: + + for name, value in converted_data.iteritems(): + if name == 'key_name': + + continue + setattr(instance, name, value) + except db.BadValueError, err: + raise ValueError('The %s could not be %s (%s)' % + (opts.model.kind(), fail_message, err)) + if commit: + + + instance.put() + return instance + + def _cleaned_data(self): + """Helper to retrieve the cleaned data attribute. + + In Django 0.96 this attribute was called self.clean_data. In 0.97 + and later it's been renamed to self.cleaned_data, to avoid a name + conflict. This helper abstracts the difference between the + versions away from its caller. + """ + try: + return self.cleaned_data + except AttributeError: + return self.clean_data + + +class ModelForm(BaseModelForm): + """A Django form tied to a Datastore model. + + Note that this particular class just sets the metaclass; all other + functionality is defined in the base class, BaseModelForm, above. + + Usage example: + + from google.appengine.ext import db + from google.appengine.ext.db import djangoforms + + # First, define a model class + class MyModel(db.Model): + foo = db.StringProperty() + bar = db.IntegerProperty(required=True, default=42) + + # Now define a form class + class MyForm(djangoforms.ModelForm): + class Meta: + model = MyModel + + You can now instantiate MyForm without arguments to create an + unbound form, or with data from a POST request to create a bound + form. You can also pass a model instance with the instance=... + keyword argument to create an unbound (!) form whose initial values + are taken from the instance. For bound forms, use the save() method + to return a model instance. + + Like Django's own corresponding ModelForm class, the nested Meta + class can have two other attributes: + + fields: if present and non-empty, a list of field names to be + included in the form; properties not listed here are + excluded from the form + + exclude: if present and non-empty, a list of field names to be + excluded from the form + + If exclude and fields are both non-empty, names occurring in both + are excluded (i.e. exclude wins). By default all property in the + model have a corresponding form field defined. + + It is also possible to define form fields explicitly. This gives + more control over the widget used, constraints, initial value, and + so on. Such form fields are not affected by the nested Meta class's + fields and exclude attributes. + + If you define a form field named 'key_name' it will be treated + specially and will be used as the value for the key_name parameter + to the Model constructor. This allows you to create instances with + named keys. The 'key_name' field will be ignored when updating an + instance (although it will still be shown on the form). + """ + + __metaclass__ = ModelFormMetaclass \ No newline at end of file diff --git a/favicon.ico b/favicon.ico new file mode 100644 index 0000000000000000000000000000000000000000..0062ab413e73fa137f91cc1b082ce6a9bc992730 GIT binary patch literal 1150 zcmchVT}YE*6vvO#ObKxkm}F29kx=MGBotCtksqk6T__r47lF2m6b%yb3oXOSIbYlS zT0@&?=KPkK%d$<~me%IfxfOnO(T!aM-B`iXL)@7O-l`DN1e2iD~XBziRG#kh#!R zlB8E8it@c6mm2KdQt4SrtcazQq9_WzNfg|g7q>oV8HRho0@vImRu-q>c1&T}ZNrEn z8w_M!@>p|P*lUL-1J(u!w6ahr(+Hi9ZooSBf?u?cW7TQJtD)!6ceg^Ls^;x8CJa_y zf%=*VO;Z1ys*4oKB@ovui@-u#Hq6{5exOLqtJr&1mYlE|hcK)zgn3L4omPc*wSr%= z8_+Kc`Kie`Q0Tpfu$IM8wNYDwH_b8_xGb!Fa=_S@3&Y(2n7fMcvbUY_wU~QvMAZdK z^0GqD1uKoB1e>V`i?b%)Jv)KVjGx!#!(1kYZAgyE(SB%Kt6uq5SN`+}7YLJM^E%zeeoan^L zM+er|To@bdfV#d4+`}^7JU$FVz3jW7t&A1oPE$#Oia#jM!CG;cH`F9xacU4>zbwNv z(v8_^Bi`xc$PJ;B0@aimxf5om`sBbn^J*!q@>tZRP~WWpI{ozearr={bfhEqd}}Jv zK>=z^KlB$XBx>VDdCR&2bjiYnIr*n?DSu{fx8Qypof#;IMom2B2sJKw|89xT^OJ2e zEr^toK%ZJ)g{BNoPa4xBd`WiX!TwTk{wUFga8*Axhs;;~iN0-fp|&9>`oXlnwito? E4Z4s|oB#j- literal 0 HcmV?d00001 diff --git a/forms.py b/forms.py new file mode 100644 index 0000000..4adc77f --- /dev/null +++ b/forms.py @@ -0,0 +1,32 @@ +import djangoforms +from django import forms +from models import * + +Vehicles = ( + ('Moto','Moto'), + ('Bicicleta','Bicicleta'), + ('Helicoptero','Helicoptero'), + ('Van','Van'), + ) + +UserTypes = ( + ('Entregador','Entregador'), + ('Usuario','Usuario'), + ) + +class AppUserForm(djangoforms.ModelForm): + real_user = forms.EmailField(label='Usuario',widget=forms.TextInput(attrs={'size':'50','maxlength':'50'} )) + #name = forms.CharField(label='Identificacao',widget=forms.TextInput(attrs={'size':'50','maxlength':'50'} )) + vehicle = forms.TypedChoiceField(choices=Vehicles, initial='Moto') + user_type = forms.TypedChoiceField(choices=UserTypes, initial='Usuario') + class Meta: + model = AppUser + exclude = ['last_status', 'last_position'] + +class DeliverFeeForm(djangoforms.ModelForm): + source_address = forms.CharField(label='Origem',widget=forms.TextInput(attrs={'size':'50','maxlength':'50'} )) + destination_address = forms.CharField(label='Destino',widget=forms.TextInput(attrs={'size':'50','maxlength':'50'} )) + item = forms.CharField(label='Item',widget=forms.Textarea) + class Meta: + model = DeliverFee + exclude = ['dest_lng','dest_lat', 'source_lat', 'source_lng','request_user', 'item', 'request_date_time', 'closed'] \ No newline at end of file diff --git a/index.yaml b/index.yaml new file mode 100644 index 0000000..a3b9e05 --- /dev/null +++ b/index.yaml @@ -0,0 +1,11 @@ +indexes: + +# AUTOGENERATED + +# This index.yaml is automatically updated whenever the dev_appserver +# detects that a new type of query is run. If you want to manage the +# index.yaml file manually, remove the above marker line (the line +# saying "# AUTOGENERATED"). If you want to manage some indexes +# manually, move them above the marker line. The index.yaml file is +# automatically uploaded to the admin console when you next deploy +# your application using appcfg.py. diff --git a/main.py b/main.py new file mode 100644 index 0000000..f693235 --- /dev/null +++ b/main.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python +# +# Copyright 2008 Google Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +import os +import sys +import logging + +# Google App Engine imports. +from google.appengine.ext.webapp import util + +os.environ['DJANGO_SETTINGS_MODULE'] = 'settings' + +# Import various parts of Django. +import django.core.handlers.wsgi +import django.core.signals +import django.dispatch.dispatcher +import django.db + +def log_exception(*args, **kwds): + """Log the current exception. + Invoked when a Django request raises an exception""" + logging.exception("Exception in request:") + +def main(): + # Create a Django application for WSGI. + application = django.core.handlers.wsgi.WSGIHandler() + + # Run the WSGI CGI handler with that application. + util.run_wsgi_app(application) + +if __name__ == '__main__': + main() diff --git a/models.py b/models.py new file mode 100644 index 0000000..19ab406 --- /dev/null +++ b/models.py @@ -0,0 +1,21 @@ + +from google.appengine.ext import db + +class AppUser(db.Model): + real_user = db.UserProperty() + vehicle = db.StringProperty() + user_type = db.StringProperty() + last_status = db.StringProperty() + last_position = db.StringProperty() + +class DeliverFee(db.Model): + source_address = db.StringProperty() + destination_address = db.StringProperty() + request_user = db.UserProperty() + item = db.StringProperty() + request_date_time = db.DateTimeProperty() + closed = db.BooleanProperty() + source_lat = db.StringProperty() + source_lng = db.StringProperty() + dest_lat = db.StringProperty() + dest_lng = db.StringProperty() \ No newline at end of file diff --git a/services.py b/services.py new file mode 100644 index 0000000..f4efe5d --- /dev/null +++ b/services.py @@ -0,0 +1,14 @@ +from google.appengine.ext.webapp import util +from protorpc.wsgi import service +import logging +import deliveryservice + +logging.debug('Handling service...') +# Map the RPC service and path (/DeliverService) +delivery_service = service.service_mapping(deliveryservice.DeliveryService, '/DeliveryService.*') + +def main(): + util.run_wsgi_app(delivery_service) + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/settings.py b/settings.py new file mode 100644 index 0000000..dd90a07 --- /dev/null +++ b/settings.py @@ -0,0 +1,66 @@ +#!/usr/bin/env python +# +# Copyright 2008 Google Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +import os + +DEBUG = True +TEMPLATE_DEBUG = DEBUG + +APPEND_SLASH = False + +# Language code for this installation. All choices can be found here: +# http://www.w3.org/TR/REC-html40/struct/dirlang.html#langcodes +# http://blogs.law.harvard.edu/tech/stories/storyReader$15 +LANGUAGE_CODE = 'en-us' + +SITE_ID = 1 + +# If you set this to False, Django will make some optimizations so as not +# to load the internationalization machinery. +USE_I18N = True + +# URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a +# trailing slash. +# Examples: "http://foo.com/media/", "/media/". +ADMIN_MEDIA_PREFIX = '/media/' + +# You really should fill this in for your application! +# see http://www.djangoproject.com/documentation/settings/#secret-key +# Make this unique, and don't share it with anybody. +SECRET_KEY = 'Zcvb456278c' + +# List of callables that know how to import templates from various sources. +TEMPLATE_CONTEXT_PROCESSORS = ( + 'django.contrib.auth.context_processors.auth', + 'django.core.context_processors.request', +) + +MIDDLEWARE_CLASSES = ( + 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.contrib.auth.middleware.AuthenticationMiddleware', +) + +#INSTALLED_APPS = ( +# 'guestbook', +#) + +ROOT_URLCONF = 'urls' + +ROOT_PATH = os.path.dirname(__file__) +TEMPLATE_DIRS = ( + os.path.join(ROOT_PATH, 'templates') +) diff --git a/templates/deliverfee.html b/templates/deliverfee.html new file mode 100644 index 0000000..ed8e36e --- /dev/null +++ b/templates/deliverfee.html @@ -0,0 +1,17 @@ +{%extends "template.html"%} +{%block body%} +
+

Submeter requisicao

+ +
+ + {{form}} + + + +
+ +
+
+ +{%endblock%} \ No newline at end of file diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..cd625f8 --- /dev/null +++ b/templates/index.html @@ -0,0 +1,14 @@ + + + Vai Frete + + + + +
+
+ Efetue o login com sua conta do Google para poder visualizar. +
+ + + diff --git a/templates/signup.html b/templates/signup.html new file mode 100644 index 0000000..c6f4730 --- /dev/null +++ b/templates/signup.html @@ -0,0 +1,17 @@ +{%extends "template.html"%} +{%block body%} +
+

Crir usuario

+ +
+ + {{form}} + + + +
+ +
+
+ +{%endblock%} \ No newline at end of file diff --git a/templates/template.html b/templates/template.html new file mode 100644 index 0000000..bf949cb --- /dev/null +++ b/templates/template.html @@ -0,0 +1,42 @@ + + + + + + + + + + + + + + + + + {%block meta%}{%endblock%} + + +
+
+ Bem vindo {{ userLogged.nickname }} - logout. +
+ +
+ + +
+
+ +
+ {%block body%}{%endblock%} +
+
+ + + diff --git a/templates/welcome.html b/templates/welcome.html new file mode 100644 index 0000000..a2aa3cc --- /dev/null +++ b/templates/welcome.html @@ -0,0 +1,9 @@ +{%extends "template.html"%} +{%block body%} +
+
+
+ Bem vindo, utilize o menu +
+ +{%endblock%} \ No newline at end of file diff --git a/urls.py b/urls.py new file mode 100644 index 0000000..539667b --- /dev/null +++ b/urls.py @@ -0,0 +1,33 @@ +#!/usr/bin/env python +# +# Copyright 2008 Google Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +from django.conf.urls import * + +urlpatterns = patterns('', + (r'^$', 'views.index'), + #(r'^online$', 'views.online'), + (r'^login$', 'views.login'), + (r'^app/editUser$', 'views.editUser'), + (r'^app/createDeliverFee$', 'views.createDeliverFee'), + #(r'^player/(?P.*)$', 'views.player'), + (r'^app/welcome$', 'views.welcome'), + #(r'^admin/cameras$', 'views.listCam'), + #(r'^admin/camera/editar/(\d+)$', 'views.editCamera'), + #(r'^admin/camera/editar$', 'views.editCamera'), + #(r'^admin/camera/apagar/(\d+)$', 'views.delCamera'), + (r'^logout$', 'views.logout') +) \ No newline at end of file diff --git a/views.py b/views.py new file mode 100644 index 0000000..b1cbcf9 --- /dev/null +++ b/views.py @@ -0,0 +1,180 @@ +#!/usr/bin/env python +# +# Copyright 2008 Google Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +import datetime +import logging +import urllib2 + +from google.appengine.ext import db +from django.http import HttpResponseRedirect +from django.shortcuts import render_to_response + +from django import http +from django import shortcuts + +from google.appengine.api import users +import json + +from models import * +from forms import * + +def respond(request, user, template, params=None): + """Helper to render a response, passing standard stuff to the response. + + Args: + request: The request object. + user: The User object representing the current user; or None if nobody + is logged in. + template: The template name; '.html' is appended automatically. + params: A dict giving the template parameters; modified in-place. + + Returns: + Whatever render_to_response(template, params) returns. + + Raises: + Whatever render_to_response(template, params) raises. + """ + if params is None: + params = {} + if user: + params['userLogged'] = user + params['sign_out'] = users.CreateLogoutURL('/') + params['is_admin'] = users.IsCurrentUserAdmin() #and + #'Dev' in os.getenv('SERVER_SOFTWARE')) + else: + params['sign_in'] = users.CreateLoginURL(request.path) + + if not template.endswith('.html'): + template += '.html' + + return shortcuts.render_to_response(template, params) + +def index(request): + return render_to_response('index.html') + +def login(request): + return HttpResponseRedirect(users.create_login_url('/app/welcome')) + +def logout(request): + return HttpResponseRedirect(users.create_logout_url('/')) + +def call_geocoder(address): + maps_url = 'http://maps.googleapis.com/maps/api/geocode/json?address=%s&sensor=false' % address.replace(' ', '+') + logging.debug ('Calling maps: %s' % maps_url) + response = urllib2.urlopen(maps_url) + return response.read() + +def createDeliverFee(request): + user = users.GetCurrentUser() + if request.method == 'POST': + logging.debug('saving the deliver fee...') + form = DeliverFeeForm(data=request.POST) + errors = form.errors + if not errors: + try: + deliver_fee = form.save(commit=False) + + #TODO: Add it assyncrhonous... + #adding geo location + source_location = json.loads(call_geocoder(deliver_fee.source_address)) + deliver_fee.source_lat = extractFromJson(source_location, 'lat') + deliver_fee.source_lng = extractFromJson(source_location, 'lng') + + dest_location = json.loads(call_geocoder(deliver_fee.destination_address)) + deliver_fee.dest_lat = extractFromJson(dest_location, 'lat') + deliver_fee.dest_lng = extractFromJson(dest_location, 'lng') + + deliver_fee.request_date_time = datetime.datetime.today() + deliver_fee.request_user = user + deliver_fee.closed = False + deliver_fee.put() + + return http.HttpResponseRedirect('/app/welcome') + except ValueError, err: + logging.error (err) + errors['__all__'] = unicode(err) + + if errors: + return respond(request, user, 'deliverfee', {'form' : form } ) + + else: + form = DeliverFeeForm() + return respond(request, user, 'deliverfee', {'form' : form } ) + +def extractFromJson(parsed_map, param): + try: + return str(parsed_map.get('results')[0].get('geometry').get('location').get(param)) + except TypeError, err: + logging.error(err) + return '0.0'; + +def editUser(request): + """Create or edit a User. GET shows a pre filled form, POST processes it.""" + app_users = get_app_user() + user = users.GetCurrentUser() + + #saves the user + if request.method == 'POST': + + logging.debug('saving the user...') + app_user = app_users[0] if len(app_users) > 0 else None + form = AppUserForm(data=request.POST, instance=app_user) + + errors = form.errors + if not errors: + try: + obj_save = form.save(commit=True) + obj_save.put() + + return http.HttpResponseRedirect('/app/welcome') + except ValueError, err: + logging.error (err) + errors['__all__'] = unicode(err) + + if errors: + return respond(request, user, 'signup', {'form' : form } ) + + if (len(app_users) == 0): + app_user = AppUser() + app_user.real_user = user + else: + app_user = app_users[0] + + form = AppUserForm(data=None, instance=app_user) + logging.debug ('Return the user..') + return respond(request, user, 'signup', {'form' : form } ) + +def welcome(request): + app_users = get_app_user() + user = users.GetCurrentUser() + + if (len(app_users) == 0): + return http.HttpResponseRedirect('/app/editUser') + else: + return respond(request, user, 'welcome') + +def get_app_user(): + user = users.GetCurrentUser() + if user is None: + return http.HttpResponseForbidden('You must be signed in to add or edit a camera') + + q = db.GqlQuery("SELECT * " + "FROM AppUser " + "WHERE real_user = :1 ", + user ) + + return q.fetch(1) \ No newline at end of file