简体   繁体   English

AppEngine,实体不见了?

[英]AppEngine, entity missing?

So I have 2 model classes: 所以我有2个模型类:

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()

This is what my JSONencoder looks like: 这是我的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)

Basically, one can assign a notification to a profile such that when a notification fires, all the profiles associated with that notification will be notified. 基本上,可以将通知分配给配置文件,以便在触发通知时,将通知与该通知关联的所有配置文件。

In my html, I have a form allowing users to create a notifiation: 在我的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>

However, I am seeing some strange things happening in my method to create a notification: 但是,我发现在创建通知的方法中发生了一些奇怪的事情:

@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)

So a user can create a profile on the new_notifications page. 因此,用户可以在new_notifications页面上创建配置文件。 After that, my server will send the newly created entity's key via ajax to the same page containing the form. 之后,我的服务器将通过ajax将新创建的实体的密钥发送到包含表单的同一页面。 This will set the hidden input's value. 这将设置隐藏输入的值。

Somehow, in my new_notification method, Profile does not exist!!!! 不知何故,在我的new_notification方法中,Profile不存在! I am sort of suspicious of AppEngine's "eventual consistency" policy but I've been waiting for 30 mins and the results is still None. 我对AppEngine的“最终一致性”政策有些怀疑,但我已经等了30分钟,结果仍然是None。

Any ideas what I am doing wrong? 有什么想法我做错了吗?

EDIT: 编辑:

I forgot to include the method which calls the JSONEncoder for my JS 我忘了包括为我的JS调用JSONEncoder的方法

@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)

您可能希望将int用作键路径,否则它将认为其是键名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM