serve() is called twice everytime I reload a blog page in wagtail - python

I am using wagtail to build a blog website. In order to count the visits of each blog, I override the get_context() and serve() to set cookie. The codes are as follows:
def get_context(self, request):
authorname=self.author.get_fullname_or_username()
data = count_visits(request, self)
context = super().get_context(request)
context['client_ip'] = data['client_ip']
context['location'] = data['location']
context['total_hits'] = data['total_hits']
context['total_visitors'] =data['total_vistors']
context['cookie'] = data['cookie']
context['author']=authorname
return context
def serve(self, request):
context = self.get_context(request)
print(request.COOKIES)
template = self.get_template(request)
response = render(request, template, context)
response.set_cookie(context['cookie'], 'true', max_age=3000)
return response
And the function count_visits is as follows:
def count_visits(request, obj):
data={}
ct = ContentType.objects.get_for_model(obj)
key = "%s_%s_read" % (ct.model, obj.pk)
if not request.COOKIES.get(key):
blog_visit_count, created =BlogVisitNumber.objects.get_or_create(content_type=ct, object_id=obj.pk)
blog_visit_count.count += 1
blog_visit_count.save()
The problem I met is that when I reload the blog page, serve() was loaded twice. The first time request was with cookie info. But the second time the cookie of the request was {}, which caused the count of visit does not work correctly.
I do not understand why serve() is called twice everytime I reload page.
After adding a print command in serve(), the log of reload blog page is as follows:
[19/Sep/2022 23:08:44] "GET /notifications/api/unread_list/?max=5 HTTP/1.1" 200 38
{'csrftoken': 'vAnNLAp8Fb9zPkUOSrHTFGB5Bp4W4aLu8mQWPklIhX1JpDTcKIceeId03jTKJpA3', 'sessionid': '9qdgjulitvosan1twtqe0h5bpfn4z3hg', 'bloglistingpage_7_read': 'true', 'blogdetailpage_6_read': 'true'}
[19/Sep/2022 23:08:44] "GET /blog-1/ HTTP/1.1" 200 27594
{}
[19/Sep/2022 23:08:45] "GET /blog-1/ HTTP/1.1" 200 23783
[19/Sep/2022 23:08:46] "GET /notifications/api/unread_list/?max=5 HTTP/1.1" 200 38
And the log of curl is:
[19/Sep/2022 23:12:24] "GET /notifications/api/unread_list/?max=5 HTTP/1.1" 200 38
{}
[19/Sep/2022 23:12:29] "GET /blog-1/ HTTP/1.1" 200 23783

Related

I get status code 300 while updating the space information and image

