def testFilterByUser(self): anonymous_user = users.User(email="*****@*****.**") test_user = users.User(email="*****@*****.**") Greeting(content = "Testing", parent = guestbook_key(DEFAULT_GUESTBOOK_NAME), author = anonymous_user).put() Greeting(content = "Testing2", parent = guestbook_key(DEFAULT_GUESTBOOK_NAME), author = test_user).put() greetings_query = Greeting.query(ancestor=guestbook_key(DEFAULT_GUESTBOOK_NAME)).filter(ndb.GenericProperty('author') == anonymous_user) results = greetings_query.fetch(2) self.assertEqual(1, len(results)) self.assertEqual(anonymous_user, results[0].author)
def main_page(request): guestbook_name = request.GET.get('guestbook_name', DEFAULT_GUESTBOOK_NAME) # Ancestor Queries, as shown here, are strongly consistent with the High # Replication Datastore. Queries that span entity groups are eventually # consistent. If we omitted the ancestor from this query there would be # a slight chance that Greeting that had just been written would not # show up in a query. greetings_query = Greeting.query(ancestor=guestbook_key(guestbook_name)).order(-Greeting.date) greetings = greetings_query.fetch(10) if users.get_current_user(): url = users.create_logout_url(request.get_full_path()) url_linktext = 'Logout' else: url = users.create_login_url(request.get_full_path()) url_linktext = 'Login' template_values = { 'greetings': greetings, 'guestbook_name': guestbook_name, 'url': url, 'url_linktext': url_linktext, } #return direct_to_template(request, 'guestbook/main_page.html', template_values) return render(request, 'guestbook/main_page.html', template_values)
def main_page(request): guestbook_name = request.GET.get('guestbook_name', DEFAULT_GUESTBOOK_NAME) # Ancestor Queries, as shown here, are strongly consistent with the High # Replication Datastore. Queries that span entity groups are eventually # consistent. If we omitted the ancestor from this query there would be # a slight chance that Greeting that had just been written would not # show up in a query. greetings_query = Greeting.query( ancestor=guestbook_key(guestbook_name)).order(-Greeting.date) greetings = greetings_query.fetch(10) # if users.get_current_user(): # url = users.create_logout_url(request.get_full_path()) # url_linktext = 'Logout' # else: # url = users.create_login_url(request.get_full_path()) # url_linktext = 'Login' # url = users.create_login_url(request.get_full_path()) url = '' url_linktext = 'Login' template_values = { 'greetings': greetings, 'guestbook_name': guestbook_name, 'url': url, 'url_linktext': url_linktext, } return render(request, 'guestbook/main_page.html', template_values)
def form_valid(self, form, **kwargs): # how to get velue in the form guestbook_name = form.cleaned_data['guestbook_name'] content = form.cleaned_data['content'] greeting = Greeting(parent=guestbook_key(guestbook_name)) if users.get_current_user(): greeting.author = users.get_current_user() greeting.guestbook_name = guestbook_name greeting.content = content greeting.put() return super(SignView, self).form_valid(form, **kwargs)
def sign_post(request): if request.method == 'POST': guestbook_name = request.POST.get('guestbook_name') greeting = Greeting(parent=guestbook_key(guestbook_name)) if users.get_current_user(): greeting.author = users.get_current_user() greeting.content = request.POST.get('content') greeting.put() return HttpResponseRedirect('/?' + urllib.urlencode({'guestbook_name': guestbook_name})) return HttpResponseRedirect('/')
def post(self): guestbook_name = self.request.get('guestbook_name',DEFAULT_GUESTBOOK_NAME) greeting = Greeting(parent=guestbook_key(guestbook_name)) if users.get_current_user(): greeting.author = users.get_current_user() greeting.content = self.request.get('content') greeting.put() query_params = {'guestbook_name': guestbook_name} self.redirect('/guestbook/?' + urllib.urlencode(query_params))
def sign_post(request): if request.method == 'POST': guestbook_name = request.POST.get('guestbook_name') greeting = Greeting(parent=guestbook_key(guestbook_name)) if users.get_current_user(): greeting.author = users.get_current_user() greeting.content = request.POST.get('content') greeting.put() return HttpResponseRedirect('/?' + urllib.urlencode({'guestbook_name':guestbook_name})) return HttpResponseRedirect('/')
def get_context_data(self, **kwargs): guestbook_name = self.request.GET.get('guestbook_name', DEFAULT_GUESTBOOK_NAME) greetings_query = Greeting.query( ancestor=guestbook_key(guestbook_name)).order(-Greeting.date) greetings = greetings_query.fetch(10) if users.get_current_user(): url = users.create_logout_url(self.request.get_full_path()) url_linktext = 'Logout' else: url = users.create_login_url(self.request.get_full_path()) url_linktext = 'Login' context = super(IndexView, self).get_context_data(**kwargs) context['guestbook_name'] = guestbook_name context['greetings_query'] = greetings_query context['greetings'] = greetings context['url'] = url context['url_linktext'] = url_linktext return context
def get(self): guestbook_name = self.request.get('guestbook_name', DEFAULT_GUESTBOOK_NAME) greetings_query = Greeting.query( ancestor=guestbook_key(guestbook_name)).order(-Greeting.date) greetings = greetings_query.fetch(10) if users.get_current_user(): url = users.create_logout_url(self.request.uri) url_linktext = 'Logout' else: url = users.create_login_url(self.request.uri) url_linktext = 'Login' template_values = { 'greetings': greetings, 'guestbook_name': urllib.quote_plus(guestbook_name), 'url': url, 'url_linktext': url_linktext, } template = settings.JINJA_ENVIRONMENT.get_template('guestbook/index.html') self.response.write(template.render(template_values))
def testGetEntityViaMemcache(self): entity_key = Greeting(content = "Testing", parent = guestbook_key(DEFAULT_GUESTBOOK_NAME)).put() retrieved_entity = GetEntityViaMemcache(entity_key) self.assertNotEqual(None, retrieved_entity) self.assertEqual("Testing", retrieved_entity.content)
def testInsertEntity(self): Greeting(content = "Testing", parent = guestbook_key(DEFAULT_GUESTBOOK_NAME)).put() greetings_query = Greeting.query(ancestor=guestbook_key(DEFAULT_GUESTBOOK_NAME)) self.assertEqual(1, len(greetings_query.fetch(2)))
def get_queryset(self): guestbook_name = self.get_guestbook_name() return Greeting.query(ancestor=guestbook_key(guestbook_name)).order( -Greeting.date).fetch(5)