Browse Source

Updated python API+test

master
Artyom Beilis 6 years ago
parent
commit
30fb4ed94e
19 changed files with 283 additions and 8 deletions
  1. +51
    -8
      contrib/integration/session/python/cppcms.py
  2. +1
    -0
      contrib/integration/session/python/cppcmstest/config.js
  3. BIN
      contrib/integration/session/python/cppcmstest/cppcmstest/.urls.py.swp
  4. +0
    -0
      contrib/integration/session/python/cppcmstest/cppcmstest/__init__.py
  5. +84
    -0
      contrib/integration/session/python/cppcmstest/cppcmstest/settings.py
  6. +12
    -0
      contrib/integration/session/python/cppcmstest/cppcmstest/urls.py
  7. +14
    -0
      contrib/integration/session/python/cppcmstest/cppcmstest/wsgi.py
  8. +1
    -0
      contrib/integration/session/python/cppcmstest/html
  9. +10
    -0
      contrib/integration/session/python/cppcmstest/manage.py
  10. +5
    -0
      contrib/integration/session/python/cppcmstest/run.sh
  11. BIN
      contrib/integration/session/python/cppcmstest/tester/.urls.py.swp
  12. BIN
      contrib/integration/session/python/cppcmstest/tester/.views.py.swp
  13. +0
    -0
      contrib/integration/session/python/cppcmstest/tester/__init__.py
  14. +3
    -0
      contrib/integration/session/python/cppcmstest/tester/admin.py
  15. +3
    -0
      contrib/integration/session/python/cppcmstest/tester/models.py
  16. +3
    -0
      contrib/integration/session/python/cppcmstest/tester/tests.py
  17. +7
    -0
      contrib/integration/session/python/cppcmstest/tester/urls.py
  18. +88
    -0
      contrib/integration/session/python/cppcmstest/tester/views.py
  19. +1
    -0
      contrib/integration/session/wwwtest/index.html

+ 51
- 8
contrib/integration/session/python/cppcms.py View File

@@ -283,6 +283,46 @@ class Session(SessionBase):
Loader.capi.cppcms_capi_session_set_exposed(self.d,key,v)
self.check()

def get_age(self):
r = Loader.capi.cppcms_capi_session_get_age(self.d);
self.check();
print 'GOT AGE',r
return r;
def set_age(self,value):
Loader.capi.cppcms_capi_session_set_age(self.d,int(value))
self.check()
print 'SET AGE',int(value)
def default_age(self):
print 'DEF AGE'
Loader.capi.cppcms_capi_session_set_default_age(self.d)
self.check()

def get_expiration(self):
r = Loader.capi.cppcms_capi_session_get_expiration(self.d);
self.check();
return r;
def set_expiration(self,value):
Loader.capi.cppcms_capi_session_set_expiration(self.d,int(value))
self.check()
def default_expiration(self):
Loader.capi.cppcms_capi_session_set_default_expiration(self.d)
self.check()

def get_on_server(self):
r = Loader.capi.cppcms_capi_session_get_on_server(self.d);
self.check();
return r;
def set_on_server(self,value):
Loader.capi.cppcms_capi_session_set_on_server(self.d,int(value))
self.check()

def reset_session(self):
Loader.capi.cppcms_capi_session_reset_session(self.d)
self.check()