i have been trying to update the space information along with the images that is on the same template.I could add the content but could not update.i tried to pass slug while submitting for updated space information for updating images too but the slug is shown null. My ajax code and view was working for the add part but not for update part. I get my console.log('rent worked') printed that is inside success function but 'request.post' is not printed which is inside EditSpaceView function What might be the reason? What should i do to make it work ?
views.py
class EditSpaceView(View):
def post(self,request,*args,**kwargs):
print ('edit space view',request)
if request.POST:
print('request.post')
response = HttpResponse('')
print('edited owner name is',request.POST.get('ownerName'))
print('edited amenities',request.POST.get('amenities'))
rental = Rental.objetcs.get(slug=self.kwargs['slug'])
rental.ownerName = request.POST.get('ownerName')
rental.email = request.POST.get('email')
rental.phoneNumber = request.POST.get('phoneNumber')
rental.listingName = request.POST.get('listingName')
rental.summary = request.POST.get('summary')
rental.property = request.POST.get('property')
rental.room = request.POST.get('room')
rental.price = request.POST.get('price')
rental.city = request.POST.get('city')
rental.place = request.POST.get('place')
rental.water = request.POST.get('water')
rental.amenities = request.POST.get('amenities')
rental.save()
response['pk-user'] = rental.slug
#response['pk-user'] = rental.pk
print('response slug', response);
return response
return HttpResponseRedirect('/')
class EditImage(View):
model = Rental
template_name = 'rentals/rent_detail.html'
def get(self, request, *args, **kwargs):
return render(request, self.template_name)
def post(self,request,*args,**kwargs):
try:
rental = Rental.objects.get(slug = self.kwargs['slug'])
print('rental slug',rental)
except Rental.DoesNotExist:
error_dict = {'message': 'Rental spae not found'}
return self.render(request,'rentals/rent_detail.html',error_dict)
if request.FILES:
for file in request.FILES.getlist('image'):
print('file',file)
image = Gallery.objects.create(rental = rental, image=file)
print('image',image)
return HttpResponseRedirect('/')
urls.py
url(r'^edit/image/(?P<slug>[\w-]+)/$', EditImage.as_view(), name="editImage"),
url(r'^edit/space/(?P<slug>[\w-]+)/$', EditSpaceView.as_view(), name="editSpace"),
ajax code
$.ajax({
url:'/edit/space/'+this.props.slug+'/',
contentType: "application/json",
data:sendData,
type:'POST',
success: function(data, textStatus, xhr ) {
console.log('rent worked');
var slug = xhr.getResponseHeader('pk-user');
console.log('slug',slug);
$.ajax({
url:'/edit/image/'+slug+'/',
data:image,
contentType:false,
processData:false,
type:'POST',
mimeType: "multipart/form-data",
success: function(data) {
console.log('success');
}
});
}
});
Stacktrace in server console
[05/Apr/2016 02:58:35] "GET /api/v1/rental/tushant-khatiwada/ HTTP/1.1" 200 963
edit space view <WSGIRequest: POST '/edit/space/tushant-khatiwada/'>
[05/Apr/2016 02:58:50] "POST /edit/space/tushant-khatiwada/ HTTP/1.1" 302 0
[05/Apr/2016 02:58:51] "GET / HTTP/1.1" 200 2363
Not Found: /edit/image/null/
[05/Apr/2016 02:58:51] "POST /edit/image/null/ HTTP/1.1" 404 6882
[05/Apr/2016 02:58:35] "GET /api/v1/rental/tushant-khatiwada/ HTTP/1.1" 200 963
edit space view <WSGIRequest: POST '/edit/space/tushant-khatiwada/'>
[05/Apr/2016 02:58:50] "POST /edit/space/tushant-khatiwada/ HTTP/1.1" 302 0
[05/Apr/2016 02:58:51] "GET / HTTP/1.1" 200 2363
These requests suggests that editing with EditSpaceView worked as expected. The 300 response is expected and due to HttpResponseRedirect("/") at the end of that view.
The request
[05/Apr/2016 02:58:51] "POST /edit/image/null/ HTTP/1.1" 404 6882
suggests that slug that you are using to build the URL for ajax request is not valid or is null. Specifically check this line,
var slug = xhr.getResponseHeader('pk-user');

flask session not reloading

Right now I am using Flask and a flask 3rd party library Flask-Session
Using the code below, I reload the page 4 times and get the following output:
set userid[0]
127.0.0.1 - - [27/Sep/2014 22:28:35] "GET / HTTP/1.1" 200 -
set userid[1]
127.0.0.1 - - [27/Sep/2014 22:28:37] "GET / HTTP/1.1" 200 -
set userid[2]
127.0.0.1 - - [27/Sep/2014 22:28:37] "GET / HTTP/1.1" 200 -
set userid[3]
127.0.0.1 - - [27/Sep/2014 22:28:38] "GET / HTTP/1.1" 200 -
Code:
from flask import Flask, session
from flask.ext.session import Session
app = Flask(__name__)
sess = Session()
nextId = 0
def verifySessionId():
global nextId
if not 'userId' in session:
session['userId'] = nextId
nextId += 1
sessionId = session['userId']
print ("set userid[" + str(session['userId']) + "]")
else:
print ("using already set userid[" + str(session['userId']) + "]")
sessionId = session.get('userId', None)
return sessionId
#app.route("/")
def hello():
userId = verifySessionId()
return str(userId)
if __name__ == "__main__":
app.config['SECRET_KEY'] = 'super secret key'
app.config['SESSION_TYPE'] = 'filesystem'
sess.init_app(app)
app.debug = True
app.run()
Shouldn't session['userId] be 'saved out' each time I reload the page?
You need to have cookies enabled for sessions to work. Even Flask-Session cannot track a browser without those.
Flask-Session sets a cookie with a unique id, then later on finds your session data again by that cookie.

Django 'django.contrib.redirects' is weird on 127.0.0.1

