Beispiel #1
0
 def post(self):
     user = users.get_current_user()
     
     user_prefs_query = UserPrefs().all()
     user_prefs_query.filter('google_account =', user)
     
     user_prefs = user_prefs_query.get()
     
     if user is not None and user_prefs is not None:
         name = self.request.get('name')
         name_yomi = self.request.get('name_yomi')
         red = self.request.get('red')
         green = self.request.get('green')
         blue = self.request.get('blue')
         rank = self.request.get('rank')
         
         color_name = ColorName().all()\
                         .filter('user_prefs =', user_prefs)\
                         .filter('name =', name)\
                         .filter('name_yomi =', name_yomi)\
                         .filter('red =', int(red))\
                         .filter('green =', int(green))\
                         .filter('blue =', int(blue))\
                         .get()
                         
         if color_name is None:
             color_name = ColorName()
             color_name.user_prefs = user_prefs
             color_name.name = name
             color_name.name_yomi = name_yomi
             color_name.red = int(red)
             color_name.green = int(green)
             color_name.blue = int(blue)
         
         color_name.rank = int(rank)
         color_name.put()
         
         taskqueue.add(url='/task/create_crayon', params={'id': color_name.key().id()})
 
         data = json.dumps({'state': 'ok'}, ensure_ascii=False)
         
     else:
         data = json.dumps({'state': 'failed'}, ensure_ascii=False)
         
     self.response.content_type = 'application/json'
     self.response.out.write(data)
Beispiel #2
0
 def post(self):
     user = users.get_current_user()
     
     user_prefs_query = UserPrefs().all()
     user_prefs_query.filter('google_account =', user)
     
     user_prefs = user_prefs_query.get()
     
     if user is not None and user_prefs is not None:
         raw_data = self.request.get('raw_data')
         
         if raw_data != '':
             json_data = json.loads(raw_data)
             
             saved_color_name_list = ColorName().all()\
                                 .filter('user_prefs =', user_prefs)\
                                 .fetch(100, 0)
                                 
             for saved_color_name in saved_color_name_list:
                 is_saved = False
                 for data in json_data:
                     if data['name'] == saved_color_name.name and \
                         data['red'] == saved_color_name.red and \
                         data['green'] == saved_color_name.green and \
                         data['blue'] == saved_color_name.blue:
                         
                         is_saved = True
                         continue
                     
                 if is_saved == False:
                     logging.info('delete.')
                     saved_color_name.delete()
                 else:
                     logging.info('not delete.')
             
             new_color_name_list = []
             
             for data in json_data:
                 name = data['name']
                 name_yomi = data['name_yomi']
                 red = data['red']
                 green = data['green']
                 blue = data['blue']
                 rank = data['rank']
             
                 color_name = ColorName().all()\
                                 .filter('user_prefs =', user_prefs)\
                                 .filter('name =', name)\
                                 .filter('name_yomi =', name_yomi)\
                                 .filter('red =', red)\
                                 .filter('green =', green)\
                                 .filter('blue =', blue)\
                                 .get()
                                 
                 do_create_crayon = False
                 if color_name is None:
                     color_name = ColorName()
                     color_name.user_prefs = user_prefs
                     color_name.name = name
                     color_name.name_yomi = name_yomi
                     color_name.red = int(red)
                     color_name.green = int(green)
                     color_name.blue = int(blue)
                     
                     new_color_name_list.append({'name': color_name.name,
                                'name_yomi': color_name.name_yomi,
                                'red': color_name.red,
                                'green': color_name.green,
                                'blue': color_name.blue})
                     
                     do_create_crayon = True
                 
                 color_name.rank = int(rank)
                 color_name.put()
                 
                 if do_create_crayon:
                     taskqueue.add(url='/task/create_crayon', params={'id': color_name.key().id()})
 
         data = json.dumps({'state': 'ok',
                            'user_id': user_prefs.user_id,
                            'new_color': new_color_name_list}, ensure_ascii=False)
         
     else:
         self.response.set_status(401)
         data = json.dumps({'state': 'failed'}, ensure_ascii=False)
         
     self.response.content_type = 'application/json'
     self.response.out.write(data)