@property
def keys(self):
"""Get list of all keys"""
l=[]
@@ -301,7 +341,8 @@ class Session(SessionBase):
r=Loader.capi.cppcms_capi_session_cookie_next(self.d)
self.check()
return l
def get_csrf_token(self):
@property
def csrf_token(self):
"""Get cppcms CSRF token"""
r=Loader.capi.cppcms_capi_session_get_csrf_token(self.d)
self.check()
@@ -330,7 +371,8 @@ class Session(SessionBase):
"""Set a value for a key"""
Loader.capi.cppcms_capi_session_set(self.d,key,value)
self.check()
def get_session_cookie_name(self):
@property
def session_cookie_name(self):
"""
Get the name of the cookie that is used to store CppCMS session
Note: the value of this cookie should be passed to load method
@@ -351,13 +393,14 @@ class Session(SessionBase):
Loader.capi.cppcms_capi_session_set_session_cookie(self.d,cookie);
Loader.capi.cppcms_capi_session_load(self.d)
elif django_request!=None:
cookie_name = self.get_session_cookie_name()
cookie_name = self.session_cookie_name
cookie=''
if cookie_name in django_request.COOKIES:
cookie = django_request.COOKIES[cookie_name]
for cookie_name in django_request.COOKIES:
Loader.capi.cppcms_capi_session_add_cookie_name(self.d,cookie_name)
Loader.capi.cppcms_capi_session_load(self.d,cookie)
Loader.capi.cppcms_capi_session_set_session_cookie(self.d,cookie)
Loader.capi.cppcms_capi_session_load(self.d)
self.check()
def save(self,django_response=None):
"""
@@ -422,14 +465,14 @@ def __private_test(config):
binary.extend(b'\x01\x00\xFF\x7F')
s.set_binary('z',binary)
s.set_exposed('x',1)
for k in s.keys():
for k in s.keys:
print ('Got ' + k)
print ('Value ' + s.get(k))
s.save()
for c in s.cookies():
print (c)
print (c.value())
if(c.name()==s.get_session_cookie_name()):
if(c.name()==s.session_cookie_name):
state = c.value()
l=None
s=None
@@ -444,7 +487,7 @@ def __private_test(config):
for c in s.cookies():
print (c)
print (c.value())
if(c.name()==s.get_session_cookie_name()):
if(c.name()==s.session_cookie_name):
state = c.value()
s=None
s=Session(p)
@@ -455,7 +498,7 @@ def __private_test(config):
for c in s.cookies():
print (c)
print (c.value())
if(c.name()==s.get_session_cookie_name()):
if(c.name()==s.session_cookie_name):
state = c.value()
print "Test Completed"



+ 1
- 0
contrib/integration/session/python/cppcmstest/config.js View File

@@ -0,0 +1 @@
../../reference/config.js

BIN
contrib/integration/session/python/cppcmstest/cppcmstest/.urls.py.swp View File


+ 0
- 0
contrib/integration/session/python/cppcmstest/cppcmstest/__init__.py View File


+ 84
- 0
contrib/integration/session/python/cppcmstest/cppcmstest/settings.py View File

@@ -0,0 +1,84 @@
"""
Django settings for cppcmstest project.

For more information on this file, see
https://docs.djangoproject.com/en/1.6/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.6/ref/settings/
"""

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '_@k=vu7op_%=76i=(bnea3(oe#3nn4+h!#^xpna8mpw$w9w3xz'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
)

MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

ROOT_URLCONF = 'cppcmstest.urls'

WSGI_APPLICATION = 'cppcmstest.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.6/ref/settings/#databases

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}

# Internationalization
# https://docs.djangoproject.com/en/1.6/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/

STATIC_URL = '/html/'
STATICFILES_DIRS = [ os.path.join(BASE_DIR, "html") ]
#STATIC_ROOT = os.path.join(BASE_DIR, "static")

+ 12
- 0
contrib/integration/session/python/cppcmstest/cppcmstest/urls.py View File

@@ -0,0 +1,12 @@
from django.conf.urls import patterns, include, url

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
# Examples:
# url(r'^$', 'cppcmstest.views.home', name='home'),
url(r'^tester/', include('tester.urls')),

url(r'^admin/', include(admin.site.urls)),
)

+ 14
- 0
contrib/integration/session/python/cppcmstest/cppcmstest/wsgi.py View File

@@ -0,0 +1,14 @@
"""
WSGI config for cppcmstest project.

It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/1.6/howto/deployment/wsgi/
"""

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "cppcmstest.settings")

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

+ 1
- 0
contrib/integration/session/python/cppcmstest/html View File

@@ -0,0 +1 @@
../../wwwtest/

+ 10
- 0
contrib/integration/session/python/cppcmstest/manage.py View File

@@ -0,0 +1,10 @@
#!/usr/bin/env python
import os
import sys

if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "cppcmstest.settings")

from django.core.management import execute_from_command_line

execute_from_command_line(sys.argv)