I'm developing a site and there are some old URLs to redirect. I use something like this:
url(r'^i/personagens/*(?:index\.php)*', 'whatever.views.legacy_personanges_home', name='legacy_personanges_home'),
and in the views
def legacy_personanges_home(request):
return HttpResponsePermanentRedirect('/bio/')
While developing I collect old urls and run them on a simple verifier to check my redirects and old pages are handled accordingly. Here it is:
host = 'http://127.0.0.1:8000'
oks = 0
bads = 0
good_codes = [200,301,302]
legacy_urls = [
'/i/personagens/index.php',
'/i/personagens/',
'/i/personagens/index.php?x=1',
]
import urllib2
for path in legacy_urls:
try:
request = urllib2.Request(host + path)
request.get_method = lambda : 'HEAD'
response = urllib2.urlopen(request)
print response.getcode(), path, '-->', response.geturl()
if response.getcode() in good_codes:
oks += 1
else:
bads += 1
except urllib2.HTTPError, e:
print e.code, path
bads += 1
total = oks + bads
print 'OK: %d/%d' % (oks, total)
print 'BAD: %d/%d' % (bads, total)
Response:
200 /i/personagens/index.php --> http://127.0.0.1:8000/bio/
200 /i/personagens/ --> http://127.0.0.1:8000/bio/
200 /i/personagens/index.php?x=1 --> http://127.0.0.1:8000/bio/
OK: 3/3
BAD: 0/3
[Finished in 2.7s]
Nice! urlopen() follows redirects and the response should be a 200. geturl() shows me the final url.
Later I discovered there were lots of different new urls to redirect, so I decided to use the Django Redirects framework. I installed it and put those very 3 urls there to redirect to my '/bio/' new url.
The Weird part:
I opened http://127.0.0.1:8000/personagens/index.php in the browser, it worked, I could see the 301 there and the following 200.
I ran again the same verifier:
404 /i/personagens/index.php
404 /i/personagens/
404 /i/personagens/index.php?x=1
OK: 0/3
BAD: 3/3
[Finished in 1.2s]
I could see the logs
[28/Feb/2014 13:00:41] "HEAD /i/personagens/index.php HTTP/1.1" 404 0
[28/Feb/2014 13:00:41] "HEAD /i/personagens/ HTTP/1.1" 404 0
[28/Feb/2014 13:00:41] "HEAD /i/personagens/index.php?x=1 HTTP/1.1" 404 0
I changed the HEAD to GET just in case... still not working
thanks in advance

Flask session member not persisting across requests

I'm writing a quick app to view a giant XML file with some AJAX style calls to viewgroup. My problem is session['groups'] not persisting. I have some old array with only 4 members that is stuck somewhere (cookie?..). That value is present when view is called. I then overwrite that session member with info from the recently opened xml file that contains 20+ members.
However, when viewgroup is called the session variable has reverted to the old value with only 4 members in the array!
Code followed by output. Note the 3 sessionStatus() calls
def sessionStatus():
print "# of groups in session = " + str(len(session['groups']))
#app.route('/')
def index():
cams = [file for file in os.listdir('xml/') if file.lower().endswith('xml')]
return render_template('index.html', cam_files=cams)
#app.route('/view/<xmlfile>')
def view(xmlfile):
path = 'xml/' + secure_filename(xmlfile)
print 'opening ' + path
xmlf = open(path, 'r')
tree = etree.parse(xmlf)
root = tree.getroot()
p = re.compile(r'Group')
groups = []
for g in root:
if (p.search(g.tag) is not None) and (g.attrib['Comment'] != 'Root'):
groups.append(Group(g.attrib['Comment']))
sessionStatus()
session['groups'] = groups
sessionStatus()
return render_template('view.html', xml=xmlfile, groups=groups)
#app.route('/viewgroup/<name>')
def viewGroup(name):
groups = session['groups']
sessionStatus()
if groups is None or len(groups) == 0:
raise Exception('invalid group name')
groups_filtered = [g for g in groups if g.name == name]
if len(groups_filtered) != 1:
raise Exception('invalid group name', groups_filtered)
group = groups_filtered[0]
prop_names = [p.name for p in group.properties]
return prop_names
Output
opening xml/d.xml
# of groups in session = 5
# of groups in session = 57
127.0.0.1 - - [17/Aug/2011 17:27:29] "GET /view/d.xml HTTP/1.1" 200 -
127.0.0.1 - - [17/Aug/2011 17:27:29] "GET /static/ivtl.css HTTP/1.1" 304 -
127.0.0.1 - - [17/Aug/2011 17:27:29] "GET /static/jquery.js HTTP/1.1" 304 -
127.0.0.1 - - [17/Aug/2011 17:27:29] "GET /static/raphael-min.js HTTP/1.1" 304 -
127.0.0.1 - - [17/Aug/2011 17:27:29] "GET /static/ivtl.css HTTP/1.1" 304 -
127.0.0.1 - - [17/Aug/2011 17:27:29] "GET /favicon.ico HTTP/1.1" 404 -
# of groups in session = 5
127.0.0.1 - - [17/Aug/2011 17:27:31] "GET /viewgroup/DeviceInformation HTTP/1.1" 200 -
I need all 57 groups to stay around. Any hints?
The data was simply too big to serialize into the session. Now I generate a key into a global dict and store that key in the session.
gXmlData[path] = groups
There's the problem that the global dict will stay around forever with more and more keys but the process isn't meant to live long.
Maybe your data is too big.
If your data is larger than 4KB, a server-side session is needed. Have a look at Flask-Session.

