Beispiel #1
0
    def process(self):
        from voluptuous import Schema
        from appname.vendor import Vendor
        from appname.vendor import vendor_schema

        vendor = json.loads(self.request.body)
        schema = Schema(vendor_schema, extra=True)
        try:
            schema(vendor)
        except:
            logging.exception('validation failed')
            logging.info(vendor)

        vendor_entity = Vendor.from_dict(vendor)
        vendor_entity.put()

        user_id = users.get_current_user().user_id()
        person = Person.get_by_id(user_id)
        if vendor_entity.is_new:
            what = "Vendor Created."
        else:
            what = "Vendor Updated."
        what = "%s %s with tags: %s" % (what, vendor_entity.name,
                                        vendor_entity.tags)
        loc = ""
        if person is not None and person.location_info is not None:
            loc = person.location_info.get('latlong')
        message = {'location': loc, 'what': what}
        event.send("ACTIVITY", message)
        logging.info("Sending message: %s" % message)

        out = vendor_entity.to_dict()
        self.response.out.write(json.dumps(out))
Beispiel #2
0
    def process(self):
        from voluptuous import Schema
        from appname.vendor import Vendor
        from appname.vendor import vendor_schema

        vendor = json.loads(self.request.body)
        schema = Schema(vendor_schema, extra=True)
        try:
            schema(vendor)
        except:
            logging.exception('validation failed')
            logging.info(vendor)

        vendor_entity = Vendor.from_dict(vendor)
        vendor_entity.put()

        user_id = users.get_current_user().user_id()
        person = Person.get_by_id(user_id)
        if vendor_entity.is_new:
            what = "Vendor Created."
        else:
            what = "Vendor Updated."
        what = "%s %s with tags: %s" % (what, vendor_entity.name, vendor_entity.tags)
        loc = ""
        if person is not None and person.location_info is not None:
            loc = person.location_info.get('latlong')
        message = {'location': loc,
                    'what': what}
        event.send("ACTIVITY", message)
        logging.info("Sending message: %s" % message)


        out = vendor_entity.to_dict()
        self.response.out.write(json.dumps(out))
Beispiel #3
0
    def from_dict(cls, data):
        """Instantiate a Transaction entity from a dict of values."""
        from appname.vendor import Vendor
        import decimal

        key = data.get('key')
        transaction = None
        if key:
            key = ndb.Key(urlsafe=key)
            transaction = key.get()

        if not transaction:
            transaction = cls()
            transaction.is_new = True
        else:
            transaction.is_new = False

        transaction.date = cls.normalize_date_input(data.get('date'))
        transaction.vendor_name = data.get('vendor')

        # TODO: Use Python Decimal here with prec set to .00.
        amount = str(data.get('amount', "0")).translate(None, "$,")
        try:
            decimal.Decimal(amount)
        except decimal.InvalidOperation:
            amount = "NaN"

        transaction.amount = amount

        vendor_keyname = base64.b64encode(transaction.vendor_name.lower())
        vendor = Vendor.get_by_id(vendor_keyname)
        if vendor:
            transaction.tags = vendor.tags

        return transaction
Beispiel #4
0
    def delete(self):
        from google.appengine.ext import ndb
        from appname.vendor import Vendor
        urlsafe = self.request.path.rsplit('/', 1)[-1]
        if not urlsafe:
            return

        key = ndb.Key(urlsafe=urlsafe)
        if key.kind() != Vendor._get_kind():
            self.error(500)
            return

        key.delete()
        logging.info("Deleted vendor with key: %s", urlsafe)
Beispiel #5
0
    def delete(self):
        from google.appengine.ext import ndb
        from appname.vendor import Vendor
        urlsafe = self.request.path.rsplit('/', 1)[-1]
        if not urlsafe:
            return

        key = ndb.Key(urlsafe=urlsafe)
        if key.kind() != Vendor._get_kind():
            self.error(500)
            return

        key.delete()
        logging.info("Deleted vendor with key: %s", urlsafe)
Beispiel #6
0
    def get(self):
        from appname.vendor import Vendor
        user_query = self.request.get('query')
        limit = int(self.request.get('limit', 40))

        query = Vendor.query()
        if user_query:
            search = user_query.strip().lower()
            query = query.filter(Vendor.n_ >= search)
            query = query.filter(Vendor.n_ < search + u"\uFFFD")

        if limit > 0:
            query = query.fetch(limit)

        out = [entity.to_dict() for entity in query]
        self.response.out.write(json.dumps(out))
Beispiel #7
0
    def get(self):
        from appname.vendor import Vendor
        user_query = self.request.get('query')
        limit = int(self.request.get('limit', 40))

        query = Vendor.query()
        if user_query:
            search = user_query.strip().lower()
            query = query.filter(Vendor.n_ >= search)
            query = query.filter(Vendor.n_ < search + u"\uFFFD")

        if limit > 0:
            query = query.fetch(limit)

        out = [entity.to_dict() for entity in query]
        self.response.out.write(json.dumps(out))