+ 5
- 0
contrib/integration/session/python/cppcmstest/run.sh View File

@@ -0,0 +1,5 @@
#!/bin/bash

export LD_LIBRARY_PATH=/opt/cppcms/lib/
export PYTHONPATH=..
python manage.py runserver 8000

BIN
contrib/integration/session/python/cppcmstest/tester/.urls.py.swp View File


BIN
contrib/integration/session/python/cppcmstest/tester/.views.py.swp View File


+ 0
- 0
contrib/integration/session/python/cppcmstest/tester/__init__.py View File


+ 3
- 0
contrib/integration/session/python/cppcmstest/tester/admin.py View File

@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.

+ 3
- 0
contrib/integration/session/python/cppcmstest/tester/models.py View File

@@ -0,0 +1,3 @@
from django.db import models

# Create your models here.

+ 3
- 0
contrib/integration/session/python/cppcmstest/tester/tests.py View File

@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.

+ 7
- 0
contrib/integration/session/python/cppcmstest/tester/urls.py View File

@@ -0,0 +1,7 @@
from django.conf.urls import url

from . import views

urlpatterns = [
url(r'^$', views.home, name='home'),
]

+ 88
- 0
contrib/integration/session/python/cppcmstest/tester/views.py View File

@@ -0,0 +1,88 @@
from django.http import HttpResponse
from django.conf import settings
import cppcms
#
# Create the session pool - note it is thread safe and should be one per projects
# Provide a path to configuration file
#
pool=cppcms.SessionPool(settings.BASE_DIR + '/config.js')

def bin2hex(val):
return ''.join('{:02x}'.format(x) for x in val)
def hex2bin(val):
return bytearray.fromhex(val)

def home(request):
session=pool.session()
session.load(django_request=request)
i = 0
output = []
while True:
i=i+1
id = "_" + str(i)
op_id = 'op' + id
key_id = 'key' + id
value_id = 'value' + id
print 'Checking ', op_id
if not op_id in request.GET:
break
op = request.GET[op_id]
if key_id in request.GET:
key = request.GET[key_id]
if value_id in request.GET:
value = request.GET[value_id]
result = 'ok'
if op=="is_set":
result = 'yes' if key in session else 'no'
elif op == "erase":
del session[key]
elif op == "clear":
session.clear();
elif op == "is_exposed":
result = 'yes' if session.get_exposed(key) else "no";
elif op == "expose":
session.set_exposed(key,int(value));
elif op == "get":
result = session[key];
elif op == "set":
session[key]=value;
elif op == "get_binary":
result = bin2hex(session.get_binary(key));
elif op == "set_binary":
session.set_binary(key,hex2bin(value));
elif op == "get_age":
result = str(session.get_age())
elif op == "set_age":
session.set_age(value)
print 'SET AGE DONE', value
elif op == "default_age":
session.default_age();
elif op == "get_expiration":
result = str(session.get_expiration())
elif op == "set_expiration":
session.set_expiration(int(value))
elif op == "default_expiration":
session.default_expiration();
elif op == "get_on_server":
result = 'yes' if session.get_on_server() else "no"
elif op == "set_on_server":
session.set_on_server(int(value))
elif op == "reset_session":
session.reset_session();
elif op == "csrf_token":
result = "t=" + session.csrf_token;
elif op == "keys":
ks=[]
for key in session.keys:
ks.append('[' + key + ']')
result = ','.join(ks)
else:
result = "invalid op=" + op;
msg = str(i) + ':' + result + ';'
print 'Res ' + msg
output.append(msg);
response = HttpResponse()
session.save(django_response=response)
response.write(''.join(output));
return response

+ 1
- 0
contrib/integration/session/wwwtest/index.html View File

@@ -15,6 +15,7 @@ function get_url()
<option value="/unit_test.php">PHP</option>
<option value="/jct/test" >Java</option>
<option value="/test.aspx" >ASP.Net</option>
<option value="/tester" >DJango</option>
</select>
<button onclick="tester(get_url())">Run Test</button><span id="wait" style="color:green"></span>
<table border='1' width='80%'>


Loading…
Cancel
Save