Skip to content
This repository has been archived by the owner on Sep 29, 2023. It is now read-only.

Feature/guessing language via accept language header #184

Open
wants to merge 9 commits into
base: develop
Choose a base branch
from
45 changes: 39 additions & 6 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,16 +362,33 @@ def selectLanguage( self, path ):
tmppath = [ urlparse.unquote( x ) for x in tmppath.lower().strip("/").split("/") ]
if len( tmppath )>0 and tmppath[0] in conf["viur.availableLanguages"]+list( conf["viur.languageAliasMap"].keys() ):
self.language = tmppath[0]
return( path[ len( tmppath[0])+1: ] ) #Return the path stripped by its language segment
return path[ len( tmppath[0])+1: ] # Return the path stripped by its language segment
else: # This URL doesnt contain an language prefix, try to read it from session
if session.current.getLanguage():
self.language = session.current.getLanguage()
elif "X-Appengine-Country" in self.request.headers.keys():
sessionLang = session.current.getLanguage()
if sessionLang:
self.language = sessionLang
return path
if "Accept-Language" in self.request.headers:
acceptLangHeader = self.request.headers["Accept-Language"]
if acceptLangHeader:
acceptLangHeader = acceptLangHeader.split(",")
# we only accept up to seven language entries here.
for possibleLang in acceptLangHeader[:7]:
lng = possibleLang.split("-")[0]
if lng in conf["viur.availableLanguages"] or lng in conf["viur.languageAliasMap"]:
self.language = lng
return path
elif ";" in lng:
lng = lng.split(";")[0]
if lng in conf["viur.availableLanguages"] or lng in conf["viur.languageAliasMap"]:
self.language = lng
return path
if "X-Appengine-Country" in self.request.headers.keys():
lng = self.request.headers["X-Appengine-Country"].lower()
if lng in conf["viur.availableLanguages"] or lng in conf["viur.languageAliasMap"]:
self.language = lng
return( path )

return path
return path

def processRequest( self, path, *args, **kwargs ): #Bring up the enviroment for this request, handle errors
self.internalRequest = False
Expand All @@ -383,6 +400,22 @@ def processRequest( self, path, *args, **kwargs ): #Bring up the enviroment for
self.args = []
self.kwargs = {}
#Add CSP headers early (if any)

if conf["viur.earlyInterceptionHook"]:
try:
newPath = conf["viur.earlyInterceptionHook"](path)
if newPath != path:
self.response.clear()
self.response.set_status(410, "Gone")
self.response.out.write(newPath)
return
except errors.Redirect as err:
if conf["viur.debug.traceExceptions"]:
raise
logging.debug("early redirect: %r", err.url.encode("UTF-8"))
self.redirect(err.url.encode("UTF-8"))
return

if conf["viur.security.contentSecurityPolicy"] and conf["viur.security.contentSecurityPolicy"]["_headerCache"]:
for k,v in conf["viur.security.contentSecurityPolicy"]["_headerCache"].items():
self.response.headers[k] = v
Expand Down
1 change: 0 additions & 1 deletion bones/textBone.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ def handle_data(self, data):
.replace(">", ">") \
.replace("\"", """) \
.replace("'", "'") \
.replace("\n", "") \
.replace("\0", "")
if data.strip():
self.flushCache()
Expand Down
3 changes: 2 additions & 1 deletion modules/cart.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ def view(self, *args, **kwargs):
items = SkelList( self.productSkel )

for skel in items:
skel["amt"] = numericBone(
skel.clone()
skel.amt = numericBone(
descr="Quantity",
defaultValue=session.current["cart_products"][str(skel["key"].value)]["amount"])

Expand Down
2 changes: 1 addition & 1 deletion modules/order.py
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,7 @@ def archiveOrder(self, order):
self.sendOrderArchivedEMail( order.key.urlsafe() )
logging.error("Order archived: "+str( order.key.urlsafe() ) )

@PeriodicTask(60*24)
# @PeriodicTask(60*24)
def startArchiveOrdersTask( self, *args, **kwargs ):
self.doArchiveActiveOrdersTask( (datetime.now()-self.archiveDelay).strftime("%d.%m.%Y %H:%M:%S"), None )
self.doArchiveCancelledOrdersTask( (datetime.now()-self.archiveDelay).strftime("%d.%m.%Y %H:%M:%S"), None )
Expand Down
6 changes: 4 additions & 2 deletions tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,14 @@ def index(self, *args, **kwargs):
continue
except db.EntityNotFoundError:
pass

res = self.findBoundTask( task )

if res: #Its bound, call it this way :)
t, s = res
t( s )
res[0]()
else:
task() #It seems it wasnt bound - call it as a static method

logging.debug("Successfully called task %s" % task.periodicTaskName )
if intervall:
# Update its last-call timestamp
Expand Down
9 changes: 2 additions & 7 deletions template/editform_bone_select.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,9 @@
>
{%- for key, descr in boneParams["values"].items() %}
{% if loop.first %}
<option value="" {{ "selected" if not boneValue }} {{ "disabled" if boneParams.required}} hidden>
-
</option>
<option value="" {{ "selected" if not boneValue }} {{ "disabled" if boneParams.required}} hidden>-</option>
{% endif %}

<option value="{{ key }}" {{ "selected" if boneValue == key }}>
{{ descr }}
</option>
<option value="{{ key }}" {{ "selected" if boneValue == key }}>{{ descr }}</option>
{% endfor -%}
</select>
{% endif %}