コード例 #1
0
 def post(self):
     user = users.get_current_user()
     if user:
         params = self.request
         # ev_key = ndb.Key(EV, params.get('name'))
         ev_obj = EV.query(EV.name == params.get('name')).fetch()
         if not ev_obj:
             ev_obj = EV()
             ev_obj.name = params.get('name')
             ev_obj.manufacturer = params.get('manufacturer')
             ev_obj.year = int(params.get('year'))
             ev_obj.battery = int(params.get('battery'))
             ev_obj.wltp = int(params.get('wltp'))
             ev_obj.power = int(params.get('power'))
             ev_obj.cost = float(params.get('cost'))
             ev_obj.put()
             template_values = {
                 'user': user,
                 'report': '1',
                 'params': {
                     'name': '',
                     'manufacturer': ''
                 }
             }
         else:
             template_values = {
                 'user': user,
                 'report': '2',
                 'params': self.request
             }
         template = JINJA_ENVIRONMENT.get_template('add.html')
         self.response.write(template.render(template_values))
     else:
         self.redirect('/')
コード例 #2
0
ファイル: addev.py プロジェクト: bigjayray/GAE_ev
    def post(self):
        self.response.headers['Content-Type'] = 'text/html'

        # gets current user
        user = users.get_current_user()

        # get value of button clicked
        action = self.request.get('button')

        # create a new ev object
        ev = EV()

        # selection statement for button
        if action == 'Add':

            # adds form values to the ev object
            ev.name = self.request.get('ev_name')
            ev.manufacturer = self.request.get('ev_manufacturer')
            ev.year = int(self.request.get('ev_year'))
            ev.battery_size = float(self.request.get('ev_battery_size'))
            ev.WLTP_range = float(self.request.get('ev_WLTP_Range'))
            ev.cost = float(self.request.get('ev_cost'))
            ev.power = float(self.request.get('ev_power'))

            # querys ev to find duplicate ev
            query = EV.query(
                ndb.AND(EV.manufacturer == ev.manufacturer, EV.name == ev.name,
                        EV.year == ev.year))

            # selection statement for if query count is above 0
            if query.count() > 0:
                error = 'Error: EV already exists!'
                template_values = {'error': error, 'user': user}

                template = JINJA_ENVIRONMENT.get_template('addev.html')
                self.response.write(template.render(template_values))
            else:
                # adds ev to datastore
                ev.put()
                self.redirect('/')

        elif action == 'Cancel':
            self.redirect('/')