How to use Images Python API correctly to display an avatar with user comments?

This is a follow up question to my previous question. I am trying to upload and display an image as an avatar following the sample app here.
This is the code I am using:
In upload.py I have:
from google.appengine.api import images
class Upload(webapp.RequestHandler):
def get(self):
self.response.out.write("""
<form action="/avatar-save" enctype="multipart/form-data" method="post">
<div><label>Avatar:</label></div>
<div><input type="file" name="img"/></div>
</form>
</body>
</html>""")
class AvatarSave(webapp.RequestHandler):
def post(self):
avatar = images.resize(self.request.get("img"), 32, 32)
greeting.avatar = db.Blob(avatar)
greeting.put()
self.redirect('/')
application = webapp.WSGIApplication(
[('/upload', Upload),
('/avatar-save', AvatarSave),
],
debug=True)
def main():
run_wsgi_app(application)
if __name__ == "__main__":
main()
My first question is: How do I check that AvatarSave actually saves the image to datastore?
I try to display the image in hw.py:
class MainPage(webapp.RequestHandler):
def get(self):
siteUser = users.get_current_user()
greeting = None
if siteUser:
greeting = ("Welcome, %s! (sign out)" %
(siteUser.nickname(), users.create_logout_url("/")))
else:
greeting = ("Sign in or register" %
users.create_login_url("/"))
self.response.out.write(greeting)
query = User.all()
query.order("-userScore")
results = query.fetch(10)
self.response.out.write("""<html><head><style>
body {font-size: small;
font-family: Verdana, Helvetica, sans-serif;
}</style>
</head><body><ol>""")
for result in results:
self.response.out.write("<li>")
self.response.out.write("<b>%s</b> %s " % (result.userName, result.userLatestComment))
self.response.out.write("<div><img src='img?img_id=%s'></img>" % result.key())
self.response.out.write("</li>")
self.response.out.write("</ol></body></html>")
class Image (webapp.RequestHandler):
def get(self):
greeting = db.get(self.request.get("img_id"))
if greeting.avatar:
self.response.headers['Content-Type'] = "image/png"
self.response.out.write(greeting.avatar)
else:
self.response.out.write("No image")
application = webapp.WSGIApplication(
[('/', MainPage),
('/img', Image),
],
debug=True)
But all I get is the broken image link icon for each line displayed. (I have 8 lines displayed.)
In the Logs I see this:
INFO 2010-12-04 01:03:57,641 dev_appserver.py:3283] "GET / HTTP/1.1" 200 -
INFO 2010-12-04 01:03:57,703 dev_appserver.py:3283] "GET /img?img_id=ag1oZWxsby0xLXdvcmxkcgoLEgRVc2VyGBMM HTTP/1.1" 200 -
INFO 2010-12-04 01:03:57,756 dev_appserver.py:3283] "GET /img?img_id=ag1oZWxsby0xLXdvcmxkcgoLEgRVc2VyGBUM HTTP/1.1" 200 -
INFO 2010-12-04 01:03:58,734 dev_appserver.py:3283] "GET /img?img_id=ag1oZWxsby0xLXdvcmxkcgoLEgRVc2VyGBIM HTTP/1.1" 200 -
INFO 2010-12-04 01:03:58,812 dev_appserver.py:3283] "GET /img?img_id=ag1oZWxsby0xLXdvcmxkcgoLEgRVc2VyGFkM HTTP/1.1" 200 -
INFO 2010-12-04 01:03:58,878 dev_appserver.py:3283] "GET /img?img_id=ag1oZWxsby0xLXdvcmxkcgoLEgRVc2VyGFsM HTTP/1.1" 200 -
INFO 2010-12-04 01:03:58,934 dev_appserver.py:3283] "GET /img?img_id=ag1oZWxsby0xLXdvcmxkcgoLEgRVc2VyGFoM HTTP/1.1" 200 -
INFO 2010-12-04 01:03:58,986 dev_appserver.py:3283] "GET /img?img_id=ag1oZWxsby0xLXdvcmxkcgoLEgRVc2VyGGkM HTTP/1.1" 200 -
INFO 2010-12-04 01:03:59,040 dev_appserver.py:3283] "GET /img?img_id=ag1oZWxsby0xLXdvcmxkcgoLEgRVc2VyGGwM HTTP/1.1" 200 -
INFO 2010-12-04 01:04:00,102 dev_appserver.py:3283] "GET /favicon.ico HTTP/1.1" 200 -
As far as I understand, some images are fetched from the datastore; but I uploaded only 1 image (or I thought I did) but according to the logs the id for each image is different.
Can anyone help me understand what is going on here; and how to fix it? Many thanks for your help.
Update
In response to Ben's answer I updated the code like this:
This is the model I am using:
class User(db.Model):
userEmail = db.StringProperty()
....
avatar = db.BlobProperty()
and this is new AvatarSave:
class AvatarSave(webapp.RequestHandler):
def post(self):
q = User.all()
q.filter("userEmail =", "az#example.com")
qTable = q.fetch(1)
if qTable:
for row in qTable:
avatar = images.resize(self.request.get("img"), 32, 32)
row.avatar = db.Blob(avatar)
db.put(qTable)
else:
self.response.out.write("user not found")
self.redirect('/')
I believe this now updates the avatar of the user associated with the userEmail "az#example.com". Is this correct?
I updated hw.py to display only this user as well:
query = User.all()
query.filter("userEmail =", "az#example.com")
results = query.fetch(1)
self.response.out.write("""<html><head><style>
body {font-size: small;
font-family: Verdana, Helvetica, sans-serif;
}</style>
</head><body><ol>""")
for result in results:
self.response.out.write("<li>")
self.response.out.write("<b>%s</b> %s " % (result.userName, result.userLatestComment))
self.response.out.write("<div><img src='img?img_id=%s'></img>" % result.key())
self.response.out.write("</li>")
self.response.out.write("</ol></body></html>")
But there is still a problem because instead of displaying the image, I get "None" and the image link is broken.
Furthermore, when I look at the Datastore Viewer, I don't see the "avatar" column. But I see that some blob was updated at some point:
Entity Kind: __BlobUploadSession__
Key: ag10ZWxs....
ID: 125
Key Name:
creation: 1291388993.27
state: init
success_path: /upload
Not sure what is going on here and how to fix it. Any suggestion will be welcome.
In the log, you're seeing different image IDs because you're requesting up to 10 user objects from the Datastore, and the Datastore query is looking for the users regardless of whether or not they have an image stored (maybe you made 8 User objects in the course of testing).
As to why the avatars aren't saving:
class AvatarSave(webapp.RequestHandler):
def post(self):
avatar = images.resize(self.request.get("img"), 32, 32)
greeting.avatar = db.Blob(avatar)
Unless some code is missing from your example, the "greeting" variable is not set here. You have to first create a new greeting (by making a new instance of whatever model it uses) or load the appropriate one, then set the avatar to it, then greeting.put()
Update
If your code is indented like:
qTable = q.fetch(1)
if qTable:
for row in qTable:
avatar = images.resize(self.request.get("img"), 32, 32)
row.avatar = db.Blob(avatar)
db.put(qTable)
It will not work. What you want is probably:
qTable = q.fetch(1)
if qTable:
for row in qTable:
avatar = images.resize(self.request.get("img"), 32, 32)
row.avatar = db.Blob(avatar)
db.put(qTable)
But since you're working with only one object here, it would be simpler to replace your current use of fetch(1) and instead do:
currentUser = q.get()
if currentUser is not None:
avatar = images.resize(self.request.get("img"), 32, 32)
currentUser.avatar = db.Blob(avatar)
currentUser.put()

Categories