2014-04-09 46 views
0

所以我有2模型类:AppEngine,实体丢失?

class Profile(db.Model): 
    """Profiles are merely lighter versions of Users. The are only used for the 
    purpose of notification 
    """ 
    created_at = db.DateTimeProperty(auto_now_add=True) 
    created_by = None # TODO User class 
    name = db.StringProperty() 
    email = db.EmailProperty() 
    phone = db.PhoneNumberProperty() 


class Notification(db.Model): 
    LEVELS = { 
     'INFO': 'INFO', 
     'WARNING': 'WARNING', 
     'CRITICAL': 'CRITICAL' 
    } 
    created_by = None # TODO user class 
    created_at = db.DateTimeProperty(auto_now_add=True) 
    profile = db.ReferenceProperty(Profile, collection_name='notifications') 
    level = db.StringProperty() 

这是我的JSONencoder样子:

class JsonEncoder(json.JSONEncoder): 
    def default(self, obj): 
    if isinstance(obj, datetime.datetime): 
     return obj.isoformat() 
    elif isinstance(obj, Profile): 
     return dict(key=obj.key().id_or_name(), 
        name=obj.name, email=obj.email, phone=obj.phone) 
    elif isinstance(obj, Limits): 
     return None 
    else: 
     return json.JSONEncoder.default(self, obj) 

基本上,可以通知分配到配置文件,这样当通知火灾,所有的与该通知相关的配置文件将被通知。

在我的html,我有一个表格,允许用户创建一个信息通报:

<form action="{{ url_for('new_notification') }}" method=post> 

    <!-- If disabled=true we won't send this in the post body --> 
    Name: 
    <input name='name' type='text' value='Select from above' disabled=true /> 
    <br/> 

    <!-- if type=hidden html will still send this in post body --> 
    <input name='key' type='hidden' value='Select from above' /> 

    Type: 
    <select name='level'> 
     {% for k,v in notification_cls.LEVELS.iteritems() %} 
     <option value="{{ v }} ">{{ k }}</option> 
     {% endfor %} 
    </select> 
    <br/> 

    <input type="submit" value="Submit"> 
    </form> 

不过,我看到了一些奇怪的事情在我的方法发生在创建通知:

@app.route('/notifications/new/', methods=['GET', 'POST']) 
def new_notification(): 
    """Displays the current notifications avaiable for each profiles 
    """ 
    if request.method == 'GET': 
    return render_template('new_notification.html', 
          notification_cls=Notification) 
    else: 
    profile_key_str = request.form.get('key', None) 
    level = request.form.get('level', None) 
    if not profile_key_str or not level: 
     abort(404) 

    profile_key = db.Key.from_path('Profile', profile_key_str) 

    profile = Profile.get(profile_key) 
    print profile  # This results in None?? 

    notification = Notification(parent=profile) # Ancestor for strong consistency 
    notification.profile = profile_key 
    notification.level = level 
    notification.put() 
    return redirect(url_for('list_notifications'), code=302) 

所以用户可以在new_notifications页面上创建一个配置文件。之后,我的服务器将通过ajax将新创建的实体密钥发送到包含该表单的相同页面。这将设置隐藏的输入值。

不知何故,在我的new_notification方法中,配置文件不存在!!!!我有点怀疑AppEngine的“最终一致性”政策,但我一直在等待30分钟,结果仍然是无。

任何想法我做错了什么?

编辑:

我忘了,包括它调用JSONEncoder我的JS方法

@app.route('/profiles/', methods=['GET']) 
def ajax_list_profiles(): 
    def _dynatable_wrapper(profiles): 
    return {'records': profiles, 
      'queryRecordCount': len(profiles), 
      'totalRecordCount': len(profiles)} 
    name = request.args.get('queries[search]', None) 
    if not name: 
    profiles = db.Query(Profile).order('name').fetch(None) 
    return json.dumps(_dynatable_wrapper(profiles), cls=JsonEncoder) 
    else: 
    profiles = db.Query(Profile).filter("name =", name).order('name').fetch(None) 
    return json.dumps(_dynatable_wrapper(profiles), cls=JsonEncoder) 
+0

配置文件键应该如何进出模板?你没有看到它被传入,而隐藏的字段具有不相关的字符串值。 –

+0

@DanielRoseman我更新了 – disappearedng

回答

0

你可能想使用int的关键路径,否则会认为它的键名。

+0

谢谢!这很痛苦 – disappearedng