Is it possible to have a wagtail RouteablePageMixin route that is a subpath of another?
This is my code:
class MyPage(RoutablePageMixin, Page):
#route("foo/")
def foo(self, request):
view = views.FooView.as_view()
return view(request, self)
#route("foo/bar/")
def bar(self, request):
view = views.BarView.as_view()
return view(request, self)
What happens:
When I visit <page url>/foo/bar/ I get a FooView response, when what I want is a BarView response.
Is this possible to achieve?
Thanks for your help.
Routes are specified as regular expressions, so you need to mark the start and end with ^ and $ to ensure the URL pattern is matched against the whole path, rather than matching on any path that happens to contain the given pattern as a substring.
#route("^foo/$")
#route("^foo/bar/$")
Alternatively, you could make use of the fact that the patterns are checked in the order they appear in the code, and put the more specific one first:
class MyPage(RoutablePageMixin, Page):
#route("foo/bar/")
def bar(self, request):
view = views.BarView.as_view()
return view(request, self)
#route("foo/")
def foo(self, request):
view = views.FooView.as_view()
return view(request, self)
Related
I have a google-cloud-endpoints, in the docs, I did'nt find how to write a PATCH method.
My request:
curl -XPATCH localhost:8080/_ah/api/hellogreeting/1 -d '{"message": "Hi"}'
My method handler looks like this:
from models import Greeting
from messages import GreetingMessage
#endpoints.method(ID_RESOURCE, Greeting,`
path='hellogreeting/{id}', http_method='PATCH',
name='greetings.patch')
def greetings_patch(self, request):
request.message, request.username
greeting = Greeting.get_by_id(request.id)
greeting.message = request.message # It's ok, cuz message exists in request
greeting.username = request.username # request.username is None. Writing the IF conditions in each string(checking on empty), I think it not beatifully.
greeting.put()
return GreetingMessage(message=greeting.message, username=greeting.username)
So, now in Greeting.username field will be None. And it's wrong.
Writing the IF conditions in each string(checking on empty), I think it not beatifully.
So, what is the best way for model updating partially?
I do not think there is one in Cloud Endpoints, but you can code yours easily like the example below.
You will need to decide how you want your patch to behave, in particular when it comes to attributes that are objects : should you also apply the patch on the object attribute (in which case use recursion) or should you just replace the original object attribute with the new one like in my example.
def apply_patch(origin, patch):
for name in dir( patch ):
if not name.startswith( '__' ):
setattr(origin,name,getattr(patch,name))
I hope I didn't miss any topic that could answer my problem. I'm here now because I'm terribly frustrated and tired with the following task:
- I have a Spreasheet with Drive.Google with lots of data in it
- I would like to create an application with wxPython that would pull data from this spreeadsheet (in the most easy way possible)
- Would also like to get multiple data from a user who will access this application through a nice interface (panel aka window)
- The multiple data introduced by the user should be able to work with the data pulled out from the Spreasheet. For example to see if the data introduced by the user is in the Spreadsheet or not and also some other operations with the next data introduced by the user.
- Finally and most importantly show the results to the user (later I would also like to add some functions to save somehow the results)
I hope I managed to express clearly what I would like to do. Now I'm new to Google API's, Python adn wxPython, but I have experience with C++ , php, html .
I've spent 2 weeks now with discovering Drive.Google and learning Python and wxPython. I did follow all tuturials on these, made my notes, read tones of stackoverflow questions-answers, wiki.wxpython.org etc. I learn every single day and I can do now many things separately but to have all functions like I described above I just couldn't work out how to do. At least please orient me in the direction. Awfel lot of times I spend hours doing examples and getting nowhere. I have Python, wxPython extention, GoogleAppEngine Launcher and even pyCharm demo. Please be kind. This is my first question ever.
here's the mess I made so far combining relevant examples:
import wx
import gdata.docs
import gdata.docs.service
import gdata.spreadsheet.service
import re, os
class Form(wx.Panel):
def __init__(self, *args, **kwargs):
super(Form, self).__init__(*args, **kwargs)
self.createControls()
self.bindEvents()
self.doLayout()
self.spreasht()
def createControls(self):
self.logger = wx.TextCtrl(self, style=wx.TE_MULTILINE|wx.TE_READONLY)
self.saveButton = wx.Button(self, label="Elvegzes")
self.nameLabel = wx.StaticText(self, label="type Name1:")
self.nameTextCtrl = wx.TextCtrl(self, value="type here")
self.name2Label = wx.StaticText(self, label="type Name2:")
self.name2TextCtrl = wx.TextCtrl(self, value="type here")
def bindEvents(self):
for control, event, handler in \
[(self.saveButton, wx.EVT_BUTTON, self.onSave),
(self.nameTextCtrl, wx.EVT_TEXT, self.onNameEntered),
(self.nameTextCtrl, wx.EVT_CHAR, self.onNameChanged)]:
control.Bind(event, handler)
def doLayout(self):
raise NotImplementedError
def spreadsht(self):
gd_client = gdata.spreadsheet.service.SpreadsheetsService()
gd_client.email = 'my email address'
gd_client.password = 'my password to it'
gd_client.source = 'payne.org-example-1'
gd_client.ProgrammaticLogin()
q = gdata.spreadsheet.service.DocumentQuery()
q['title'] = 'stationcenter'
q['title-exact'] = 'true'
feed = gd_client.GetSpreadsheetsFeed(query=q)
spreadsheet_id = feed.entry[0].id.text.rsplit('/',1)[1]
feed = gd_client.GetWorksheetsFeed(spreadsheet_id)
worksheet_id = feed.entry[0].id.text.rsplit('/',1)[1]
al1 = raw_input('Name1: ')
print al1
al2 = raw_input('Name2: ')
print al2
rows = gd_client.GetListFeed(spreadsheet_id, worksheet_id).entry
for row in rows:
for key in row.custom:
if al1 == row.custom[key].text:
print ' %s: %s' % (key, row.custom[key].text)
def onColorchanged(self, event):
self.__log('User wants color: %s'%self.colors[event.GetInt()])
def onReferrerEntered(self, event):
self.__log('User entered referrer: %s'%event.GetString())
def onSave(self,event):
self.__log('User clicked on button with id %d'%event.GetId())
def onNameEntered(self, event):
self.__log('User entered name: %s'%event.GetString())
def onNameChanged(self, event):
self.__log('User typed character: %d'%event.GetKeyCode())
event.Skip()
def onInsuranceChanged(self, event):
self.__log('User wants insurance: %s'%bool(event.Checked()))
# Helper method(s):
def __log(self, message):
''' Private method to append a string to the logger text
control. '''
self.logger.AppendText('%s\n'%message)
class FormWithSizer(Form):
def doLayout(self):
''' Layout the controls by means of sizers. '''
boxSizer = wx.BoxSizer(orient=wx.HORIZONTAL)
gridSizer = wx.FlexGridSizer(rows=5, cols=2, vgap=10, hgap=10)
# Prepare some reusable arguments for calling sizer.Add():
expandOption = dict(flag=wx.EXPAND)
noOptions = dict()
emptySpace = ((0, 0), noOptions)
# Add the controls to the sizers:
for control, options in \
[(self.nameLabel, noOptions),
(self.nameTextCtrl, expandOption),
(self.allomas2Label, noOptions),
(self.allomas2TextCtrl, expandOption),
emptySpace,
(self.saveButton, dict(flag=wx.ALIGN_CENTER))]:
gridSizer.Add(control, **options)
for control, options in \
[(gridSizer, dict(border=5, flag=wx.ALL)),
(self.logger, dict(border=5, flag=wx.ALL|wx.EXPAND,
proportion=1))]:
boxSizer.Add(control, **options)
self.SetSizerAndFit(boxSizer)
class FrameWithForms(wx.Frame):
def __init__(self, *args, **kwargs):
super(FrameWithForms, self).__init__(*args, **kwargs)
notebook = wx.Notebook(self)
form2 = FormWithSizer(notebook)
notebook.AddPage(form2, 'CALTH')
self.SetClientSize(notebook.GetBestSize())
if __name__ == '__main__':
app = wx.App(0)
frame = FrameWithForms(None, title='Relevant title˝')
frame.Show()
app.MainLoop()
THANK YOU AGAIN!!!!!!!!!!!
First, make sure you can download the data you want with just Python. Then create a wxPython GUI with a single button. In that button's handler, have it call the script that can download the data you want.
If that causes your GUI to become unresponsive, then you'll need to use a thread to do the downloading. I recommend the following articles if that's the case:
http://www.blog.pythonlibrary.org/2010/05/22/wxpython-and-threads/
http://wiki.wxpython.org/LongRunningTasks
Okay, so now you have the data downloading appropriately. Now you add a grid widget or a listctrl / object list view widget. Pick one of those. I prefer object list view, which you can read about here. Then in your button handler you can call your downloader script or thread and when that's done, you can load the widget with that data. If you're using a thread, then the thread will have to call the next step (i.e. the widget loading bit).
Now you should have your data displayed. All that's left is making it look pretty and maybe putting the downloading part into a menu item.
Searching on the documentation provided by google and browsing SO I haven't found a way to retrieve the choices set on a db.Property object (I want to retrieve it in order to create forms based on the model).
I'm using the following recipe to do what I need, Is this correct? Is there any other way of doing it? (simpler, more elegant, more pythonic, etc.)
For a model like this:
class PhoneNumber(db.Model):
contact = db.ReferenceProperty(Contact,
collection_name='phone_numbers')
phone_type = db.StringProperty(choices=('home', 'work'))
number = db.PhoneNumberProperty()
I do the following modification:
class PhoneNumber(db.Model):
_phone_types = ('home', 'work')
contact = db.ReferenceProperty(Contact,
collection_name='phone_numbers')
phone_type = db.StringProperty(choices=_phone_types)
number = db.PhoneNumberProperty()
#classmethod
def get_phone_types(self):
return self._phone_types
You should be able to use PhoneNumber.phone_type.choices. If you want you could make that into a class method too:
#classmethod
def get_phone_types(class_):
return class_.phone_type.choices
You can decide if you prefer the class method approach or not.
Don't forget about Python's dir built-in! It is very useful when exploring objects.
I am trying to make general purpose image display class which
receives two parameters,
Key, and "Column location info in entity"
from URL and return and display specified image delivered from blob.
If you know what I am doing wrong, please give me a hint.
I have datastore "item" like below,
Key | image_index | image1 | image2 | image3 |
and I am requesting image with the URL like below,
http://stackoverwlow.com/image/{key}/{image_N}
I made url handler in the main like below,
def main():
application = webapp.WSGIApplication(
[('/', MainPage),
('/image/([^/]+)/([^/]+)', imageDisplay),
], debug=True)
wsgiref.handlers.CGIHandler().run(application)
and
I made imageDisplay class like below,
from google.appengine.ext import db
from google.appengine.ext import webapp
class imageDisplay(webapp.RequestHandler):
def get(self, _key, _size):
image = db.get(_key)
self.response.headers['Content-Type'] = 'image/jpg'
self.response.out.write(image._size)
But, if I try this code, it will return following error,
global name 'image_size' is not defined
If I specify which image in the entity should be displayed, it works.
So, the data is there.
self.response.out.write(image.image3)
My question is, how to specify "_size" from obtained entity "image"?
Thank you in advance.
With best regards
You need to fetch the attribute programmatically, like this:
self.response.out.write(getattr(image, size))
Since you're addressing your values like an array, you should probably just use one, though: use a db.ListProperty(db.Blob), instead!
I'm using the webapp framework from google for this. I'm using template.render() in a get method to render a template for me.
I'm using the following code to do this for me
path = os.path.join(os.path.dirname(__file__), file_name)
self.response.out.write(template.render(path, template_values))
Where file_name is the template to render and template_values is a dict() containing any values to be rendered. What if I don't have any values that I want rendered. Do I just pass in an empty dict() object? It doesn't seem like a great solution to me. Should I be using template.load() instead?
(I can't find the docs for the template class over on google app engine either, hence I'm asking.)
You can pass an empty dictionary to it and it doesn't mind. You just have to send it something. Your templates just won't display anything.
template_values = {}
path = os.path.join(os.path.dirname(__file__), file_name)
self.response.out.write(template.render(path, template_values))
Okay, thanks for all the answers.
What I've done is:
def render_template(template_name, template_values=dict()):
path = os.path.join(os.path.dirname(__file__), template_name)
self.response.out.write(template.render(path, template_values))
Which seems to be the most pythonic solution I could come up with.
Since you are rendering a Django template, you need to use render, and you probably can't provide an empty dictionary, as it will complain about not being able to find the variables it expects, unless you enclose each variable reference in an {% if %} block. You should provide a dictionary with all of the keys that the template expects but with empty strings as values.
If you have no template variables to pass in, just pass an empty dictionary. If you do use any variables in the template, they will all evaluate as None.
To make this easier, you could modify your helper code:
def render_template(template_name, template_values = None):
if template_values is None:
template_values = {}
path = os.path.join(os.path.dirname(__file__), template_name)
self.response.out.write(template.render(path, template_values))