<commit_before>#!/usr/bin/python

# Usage: Train_Evaluate_7MLP-100PCA.py <label> <target-variable> <data-sources> <train-set-sessions> <test-set-sessions>
# NB: Normally, use quotes around each parameter, as they are strings with commas and stuff

# The script assumes that the dataset is in the same dir as the script, and writes its output to a file <label>.RData
# The output contains the trained model (model), args = commandArgs(trailingOnly=TRUE)

import sys
import os
import numpy
import pandas
import json

from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasClassifier
from keras.layers import Dropout
from keras.constraints import maxnorm
from keras.optimizers import SGD
from keras.utils.np_utils import to_categorical
from keras.callbacks import ModelCheckpoint

from sklearn.metrics import confusion_matrix, roc_auc_score, accuracy_score, f1_score, cohen_kappa_score
from sklearn.cross_validation import cross_val_score
from sklearn.preprocessing import LabelEncoder
from sklearn.cross_validation import StratifiedKFold
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline
from sklearn import decomposition

print 'Argument List:', str(sys.argv)

if(len(sys.argv)!=6):
  sys.exit("Wrong number of arguments. Usage:\nTrain_Evaluate_7MLP-100PCA.py <label> <target-variable> <data-sources> <train-set-sessions> <test-set-sessions>")

label = sys.argv[1]
target = sys.argv[2] # either 'Activity' or 'Social'
datasourcestring = sys.argv[3] # Comma separated combinations of all,et,acc,aud,vid
trainstring = sys.argv[4]
teststring = sys.argv[5]
# # For tests
# label='7MLP-100PCA_et_GM_LOSO_Activity_1'
# target='Activity'
# datasourcestring='et'
# trainstring='case1-day1-session2-teacher1,case1-day1-session3-teacher1,case1-day1-session4-teacher1,case2-day1-session1-teacher2,case2-day1-session2-teacher2,case2-day2-session1-teacher2,case2-day2-session2-teacher2,case2-day3-session1-teacher2,case2-day3-session2-teacher2,case2-day4-session1-teacher2,case2-day4-session2-teacher2'
# teststring='case1-day1-session1-teacher1'

# We parse the data sources to take into account, and sessions for the train and test sets
features = range(0,2) # These are only the session and timestamp
sources = datasourcestring.split(",")
for source in sources:
    if(source=='all'):
        features.extend(range(2,7557))
        break
    elif(source=='et'):
        features.extend(range(2,12))
    elif(source=='acc'):
        features.extend(range(12,152))
    elif(source=='aud'):
        features.extend(range(152,6557))
    elif(source=='vid'):
        features.extend(range(6557,7557))
    else:
        sys.exit("Wrong data sources. Possible values: all,et,acc,aud,vid")
features.extend(range(7557,7559)) # Add activity and Social
print("Selected features: "+str(len(features)))

sessiontrain = trainstring.split(",") # Gives an array of the sessions to train in
sessiontest = teststring.split(",") # Gives an array of the sessions to train in

if(len(sessiontrain)==0 | len(sessiontest)==0):
    sys.exit("Wrong train/test sessions specification. Should be a comma-separated string with the sessions identificators")

path = os.path.dirname(os.path.realpath(sys.argv[0]))
# READING AND PREPARING THE DATA
processeddatadir = path
#processeddatadir = "../src/models" # TODO: Change this for the actual script
datafile = os.path.join(processeddatadir,'completeDataset.csv')
gzdatafile = os.path.join(processeddatadir,'completeDataset.csv.gz')
fulldata = pandas.DataFrame()
if(os.path.isfile(datafile)):
    fulldata = pandas.read_csv(datafile, sep=',', quotechar='"')
elif(os.path.isfile(gzdatafile)):
    fulldata = pandas.read_csv(gzdatafile, compression='gzip', sep=',', quotechar='"')
else:
    sys.exit("Data not available in the script's folder")

# Drop the useless first column
fulldata.drop(fulldata.columns[[0]],axis=1,inplace=True)

def cleanAct(value):
    if pandas.isnull(value):
        return 'Other'
    elif value=='OFF' or value=='TDT' or value=='TEC':
        return 'Other'
    else:
        return value

def cleanSoc(value):
    if pandas.isnull(value):
        return 'Other'
    else:
        return value

# We only look for predicting 4 states of activity and 3 of social, the rest (incl.NA) we bunch in 'Other' (so in the end it is a 5- and 4-class classification problem)
fulldata['Activity'] = fulldata['Activity.win'].map(cleanAct)
fulldata['Social'] = fulldata['Social.win'].map(cleanSoc)

# Drop the useless first column
fulldata.drop(fulldata.columns[[2,3,4]],axis=1,inplace=True)
print(fulldata.columns.values[0:5],"...",fulldata.columns.values[-5:])
#fulldata.head(3)

# Now the column indices match what is expected in the arguments parsed above
# * [,0]: ''session id''
# * [,1]: ''timestamp'' within the session (in ms)
# * [,2:12]: ''eyetracking'' features (mean/sd pupil diameter, nr. of long fixations, avg. saccade speed, fixation duration, fixation dispersion, saccade duration, saccade amplitude, saccade length, saccade velocity)
# * [,12:152]: ''accelerometer'' features, including X, Y, Z (mean, sd, max, min, median, and 30 FFT coefficients of each of them) and jerk (mean, sd, max, min, median, and 30 FFT coefficients of each of it)
# * [,152:6557]: ''audio'' features extracted from an audio snippet of the 10s window, using openSMILE. Includes features about whether there is someone speaking (153:163), emotion recognition models (164:184), and brute-force audio spectrum features and characteristics used in various audio recognition challenges/tasks (185:6557)
# * [,6557:7557]: ''video'' features extracted from an image taken in the middle of the window (the 1000 values of the last layer when passing the immage through a VGG pre-trained model)
# * [,7557:7559]: ''Activity,Social'' labels we want to predict

# SELECTING THE DATASET FEATURES (DATA SOURCES BEING TRIED)
data = fulldata.ix[:,features]

# We drop the non-needed target variable
if target == 'Activity':
    data.drop('Social',axis=1,inplace=True)
elif target == 'Social':
    data.drop('Activity',axis=1,inplace=True)

print(data.shape)
#data.head(3)

# SPLITTING THE DATA
test = data.loc[data['session'].isin(sessiontest)]
train = data.loc[data['session'].isin(sessiontrain)]
print(test.shape)
print(train.shape)
# Removing null values
test = test[test.notnull().all(axis=1)]
train = train[train.notnull().all(axis=1)]
print("Removing null and NAs...")
print(test.shape)
print(train.shape)

X_train = train.values[:,range(2,train.shape[1]-1)].astype(float)
Y_train = train.values[:,(train.shape[1]-1)]
X_test = test.values[:,range(2,test.shape[1]-1)].astype(float)
Y_test = test.values[:,(test.shape[1]-1)]
Y_total = data.values[:,(data.shape[1]-1)]
print(X_train.shape, Y_train.shape, X_test.shape, Y_test.shape)

#######################################################
# DO OTHER DATA TRANSFORMATIONS NEEDED, e.g. PCA, SELECT K-BEST FEATURES, etc (NORMALLY, ON THE TRAIN SET ONLY, TO BE APPLIED LATER TO THE TEST SET)
print("Transforming data... ")
k = 100
outs = len(data[target].unique())

# We standardize on the basis of the training data
scaler = StandardScaler().fit(X_train)
X_train_st = scaler.transform(X_train)
X_test_st = scaler.transform(X_test)

if X_train.shape[1]>k:
    # Apply 100-component pca
    print("PCA with "+str(k)+" components")
    pca = decomposition.PCA(n_components=k)
    X_train_pca = pca.fit_transform(X_train_st)
    X_test_pca = pca.transform(X_test_st)
    print 'Variance explained:'
    print pca.explained_variance_ratio_
    print 'Total variance explained by '+str(k)+' components:'
    print sum(pca.explained_variance_ratio_)
else:
    k=X_train.shape[1]
    X_train_pca = X_train_st
    X_test_pca = X_test_st

#######################################################

# PREPARING THE DATA FOR KERAS TRAINING
# One hot encoding of the response variable (using dummy variables)

# encode class values as integers
encoder = LabelEncoder()
encoder.fit(Y_total)
encoded_Y_train = encoder.transform(Y_train)
# convert integers to dummy variables (i.e. one hot encoded)
dummy_y_train = to_categorical(encoded_Y_train)
#encoder.fit(Y_test)
encoded_Y_test = encoder.transform(Y_test)
# convert integers to dummy variables (i.e. one hot encoded)
dummy_y_test = to_categorical(encoded_Y_test)

##################### MODEL BUILDING --- CUSTOMIZE THIS ################
# Apply dropout regularization, it is overfitting!
def create_deeper_dropout_decay_PCA(k, outs):
    # create model
    model = Sequential()
    model.add(Dropout(0.2, input_shape=(k,)))
    model.add(Dense(300, init='uniform', activation='tanh'))
    model.add(Dropout(0.2))
    model.add(Dense(300, init='uniform', activation='tanh'))
    model.add(Dropout(0.2))
    model.add(Dense(80, init='uniform', activation='tanh'))
    model.add(Dropout(0.2))
    model.add(Dense(80, init='uniform', activation='tanh'))
    model.add(Dropout(0.2))
    model.add(Dense(20, init='uniform', activation='tanh'))
    model.add(Dropout(0.2))
    model.add(Dense(20, init='uniform', activation='tanh'))
    model.add(Dropout(0.2))
    model.add(Dense(outs, init='uniform', activation='sigmoid'))
    # Compile model, with larger learning rate and momentum, as recommended by the original paper
    sgd = SGD(lr=0.1, momentum=0.8, decay=0.0001, nesterov=False)
    model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
    return model

# evaluate baseline model with standardized dataset
# fix random seed for reproducibility
seed = 66
numpy.random.seed(seed)
#estimators = []
#estimators.append(('standardize', StandardScaler()))
#estimators.append(('mlp', KerasClassifier(build_fn=create_baseline, nb_epoch=10, batch_size=10, verbose=1)))
# We define a pipeline of estimators, in which first the scaler is fitted to the data, then the MLP is applied
#pipeline = Pipeline(estimators)
#kfold = StratifiedKFold(y=Y_train, n_folds=3, shuffle=True, random_state=seed)

#model = create_baseline()
model = create_deeper_dropout_decay_PCA(k, outs)
#print model.summary()
#############################################################################

# To save the best model
# serialize model to JSON
model_json = model.to_json()
modelfile = os.path.join(path,label+".model.json")
with open(modelfile, "w") as json_file:
    json_file.write(model_json)
filepath=os.path.join(path,label+".weights.hdf5")
# Define that the accuracy in cv is monitored, and that weights are stored in a file when max accuracy is achieved
checkpoint = ModelCheckpoint(filepath, monitor='val_acc', verbose=1, save_best_only=True, mode='max')
callbacks_list = [checkpoint]

# Fit the model
history = model.fit(X_train_pca, dummy_y_train, validation_data=(X_test_pca,dummy_y_test),
                    nb_epoch=500, batch_size=10, verbose=0, callbacks=callbacks_list)
#results = cross_val_score(pipeline, X_train, dummy_y_train, cv=kfold)
#print("Standardized data Acc (in CV training data): %.2f%% (%.2f%%)" % (results.mean()*100, results.std()*100))
# evaluate the model
#scores = pipeline.evaluate(X_test, dummy_y_test)
#print pipeline.metrics_names[1]
#print scores[1]*100
# For other metrics, see http://machinelearningmastery.com/metrics-evaluate-machine-learning-algorithms-python/

# Other performance/accuracy metrics
Y_pred = model.predict(X_test_pca)
print Y_pred.shape

# Accuracy
print('Accuracy:')
acc = accuracy_score(numpy.argmax(dummy_y_test, axis=1), numpy.argmax(Y_pred, axis=1))
print(acc)

# Confusion matrix
cm = confusion_matrix(numpy.argmax(dummy_y_test, axis=1), numpy.argmax(Y_pred, axis=1))
numpy.set_printoptions(precision=2)
print('Confusion matrix:')
print(cm)

# AUC
roc = NaN
try:
  roc = roc_auc_score(dummy_y_test, Y_pred, average='macro')
except:
  pass
print('AUC score:')
print(roc)

# F1
f1= f1_score(numpy.argmax(dummy_y_test, axis=1), numpy.argmax(Y_pred, axis=1), average='macro')
print('F1 score:')
print(f1)

# KAppa?
kappa = cohen_kappa_score(numpy.argmax(dummy_y_test, axis=1), numpy.argmax(Y_pred, axis=1))
print('Kappa score:')
print(kappa)

#import matplotlib
#matplotlib.use('Agg')
#import matplotlib.pyplot as plt
# summarize history for accuracy
#plt.plot(history.history['acc'])
#plt.plot(history.history['val_acc'])
#plt.title('model accuracy')
#plt.ylabel('accuracy')
#plt.xlabel('epoch')
#plt.legend(['train','test'], loc='upper left')
#plt.show()
# summarize history for loss
#plt.plot(history.history['loss'])
#plt.plot(history.history['val_loss'])
#plt.title('model loss')
#plt.ylabel('loss')
#plt.xlabel('epoch')
#plt.legend(['train','test'], loc='upper left')
#plt.show()

# The models are stored already... what about the performance metrics?
# Store them in a csv
perfdata = [
    {'label': label, 'cm': json.dumps(cm.tolist()), 'acc': acc, 'auc': roc, 'f1': f1, 'kappa': kappa}
]
# Later, to recover cm:
# cm = np.array(json.loads(text))
df = pandas.DataFrame(perfdata)
filename = os.path.join(path,label+".perf.csv")
df.to_csv(filename, index=False, encoding='utf-8')
<commit_msg>Small bugfix<commit_after>#!/usr/bin/python

# Usage: Train_Evaluate_7MLP-100PCA.py <label> <target-variable> <data-sources> <train-set-sessions> <test-set-sessions>
# NB: Normally, use quotes around each parameter, as they are strings with commas and stuff

# The script assumes that the dataset is in the same dir as the script, and writes its output to a file <label>.RData
# The output contains the trained model (model), args = commandArgs(trailingOnly=TRUE)

import sys
import os
import numpy
import pandas
import json

from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasClassifier
from keras.layers import Dropout
from keras.constraints import maxnorm
from keras.optimizers import SGD
from keras.utils.np_utils import to_categorical
from keras.callbacks import ModelCheckpoint

from sklearn.metrics import confusion_matrix, roc_auc_score, accuracy_score, f1_score, cohen_kappa_score
from sklearn.cross_validation import cross_val_score
from sklearn.preprocessing import LabelEncoder
from sklearn.cross_validation import StratifiedKFold
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline
from sklearn import decomposition

print 'Argument List:', str(sys.argv)

if(len(sys.argv)!=6):
  sys.exit("Wrong number of arguments. Usage:\nTrain_Evaluate_7MLP-100PCA.py <label> <target-variable> <data-sources> <train-set-sessions> <test-set-sessions>")

label = sys.argv[1]
target = sys.argv[2] # either 'Activity' or 'Social'
datasourcestring = sys.argv[3] # Comma separated combinations of all,et,acc,aud,vid
trainstring = sys.argv[4]
teststring = sys.argv[5]
# # For tests
# label='7MLP-100PCA_et_GM_LOSO_Activity_1'
# target='Activity'
# datasourcestring='et'
# trainstring='case1-day1-session2-teacher1,case1-day1-session3-teacher1,case1-day1-session4-teacher1,case2-day1-session1-teacher2,case2-day1-session2-teacher2,case2-day2-session1-teacher2,case2-day2-session2-teacher2,case2-day3-session1-teacher2,case2-day3-session2-teacher2,case2-day4-session1-teacher2,case2-day4-session2-teacher2'
# teststring='case1-day1-session1-teacher1'

# We parse the data sources to take into account, and sessions for the train and test sets
features = range(0,2) # These are only the session and timestamp
sources = datasourcestring.split(",")
for source in sources:
    if(source=='all'):
        features.extend(range(2,7557))
        break
    elif(source=='et'):
        features.extend(range(2,12))
    elif(source=='acc'):
        features.extend(range(12,152))
    elif(source=='aud'):
        features.extend(range(152,6557))
    elif(source=='vid'):
        features.extend(range(6557,7557))
    else:
        sys.exit("Wrong data sources. Possible values: all,et,acc,aud,vid")
features.extend(range(7557,7559)) # Add activity and Social
print("Selected features: "+str(len(features)))

sessiontrain = trainstring.split(",") # Gives an array of the sessions to train in
sessiontest = teststring.split(",") # Gives an array of the sessions to train in

if(len(sessiontrain)==0 | len(sessiontest)==0):
    sys.exit("Wrong train/test sessions specification. Should be a comma-separated string with the sessions identificators")

path = os.path.dirname(os.path.realpath(sys.argv[0]))
# READING AND PREPARING THE DATA
processeddatadir = path
#processeddatadir = "../src/models" # TODO: Change this for the actual script
datafile = os.path.join(processeddatadir,'completeDataset.csv')
gzdatafile = os.path.join(processeddatadir,'completeDataset.csv.gz')
fulldata = pandas.DataFrame()
if(os.path.isfile(datafile)):
    fulldata = pandas.read_csv(datafile, sep=',', quotechar='"')
elif(os.path.isfile(gzdatafile)):
    fulldata = pandas.read_csv(gzdatafile, compression='gzip', sep=',', quotechar='"')
else:
    sys.exit("Data not available in the script's folder")

# Drop the useless first column
fulldata.drop(fulldata.columns[[0]],axis=1,inplace=True)

def cleanAct(value):
    if pandas.isnull(value):
        return 'Other'
    elif value=='OFF' or value=='TDT' or value=='TEC':
        return 'Other'
    else:
        return value

def cleanSoc(value):
    if pandas.isnull(value):
        return 'Other'
    else:
        return value

# We only look for predicting 4 states of activity and 3 of social, the rest (incl.NA) we bunch in 'Other' (so in the end it is a 5- and 4-class classification problem)
fulldata['Activity'] = fulldata['Activity.win'].map(cleanAct)
fulldata['Social'] = fulldata['Social.win'].map(cleanSoc)

# Drop the useless first column
fulldata.drop(fulldata.columns[[2,3,4]],axis=1,inplace=True)
print(fulldata.columns.values[0:5],"...",fulldata.columns.values[-5:])
#fulldata.head(3)

# Now the column indices match what is expected in the arguments parsed above
# * [,0]: ''session id''
# * [,1]: ''timestamp'' within the session (in ms)
# * [,2:12]: ''eyetracking'' features (mean/sd pupil diameter, nr. of long fixations, avg. saccade speed, fixation duration, fixation dispersion, saccade duration, saccade amplitude, saccade length, saccade velocity)
# * [,12:152]: ''accelerometer'' features, including X, Y, Z (mean, sd, max, min, median, and 30 FFT coefficients of each of them) and jerk (mean, sd, max, min, median, and 30 FFT coefficients of each of it)
# * [,152:6557]: ''audio'' features extracted from an audio snippet of the 10s window, using openSMILE. Includes features about whether there is someone speaking (153:163), emotion recognition models (164:184), and brute-force audio spectrum features and characteristics used in various audio recognition challenges/tasks (185:6557)
# * [,6557:7557]: ''video'' features extracted from an image taken in the middle of the window (the 1000 values of the last layer when passing the immage through a VGG pre-trained model)
# * [,7557:7559]: ''Activity,Social'' labels we want to predict

# SELECTING THE DATASET FEATURES (DATA SOURCES BEING TRIED)
data = fulldata.ix[:,features]

# We drop the non-needed target variable
if target == 'Activity':
    data.drop('Social',axis=1,inplace=True)
elif target == 'Social':
    data.drop('Activity',axis=1,inplace=True)

print(data.shape)
#data.head(3)

# SPLITTING THE DATA
test = data.loc[data['session'].isin(sessiontest)]
train = data.loc[data['session'].isin(sessiontrain)]
print(test.shape)
print(train.shape)
# Removing null values
test = test[test.notnull().all(axis=1)]
train = train[train.notnull().all(axis=1)]
print("Removing null and NAs...")
print(test.shape)
print(train.shape)

X_train = train.values[:,range(2,train.shape[1]-1)].astype(float)
Y_train = train.values[:,(train.shape[1]-1)]
X_test = test.values[:,range(2,test.shape[1]-1)].astype(float)
Y_test = test.values[:,(test.shape[1]-1)]
Y_total = data.values[:,(data.shape[1]-1)]
print(X_train.shape, Y_train.shape, X_test.shape, Y_test.shape)

#######################################################
# DO OTHER DATA TRANSFORMATIONS NEEDED, e.g. PCA, SELECT K-BEST FEATURES, etc (NORMALLY, ON THE TRAIN SET ONLY, TO BE APPLIED LATER TO THE TEST SET)
print("Transforming data... ")
k = 100
outs = len(data[target].unique())

# We standardize on the basis of the training data
scaler = StandardScaler().fit(X_train)
X_train_st = scaler.transform(X_train)
X_test_st = scaler.transform(X_test)

if X_train.shape[1]>k:
    # Apply 100-component pca
    print("PCA with "+str(k)+" components")
    pca = decomposition.PCA(n_components=k)
    X_train_pca = pca.fit_transform(X_train_st)
    X_test_pca = pca.transform(X_test_st)
    print 'Variance explained:'
    print pca.explained_variance_ratio_
    print 'Total variance explained by '+str(k)+' components:'
    print sum(pca.explained_variance_ratio_)
else:
    k=X_train.shape[1]
    X_train_pca = X_train_st
    X_test_pca = X_test_st

#######################################################

# PREPARING THE DATA FOR KERAS TRAINING
# One hot encoding of the response variable (using dummy variables)

# encode class values as integers
encoder = LabelEncoder()
encoder.fit(Y_total)
encoded_Y_train = encoder.transform(Y_train)
# convert integers to dummy variables (i.e. one hot encoded)
dummy_y_train = to_categorical(encoded_Y_train)
#encoder.fit(Y_test)
encoded_Y_test = encoder.transform(Y_test)
# convert integers to dummy variables (i.e. one hot encoded)
dummy_y_test = to_categorical(encoded_Y_test)

##################### MODEL BUILDING --- CUSTOMIZE THIS ################
# Apply dropout regularization, it is overfitting!
def create_deeper_dropout_decay_PCA(k, outs):
    # create model
    model = Sequential()
    model.add(Dropout(0.2, input_shape=(k,)))
    model.add(Dense(300, init='uniform', activation='tanh'))
    model.add(Dropout(0.2))
    model.add(Dense(300, init='uniform', activation='tanh'))
    model.add(Dropout(0.2))
    model.add(Dense(80, init='uniform', activation='tanh'))
    model.add(Dropout(0.2))
    model.add(Dense(80, init='uniform', activation='tanh'))
    model.add(Dropout(0.2))
    model.add(Dense(20, init='uniform', activation='tanh'))
    model.add(Dropout(0.2))
    model.add(Dense(20, init='uniform', activation='tanh'))
    model.add(Dropout(0.2))
    model.add(Dense(outs, init='uniform', activation='sigmoid'))
    # Compile model, with larger learning rate and momentum, as recommended by the original paper
    sgd = SGD(lr=0.1, momentum=0.8, decay=0.0001, nesterov=False)
    model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
    return model

# evaluate baseline model with standardized dataset
# fix random seed for reproducibility
seed = 66
numpy.random.seed(seed)
#estimators = []
#estimators.append(('standardize', StandardScaler()))
#estimators.append(('mlp', KerasClassifier(build_fn=create_baseline, nb_epoch=10, batch_size=10, verbose=1)))
# We define a pipeline of estimators, in which first the scaler is fitted to the data, then the MLP is applied
#pipeline = Pipeline(estimators)
#kfold = StratifiedKFold(y=Y_train, n_folds=3, shuffle=True, random_state=seed)

#model = create_baseline()
model = create_deeper_dropout_decay_PCA(k, outs)
#print model.summary()
#############################################################################

# To save the best model
# serialize model to JSON
model_json = model.to_json()
modelfile = os.path.join(path,label+".model.json")
with open(modelfile, "w") as json_file:
    json_file.write(model_json)
filepath=os.path.join(path,label+".weights.hdf5")
# Define that the accuracy in cv is monitored, and that weights are stored in a file when max accuracy is achieved
checkpoint = ModelCheckpoint(filepath, monitor='val_acc', verbose=1, save_best_only=True, mode='max')
callbacks_list = [checkpoint]

# Fit the model
history = model.fit(X_train_pca, dummy_y_train, validation_data=(X_test_pca,dummy_y_test),
                    nb_epoch=500, batch_size=10, verbose=0, callbacks=callbacks_list)
#results = cross_val_score(pipeline, X_train, dummy_y_train, cv=kfold)
#print("Standardized data Acc (in CV training data): %.2f%% (%.2f%%)" % (results.mean()*100, results.std()*100))
# evaluate the model
#scores = pipeline.evaluate(X_test, dummy_y_test)
#print pipeline.metrics_names[1]
#print scores[1]*100
# For other metrics, see http://machinelearningmastery.com/metrics-evaluate-machine-learning-algorithms-python/

# Other performance/accuracy metrics
Y_pred = model.predict(X_test_pca)
print Y_pred.shape

# Accuracy
print('Accuracy:')
acc = accuracy_score(numpy.argmax(dummy_y_test, axis=1), numpy.argmax(Y_pred, axis=1))
print(acc)

# Confusion matrix
cm = confusion_matrix(numpy.argmax(dummy_y_test, axis=1), numpy.argmax(Y_pred, axis=1))
numpy.set_printoptions(precision=2)
print('Confusion matrix:')
print(cm)

# AUC
roc = None
try:
  roc = roc_auc_score(dummy_y_test, Y_pred, average='macro')
except:
  pass
print('AUC score:')
print(roc)

# F1
f1= f1_score(numpy.argmax(dummy_y_test, axis=1), numpy.argmax(Y_pred, axis=1), average='macro')
print('F1 score:')
print(f1)

# KAppa?
kappa = cohen_kappa_score(numpy.argmax(dummy_y_test, axis=1), numpy.argmax(Y_pred, axis=1))
print('Kappa score:')
print(kappa)

#import matplotlib
#matplotlib.use('Agg')
#import matplotlib.pyplot as plt
# summarize history for accuracy
#plt.plot(history.history['acc'])
#plt.plot(history.history['val_acc'])
#plt.title('model accuracy')
#plt.ylabel('accuracy')
#plt.xlabel('epoch')
#plt.legend(['train','test'], loc='upper left')
#plt.show()
# summarize history for loss
#plt.plot(history.history['loss'])
#plt.plot(history.history['val_loss'])
#plt.title('model loss')
#plt.ylabel('loss')
#plt.xlabel('epoch')
#plt.legend(['train','test'], loc='upper left')
#plt.show()

# The models are stored already... what about the performance metrics?
# Store them in a csv
perfdata = [
    {'label': label, 'cm': json.dumps(cm.tolist()), 'acc': acc, 'auc': roc, 'f1': f1, 'kappa': kappa}
]
# Later, to recover cm:
# cm = np.array(json.loads(text))
df = pandas.DataFrame(perfdata)
filename = os.path.join(path,label+".perf.csv")
df.to_csv(filename, index=False, encoding='utf-8')
<|endoftext|>


<commit_before>""" Testing decorators module
"""

import numpy as np
from nose.tools import (assert_true, assert_raises, assert_equal)
from skimage._shared.testing import doctest_skip_parser

def test_skipper():
    def f():
        pass

    class c():

        def __init__(self):
            self.me = "I think, therefore..."

    docstring = \
        """ Header

        >>> something # skip if not HAVE_AMODULE
        >>> something + else
        >>> a = 1 # skip if not HAVE_BMODULE
        >>> something2   # skip if HAVE_AMODULE
        """
    f.__doc__ = docstring
    c.__doc__ = docstring

    global HAVE_AMODULE, HAVE_BMODULE
    HAVE_AMODULE = False
    HAVE_BMODULE = True

    f2 = doctest_skip_parser(f)
    c2 = doctest_skip_parser(c)
    assert_true(f is f2)
    assert_true(c is c2)

    assert_equal(f2.__doc__,
                 """ Header

                 >>> something # doctest: +SKIP
                 >>> something + else
                 >>> a = 1
                 >>> something2
                 """)
    assert_equal(c2.__doc__,
                 """ Header

                 >>> something # doctest: +SKIP
                 >>> something + else
                 >>> a = 1
                 >>> something2
                 """)

    HAVE_AMODULE = True
    HAVE_BMODULE = False
    f.__doc__ = docstring
    c.__doc__ = docstring
    f2 = doctest_skip_parser(f)
    c2 = doctest_skip_parser(c)

    assert_true(f is f2)
    assert_equal(f2.__doc__,
                 """ Header

                 >>> something
                 >>> something + else
                 >>> a = 1 # doctest: +SKIP
                 >>> something2   # doctest: +SKIP
                 """)
    assert_equal(c2.__doc__,
                 """ Header

                 >>> something
                 >>> something + else
                 >>> a = 1 # doctest: +SKIP
                 >>> something2   # doctest: +SKIP
                 """)

    del HAVE_AMODULE
    f.__doc__ = docstring
    c.__doc__ = docstring
    assert_raises(NameError, doctest_skip_parser, f)
    assert_raises(NameError, doctest_skip_parser, c)

if __name__ == '__main__':
    np.testing.run_module_suite()
<commit_msg>Add unit test for test_parallel decorator<commit_after>""" Testing decorators module
"""

import numpy as np
from nose.tools import (assert_true, assert_raises, assert_equal)
from skimage._shared.testing import doctest_skip_parser, test_parallel

def test_skipper():
    def f():
        pass

    class c():

        def __init__(self):
            self.me = "I think, therefore..."

    docstring = \
        """ Header

        >>> something # skip if not HAVE_AMODULE
        >>> something + else
        >>> a = 1 # skip if not HAVE_BMODULE
        >>> something2   # skip if HAVE_AMODULE
        """
    f.__doc__ = docstring
    c.__doc__ = docstring

    global HAVE_AMODULE, HAVE_BMODULE
    HAVE_AMODULE = False
    HAVE_BMODULE = True

    f2 = doctest_skip_parser(f)
    c2 = doctest_skip_parser(c)
    assert_true(f is f2)
    assert_true(c is c2)

    assert_equal(f2.__doc__,
                 """ Header

                 >>> something # doctest: +SKIP
                 >>> something + else
                 >>> a = 1
                 >>> something2
                 """)
    assert_equal(c2.__doc__,
                 """ Header

                 >>> something # doctest: +SKIP
                 >>> something + else
                 >>> a = 1
                 >>> something2
                 """)

    HAVE_AMODULE = True
    HAVE_BMODULE = False
    f.__doc__ = docstring
    c.__doc__ = docstring
    f2 = doctest_skip_parser(f)
    c2 = doctest_skip_parser(c)

    assert_true(f is f2)
    assert_equal(f2.__doc__,
                 """ Header

                 >>> something
                 >>> something + else
                 >>> a = 1 # doctest: +SKIP
                 >>> something2   # doctest: +SKIP
                 """)
    assert_equal(c2.__doc__,
                 """ Header

                 >>> something
                 >>> something + else
                 >>> a = 1 # doctest: +SKIP
                 >>> something2   # doctest: +SKIP
                 """)

    del HAVE_AMODULE
    f.__doc__ = docstring
    c.__doc__ = docstring
    assert_raises(NameError, doctest_skip_parser, f)
    assert_raises(NameError, doctest_skip_parser, c)

def test_test_parallel():
    state = []

    @test_parallel()
    def change_state1():
        state.append(None)
    change_state1()
    assert len(state) == 2

    @test_parallel(num_threads=1)
    def change_state2():
        state.append(None)
    change_state2()
    assert len(state) == 3

    @test_parallel(num_threads=3)
    def change_state3():
        state.append(None)
    change_state3()
    assert len(state) == 6

if __name__ == '__main__':
    np.testing.run_module_suite()
<|endoftext|>


<commit_before># Copyright (c) 2014, Salesforce.com, Inc.  All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# - Redistributions of source code must retain the above copyright
#   notice, this list of conditions and the following disclaimer.
# - Redistributions in binary form must reproduce the above copyright
#   notice, this list of conditions and the following disclaimer in the
#   documentation and/or other materials provided with the distribution.
# - Neither the name of Salesforce.com nor the names of its contributors
#   may be used to endorse or promote products derived from this
#   software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
# TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

__version__ = '2.0.4'

import os

try:
    import distributions.has_cython
    has_cython = distributions.has_cython.has_cython()
except ImportError:
    has_cython = False

ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
<commit_msg>Bump to version 2.0.5<commit_after># Copyright (c) 2014, Salesforce.com, Inc.  All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# - Redistributions of source code must retain the above copyright
#   notice, this list of conditions and the following disclaimer.
# - Redistributions in binary form must reproduce the above copyright
#   notice, this list of conditions and the following disclaimer in the
#   documentation and/or other materials provided with the distribution.
# - Neither the name of Salesforce.com nor the names of its contributors
#   may be used to endorse or promote products derived from this
#   software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
# TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

__version__ = '2.0.5'

import os

try:
    import distributions.has_cython
    has_cython = distributions.has_cython.has_cython()
except ImportError:
    has_cython = False

ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
<|endoftext|>


<commit_before>import os
import urllib
import requests
import json
import slu_utils

class GoogleClient:
    timeout = 5
    url = ''
    headers = {"Content-Type": "audio/x-flac; rate=44100"}

    def __init__(self, language, key_file):
        keys = slu_utils.lines_to_list(key_file)
        q = {"output": "json", "lang": language, "key": keys[0]}
        self.url = "https://www.google.com/speech-api/v2/recognize?%s" % (urllib.urlencode(q))

    def recognize_file(self, file_path):
        try:
            print "[" + self.__class__.__name__ + "] [GOOGLE] Recognizing.."
            transcriptions = []
            data = open(file_path, "rb").read()
            response = requests.post(self.url, headers=self.headers, data=data, timeout=self.timeout)
            json_units = response.text.split(os.linesep)
            for unit in json_units:
                if not unit:
                    continue
                obj = json.loads(unit)
                alternatives = obj["result"]
                if len(alternatives) > 0:
                    for obj in alternatives:
                        results = obj["alternative"]
                        for result in results:
                            transcriptions.append(result["transcript"])
            return transcriptions
        except ValueError as ve:
            print "[" + self.__class__.__name__ + "] [RECOGNIZE]ERROR! Google APIs are temporary unavailable. Returning empty list.."
            return []
        except requests.exceptions.RequestException as e:
            print "[" + self.__class__.__name__ + "] [RECOGNIZE]ERROR! Unable to reach Google. Returning empty list.."
            return []

    def recognize_data(self, data):
        try:
            print "[" + self.__class__.__name__ + "] [GOOGLE] Recognizing.."
            transcriptions = []
            response = requests.post(self.url, headers=self.headers, data=data, timeout=self.timeout)
            json_units = response.text.split(os.linesep)
            for unit in json_units:
                if not unit:
                    continue
                obj = json.loads(unit)
                alternatives = obj["result"]
                if len(alternatives) > 0:
                    for obj in alternatives:
                        results = obj["alternative"]
                        for result in results:
                            transcriptions.append(result["transcript"])
            return transcriptions
        except ValueError as ve:
            print "[" + self.__class__.__name__ + "] [RECOGNIZE]ERROR! Google APIs are temporary unavailable. Returning empty list.."
            return []
        except requests.exceptions.RequestException as e:
            print "[" + self.__class__.__name__ + "] [RECOGNIZE]ERROR! Unable to reach Google. Returning empty list.."
            return []

def main():
    g = GoogleClient("en-US", "resources/google_keys.txt")
    while True:
        print g.recognize_file('resources/recording.flac')

if __name__ == "__main__":
    main()
<commit_msg>google client connection timeout increased to 10 (it was 5)<commit_after>import os
import urllib
import requests
import json
import slu_utils

class GoogleClient:
    timeout = 10
    url = ''
    headers = {"Content-Type": "audio/x-flac; rate=44100"}

    def __init__(self, language, key_file):
        keys = slu_utils.lines_to_list(key_file)
        q = {"output": "json", "lang": language, "key": keys[0]}
        self.url = "https://www.google.com/speech-api/v2/recognize?%s" % (urllib.urlencode(q))

    def recognize_file(self, file_path):
        try:
            print "[" + self.__class__.__name__ + "] [GOOGLE] Recognizing.."
            transcriptions = []
            data = open(file_path, "rb").read()
            response = requests.post(self.url, headers=self.headers, data=data, timeout=self.timeout)
            json_units = response.text.split(os.linesep)
            for unit in json_units:
                if not unit:
                    continue
                obj = json.loads(unit)
                alternatives = obj["result"]
                if len(alternatives) > 0:
                    for obj in alternatives:
                        results = obj["alternative"]
                        for result in results:
                            transcriptions.append(result["transcript"])
            return transcriptions
        except ValueError as ve:
            print "[" + self.__class__.__name__ + "] [RECOGNIZE]ERROR! Google APIs are temporary unavailable. Returning empty list.."
            return []
        except requests.exceptions.RequestException as e:
            print "[" + self.__class__.__name__ + "] [RECOGNIZE]ERROR! Unable to reach Google. Returning empty list.."
            return []

    def recognize_data(self, data):
        try:
            print "[" + self.__class__.__name__ + "] [GOOGLE] Recognizing.."
            transcriptions = []
            response = requests.post(self.url, headers=self.headers, data=data, timeout=self.timeout)
            json_units = response.text.split(os.linesep)
            for unit in json_units:
                if not unit:
                    continue
                obj = json.loads(unit)
                alternatives = obj["result"]
                if len(alternatives) > 0:
                    for obj in alternatives:
                        results = obj["alternative"]
                        for result in results:
                            transcriptions.append(result["transcript"])
            return transcriptions
        except ValueError as ve:
            print "[" + self.__class__.__name__ + "] [RECOGNIZE]ERROR! Google APIs are temporary unavailable. Returning empty list.."
            return []
        except requests.exceptions.RequestException as e:
            print "[" + self.__class__.__name__ + "] [RECOGNIZE]ERROR! Unable to reach Google. Returning empty list.."
            return []

def main():
    g = GoogleClient("en-US", "resources/google_keys.txt")
    while True:
        print g.recognize_file('resources/recording.flac')

if __name__ == "__main__":
    main()
<|endoftext|>


<commit_before>def patch():
    """
    Disable creation of foreign key constraints, unsupported from Google Cloud SQL
    """
    from django.db.models import get_apps, get_models
    from django.db.models.fields.related import RelatedField

    for app in get_apps():
        for model in get_models(app):
            for field in model._meta.get_fields(include_parents=False):
                if isinstance(field, RelatedField):
                    field.db_constraint = False
<commit_msg>compatibility with django 1.7<commit_after>def patch():
    """
    Disable creation of foreign key constraints, unsupported from Google Cloud SQL
    """
    from django.db.models import get_apps, get_models
    from django.db.models.fields.related import RelatedField

    for app in get_apps():
        for model in get_models(app):
            fields = model._meta.get_fields(include_parents=False)\
                if hasattr(model._meta, 'get_fields') else model._meta.fields
            for field in fields:
                if isinstance(field, RelatedField):
                    field.db_constraint = False
<|endoftext|>


<commit_before>'''
Created on Jun 18, 2012

@author: Sjoerd
'''
import pdb
from django.contrib import admin
from django import forms
from django.db import models
from django.contrib.auth.models import Group,Permission
from django.contrib.contenttypes.models import ContentType
from django.contrib.admin.options import InlineModelAdmin
from django.forms import TextInput, Textarea
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect

from guardian.admin import GuardedModelAdmin
from guardian.shortcuts import get_objects_for_user,assign

from comicmodels.models import ComicSite,Page

class PageAdminForm(forms.ModelForm):
    move = forms.CharField(widget=forms.Select)
    move.required = False
    move.widget.choices=(
                         (models.BLANK_CHOICE_DASH[0]),
                         ('FIRST', 'First'),
                         ('UP', 'Up'),
                         ('DOWN', 'Down'),
                         ('LAST', 'Last'),
                        )

    class Meta:
        model = Page

class PageAdmin(GuardedModelAdmin):
    """Define the admin interface for pages"""

    form = PageAdminForm

    # Make sure regular template overrides work. GuardedModelAdmin disables this
    # With change_form_template = None templates in templates/admin/comicsite/page
    # will be heeded again.
    #change_form_template = None

    #Show these page params in admin overview list
    list_display = ('title','ComicSite','order')

    list_filter = ['ComicSite']

    def save_model(self, request, obj, form, change):

        if obj.id is None:
            #at page creation, set the correct object permissions
            # get admin group for the comicsite of this page
            agn = obj.ComicSite.admin_group_name()
            admingroup = Group.objects.get(name=agn)

            # add change_page permission to the current page
            obj.save()
            assign("change_page",admingroup,obj)

            # FIXME: is this double save really needed?

        obj.save()
        move = form.cleaned_data['move']
        obj.move(move)

    def queryset(self, request):
        """ overwrite this method to return only pages comicsites to which current user has access """
        user_qs = get_objects_for_user(request.user, 'comicsite.change_page')
        return user_qs

    def response_change(self, request, obj, post_url_continue=None):
        """This makes the response after adding go to another apps changelist for some model"""
        return HttpResponseRedirect(reverse("admin:comicmodels_comicsite_change",args=[obj.ComicSite.pk]))

    def response_add(self, request, obj, post_url_continue=None):
        return self.response_change(request, obj, post_url_continue=None)

class LinkedInline(InlineModelAdmin):
    """ Show some info and link to complete model admin
    Created to show all pages belonging to a site on site admin without having to edit pages
    there in a cramped interface
    """
    template = 'admin/edit_inline/linked.html'

    admin_model_path = None
    can_delete = False

    formfield_overrides = {
        models.CharField: {'widget': TextInput(attrs={'size':'40'})},
        models.TextField: {'widget': Textarea(attrs={'rows':1, 'cols':20})},
    }

    def __init__(self, *args):
        super(LinkedInline, self).__init__(*args)
        if self.admin_model_path is None:
            self.admin_model_path = self.model.__name__.lower()

class PageInline(LinkedInline):
    model = Page
    extra = 0

    fields = ('title','html_trunc','hidden','order')
    # make sure page is only displayed, not edited
    #readonly_fields=("title","html")
    readonly_fields=('title','html_trunc','hidden','order')

    def html_trunc(self,obj):
        return obj.html[:300]

class ComicSiteAdminForm(forms.ModelForm):
    description = forms.CharField(widget=forms.Textarea(attrs={'rows':2, 'cols':80}),help_text = "Short summary of this project, max 1024 characters.")

    class Meta:
        model = ComicSite

class ComicSiteAdmin(GuardedModelAdmin):

    # Make sure regular template overrides work. GuardedModelAdmin disables this
    # With change_form_template = None templates in templates/admin/comicsite/page
    # will be heeded again.
    change_form_template = None

    form = ComicSiteAdminForm
    inlines = [PageInline]

    def queryset(self, request):
        """ overwrite this method to return only comicsites to which current user has access """
        qs = super(admin.ModelAdmin, self).queryset(request)

        if request.user.is_superuser:
            return qs

        user_qs = get_objects_for_user(request.user, 'comicsite.change_comicsite')
        return user_qs

    def save_model(self, request, obj, form, change):
        """ when saving for the first time, set object permissions; give all permissions to creator """

        if obj.id is None:
            # create admins group
            admingroup = Group.objects.create(name=obj.admin_group_name())

            # create participants group
            participantsgroup = Group.objects.create(name=obj.short_name+"_participants")
            participantsgroup.save()

            # add regular django class-level permissions so this group can use admin interface
            can_add = Permission.objects.get(codename="add_comicsite")
            can_change = Permission.objects.get(codename="change_comicsite")
            can_delete = Permission.objects.get(codename="delete_comicsite")

            can_add_page = Permission.objects.get(codename="add_page")
            can_change_page = Permission.objects.get(codename="change_page")
            can_delete_page = Permission.objects.get(codename="delete_page")

            admingroup.permissions.add(can_add,can_change,can_delete,can_add_page,
                                       can_change_page,can_delete_page)

            # add object-level permission to the specific ComicSite so it shows up in admin
            #admingroup.save()
            obj.save()
            #pdb.set_trace()
            assign("change_comicsite",admingroup,obj)

            # add current user to admins for this site
            request.user.groups.add(admingroup)
        else:
            #if object already existed just save
            obj.save()

class PageAdminForm():
    move = forms.CharField(widget=forms.Select)
    move.required = False
    move.widget.choices=(
                         (models.BLANK_CHOICE_DASH[0]),
                         ('FIRST', 'First'),
                         ('UP', 'Up'),
                         ('DOWN', 'Down'),
                         ('LAST', 'Last'),
                        )

admin.site.register(ComicSite,ComicSiteAdmin)
admin.site.register(Page,PageAdmin)

<commit_msg>one more path I forgot to change over<commit_after>'''
Created on Jun 18, 2012

@author: Sjoerd
'''
import pdb
from django.contrib import admin
from django import forms
from django.db import models
from django.contrib.auth.models import Group,Permission
from django.contrib.contenttypes.models import ContentType
from django.contrib.admin.options import InlineModelAdmin
from django.forms import TextInput, Textarea
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect

from guardian.admin import GuardedModelAdmin
from guardian.shortcuts import get_objects_for_user,assign

from comicmodels.models import ComicSite,Page

class PageAdminForm(forms.ModelForm):
    move = forms.CharField(widget=forms.Select)
    move.required = False
    move.widget.choices=(
                         (models.BLANK_CHOICE_DASH[0]),
                         ('FIRST', 'First'),
                         ('UP', 'Up'),
                         ('DOWN', 'Down'),
                         ('LAST', 'Last'),
                        )

    class Meta:
        model = Page

class PageAdmin(GuardedModelAdmin):
    """Define the admin interface for pages"""

    form = PageAdminForm

    # Make sure regular template overrides work. GuardedModelAdmin disables this
    # With change_form_template = None templates in templates/admin/comicsite/page
    # will be heeded again.
    #change_form_template = None

    #Show these page params in admin overview list
    list_display = ('title','ComicSite','order')

    list_filter = ['ComicSite']

    def save_model(self, request, obj, form, change):

        if obj.id is None:
            #at page creation, set the correct object permissions
            # get admin group for the comicsite of this page
            agn = obj.ComicSite.admin_group_name()
            admingroup = Group.objects.get(name=agn)

            # add change_page permission to the current page
            obj.save()
            assign("change_page",admingroup,obj)

            # FIXME: is this double save really needed?

        obj.save()
        move = form.cleaned_data['move']
        obj.move(move)

    def queryset(self, request):
        """ overwrite this method to return only pages comicsites to which current user has access """
        user_qs = get_objects_for_user(request.user, 'comicmodels.change_page')
        return user_qs

    def response_change(self, request, obj, post_url_continue=None):
        """This makes the response after adding go to another apps changelist for some model"""
        return HttpResponseRedirect(reverse("admin:comicmodels_comicsite_change",args=[obj.ComicSite.pk]))

    def response_add(self, request, obj, post_url_continue=None):
        return self.response_change(request, obj, post_url_continue=None)

class LinkedInline(InlineModelAdmin):
    """ Show some info and link to complete model admin
    Created to show all pages belonging to a site on site admin without having to edit pages
    there in a cramped interface
    """
    template = 'admin/edit_inline/linked.html'

    admin_model_path = None
    can_delete = False

    formfield_overrides = {
        models.CharField: {'widget': TextInput(attrs={'size':'40'})},
        models.TextField: {'widget': Textarea(attrs={'rows':1, 'cols':20})},
    }

    def __init__(self, *args):
        super(LinkedInline, self).__init__(*args)
        if self.admin_model_path is None:
            self.admin_model_path = self.model.__name__.lower()

class PageInline(LinkedInline):
    model = Page
    extra = 0

    fields = ('title','html_trunc','hidden','order')
    # make sure page is only displayed, not edited
    #readonly_fields=("title","html")
    readonly_fields=('title','html_trunc','hidden','order')

    def html_trunc(self,obj):
        return obj.html[:300]

class ComicSiteAdminForm(forms.ModelForm):
    description = forms.CharField(widget=forms.Textarea(attrs={'rows':2, 'cols':80}),help_text = "Short summary of this project, max 1024 characters.")

    class Meta:
        model = ComicSite

class ComicSiteAdmin(GuardedModelAdmin):

    # Make sure regular template overrides work. GuardedModelAdmin disables this
    # With change_form_template = None templates in templates/admin/comicsite/page
    # will be heeded again.
    change_form_template = None

    form = ComicSiteAdminForm
    inlines = [PageInline]

    def queryset(self, request):
        """ overwrite this method to return only comicsites to which current user has access """
        qs = super(admin.ModelAdmin, self).queryset(request)

        if request.user.is_superuser:
            return qs

        user_qs = get_objects_for_user(request.user, 'comicmodels.change_comicsite')
        return user_qs

    def save_model(self, request, obj, form, change):
        """ when saving for the first time, set object permissions; give all permissions to creator """

        if obj.id is None:
            # create admins group
            admingroup = Group.objects.create(name=obj.admin_group_name())

            # create participants group
            participantsgroup = Group.objects.create(name=obj.short_name+"_participants")
            participantsgroup.save()

            # add regular django class-level permissions so this group can use admin interface
            can_add = Permission.objects.get(codename="add_comicsite")
            can_change = Permission.objects.get(codename="change_comicsite")
            can_delete = Permission.objects.get(codename="delete_comicsite")

            can_add_page = Permission.objects.get(codename="add_page")
            can_change_page = Permission.objects.get(codename="change_page")
            can_delete_page = Permission.objects.get(codename="delete_page")

            admingroup.permissions.add(can_add,can_change,can_delete,can_add_page,
                                       can_change_page,can_delete_page)

            # add object-level permission to the specific ComicSite so it shows up in admin
            #admingroup.save()
            obj.save()
            #pdb.set_trace()
            assign("change_comicsite",admingroup,obj)

            # add current user to admins for this site
            request.user.groups.add(admingroup)
        else:
            #if object already existed just save
            obj.save()

class PageAdminForm():
    move = forms.CharField(widget=forms.Select)
    move.required = False
    move.widget.choices=(
                         (models.BLANK_CHOICE_DASH[0]),
                         ('FIRST', 'First'),
                         ('UP', 'Up'),
                         ('DOWN', 'Down'),
                         ('LAST', 'Last'),
                        )

admin.site.register(ComicSite,ComicSiteAdmin)
admin.site.register(Page,PageAdmin)

<|endoftext|>


<commit_before>"""
Helper functions for creating Form classes from Django models
and database field objects.
"""

from django.utils.translation import gettext
from util import ValidationError
from forms import BaseForm, DeclarativeFieldsMetaclass, SortedDictFromList
from fields import Field, ChoiceField
from widgets import Select, SelectMultiple, MultipleHiddenInput

__all__ = ('save_instance', 'form_for_model', 'form_for_instance', 'form_for_fields',
           'ModelChoiceField', 'ModelMultipleChoiceField')

def model_save(self, commit=True):
    """
    Creates and returns model instance according to self.clean_data.

    This method is created for any form_for_model Form.
    """
    if self.errors:
        raise ValueError("The %s could not be created because the data didn't validate." % self._model._meta.object_name)
    return save_instance(self, self._model(), commit)

def save_instance(form, instance, commit=True):
    """
    Saves bound Form ``form``'s clean_data into model instance ``instance``.

    Assumes ``form`` has a field for every non-AutoField database field in
    ``instance``. If commit=True, then the changes to ``instance`` will be
    saved to the database. Returns ``instance``.
    """
    from django.db import models
    opts = instance.__class__._meta
    if form.errors:
        raise ValueError("The %s could not be changed because the data didn't validate." % opts.object_name)
    clean_data = form.clean_data
    for f in opts.fields:
        if not f.editable or isinstance(f, models.AutoField):
            continue
        setattr(instance, f.name, clean_data[f.name])
    if commit:
        instance.save()
        for f in opts.many_to_many:
            setattr(instance, f.attname, clean_data[f.name])
    # GOTCHA: If many-to-many data is given and commit=False, the many-to-many
    # data will be lost. This happens because a many-to-many options cannot be
    # set on an object until after it's saved. Maybe we should raise an
    # exception in that case.
    return instance

def make_instance_save(instance):
    "Returns the save() method for a form_for_instance Form."
    def save(self, commit=True):
        return save_instance(self, instance, commit)
    return save

def form_for_model(model, form=BaseForm, formfield_callback=lambda f: f.formfield()):
    """
    Returns a Form class for the given Django model class.

    Provide ``form`` if you want to use a custom BaseForm subclass.

    Provide ``formfield_callback`` if you want to define different logic for
    determining the formfield for a given database field. It's a callable that
    takes a database Field instance and returns a form Field instance.
    """
    opts = model._meta
    field_list = []
    for f in opts.fields + opts.many_to_many:
        if not f.editable:
            continue
        formfield = formfield_callback(f)
        if formfield:
            field_list.append((f.name, formfield))
    fields = SortedDictFromList(field_list)
    return type(opts.object_name + 'Form', (form,), {'base_fields': fields, '_model': model, 'save': model_save})

def form_for_instance(instance, form=BaseForm, formfield_callback=lambda f, **kwargs: f.formfield(**kwargs)):
    """
    Returns a Form class for the given Django model instance.

    Provide ``form`` if you want to use a custom BaseForm subclass.

    Provide ``formfield_callback`` if you want to define different logic for
    determining the formfield for a given database field. It's a callable that
    takes a database Field instance, plus **kwargs, and returns a form Field
    instance with the given kwargs (i.e. 'initial').
    """
    model = instance.__class__
    opts = model._meta
    field_list = []
    for f in opts.fields + opts.many_to_many:
        if not f.editable:
            continue
        current_value = f.value_from_object(instance)
        formfield = formfield_callback(f, initial=current_value)
        if formfield:
            field_list.append((f.name, formfield))
    fields = SortedDictFromList(field_list)
    return type(opts.object_name + 'InstanceForm', (form,),
        {'base_fields': fields, '_model': model, 'save': make_instance_save(instance)})

def form_for_fields(field_list):
    "Returns a Form class for the given list of Django database field instances."
    fields = SortedDictFromList([(f.name, f.formfield()) for f in field_list if f.editable])
    return type('FormForFields', (BaseForm,), {'base_fields': fields})

class QuerySetIterator(object):
    def __init__(self, queryset, empty_label, cache_choices):
        self.queryset, self.empty_label, self.cache_choices = queryset, empty_label, cache_choices

    def __iter__(self):
        if self.empty_label is not None:
            yield (u"", self.empty_label)
        for obj in self.queryset:
            yield (obj._get_pk_val(), str(obj))
        # Clear the QuerySet cache if required.
        if not self.cache_choices:
            self.queryset._result_cache = None

class ModelChoiceField(ChoiceField):
    "A ChoiceField whose choices are a model QuerySet."
    # This class is a subclass of ChoiceField for purity, but it doesn't
    # actually use any of ChoiceField's implementation.
    def __init__(self, queryset, empty_label=u"---------", cache_choices=False,
            required=True, widget=Select, label=None, initial=None, help_text=None):
        self.queryset = queryset
        self.empty_label = empty_label
        self.cache_choices = cache_choices
        # Call Field instead of ChoiceField __init__() because we don't need
        # ChoiceField.__init__().
        Field.__init__(self, required, widget, label, initial, help_text)
        self.widget.choices = self.choices

    def _get_choices(self):
        # If self._choices is set, then somebody must have manually set
        # the property self.choices. In this case, just return self._choices.
        if hasattr(self, '_choices'):
            return self._choices
        # Otherwise, execute the QuerySet in self.queryset to determine the
        # choices dynamically.
        return QuerySetIterator(self.queryset, self.empty_label, self.cache_choices)

    def _set_choices(self, value):
        # This method is copied from ChoiceField._set_choices(). It's necessary
        # because property() doesn't allow a subclass to overwrite only
        # _get_choices without implementing _set_choices.
        self._choices = self.widget.choices = list(value)

    choices = property(_get_choices, _set_choices)

    def clean(self, value):
        Field.clean(self, value)
        if value in ('', None):
            return None
        try:
            value = self.queryset.model._default_manager.get(pk=value)
        except self.queryset.model.DoesNotExist:
            raise ValidationError(gettext(u'Select a valid choice. That choice is not one of the available choices.'))
        return value

class ModelMultipleChoiceField(ModelChoiceField):
    "A MultipleChoiceField whose choices are a model QuerySet."
    hidden_widget = MultipleHiddenInput
    def __init__(self, queryset, cache_choices=False, required=True,
            widget=SelectMultiple, label=None, initial=None, help_text=None):
        super(ModelMultipleChoiceField, self).__init__(queryset, None, cache_choices,
            required, widget, label, initial, help_text)

    def clean(self, value):
        if self.required and not value:
            raise ValidationError(gettext(u'This field is required.'))
        elif not self.required and not value:
            return []
        if not isinstance(value, (list, tuple)):
            raise ValidationError(gettext(u'Enter a list of values.'))
        final_values = []
        for val in value:
            try:
                obj = self.queryset.model._default_manager.get(pk=val)
            except self.queryset.model.DoesNotExist:
                raise ValidationError(gettext(u'Select a valid choice. %s is not one of the available choices.') % val)
            else:
                final_values.append(obj)
        return final_values
<commit_msg>Added a comment to ModelChoiceField from [4552]<commit_after>"""
Helper functions for creating Form classes from Django models
and database field objects.
"""

from django.utils.translation import gettext
from util import ValidationError
from forms import BaseForm, DeclarativeFieldsMetaclass, SortedDictFromList
from fields import Field, ChoiceField
from widgets import Select, SelectMultiple, MultipleHiddenInput

__all__ = ('save_instance', 'form_for_model', 'form_for_instance', 'form_for_fields',
           'ModelChoiceField', 'ModelMultipleChoiceField')

def model_save(self, commit=True):
    """
    Creates and returns model instance according to self.clean_data.

    This method is created for any form_for_model Form.
    """
    if self.errors:
        raise ValueError("The %s could not be created because the data didn't validate." % self._model._meta.object_name)
    return save_instance(self, self._model(), commit)

def save_instance(form, instance, commit=True):
    """
    Saves bound Form ``form``'s clean_data into model instance ``instance``.

    Assumes ``form`` has a field for every non-AutoField database field in
    ``instance``. If commit=True, then the changes to ``instance`` will be
    saved to the database. Returns ``instance``.
    """
    from django.db import models
    opts = instance.__class__._meta
    if form.errors:
        raise ValueError("The %s could not be changed because the data didn't validate." % opts.object_name)
    clean_data = form.clean_data
    for f in opts.fields:
        if not f.editable or isinstance(f, models.AutoField):
            continue
        setattr(instance, f.name, clean_data[f.name])
    if commit:
        instance.save()
        for f in opts.many_to_many:
            setattr(instance, f.attname, clean_data[f.name])
    # GOTCHA: If many-to-many data is given and commit=False, the many-to-many
    # data will be lost. This happens because a many-to-many options cannot be
    # set on an object until after it's saved. Maybe we should raise an
    # exception in that case.
    return instance

def make_instance_save(instance):
    "Returns the save() method for a form_for_instance Form."
    def save(self, commit=True):
        return save_instance(self, instance, commit)
    return save

def form_for_model(model, form=BaseForm, formfield_callback=lambda f: f.formfield()):
    """
    Returns a Form class for the given Django model class.

    Provide ``form`` if you want to use a custom BaseForm subclass.

    Provide ``formfield_callback`` if you want to define different logic for
    determining the formfield for a given database field. It's a callable that
    takes a database Field instance and returns a form Field instance.
    """
    opts = model._meta
    field_list = []
    for f in opts.fields + opts.many_to_many:
        if not f.editable:
            continue
        formfield = formfield_callback(f)
        if formfield:
            field_list.append((f.name, formfield))
    fields = SortedDictFromList(field_list)
    return type(opts.object_name + 'Form', (form,), {'base_fields': fields, '_model': model, 'save': model_save})

def form_for_instance(instance, form=BaseForm, formfield_callback=lambda f, **kwargs: f.formfield(**kwargs)):
    """
    Returns a Form class for the given Django model instance.

    Provide ``form`` if you want to use a custom BaseForm subclass.

    Provide ``formfield_callback`` if you want to define different logic for
    determining the formfield for a given database field. It's a callable that
    takes a database Field instance, plus **kwargs, and returns a form Field
    instance with the given kwargs (i.e. 'initial').
    """
    model = instance.__class__
    opts = model._meta
    field_list = []
    for f in opts.fields + opts.many_to_many:
        if not f.editable:
            continue
        current_value = f.value_from_object(instance)
        formfield = formfield_callback(f, initial=current_value)
        if formfield:
            field_list.append((f.name, formfield))
    fields = SortedDictFromList(field_list)
    return type(opts.object_name + 'InstanceForm', (form,),
        {'base_fields': fields, '_model': model, 'save': make_instance_save(instance)})

def form_for_fields(field_list):
    "Returns a Form class for the given list of Django database field instances."
    fields = SortedDictFromList([(f.name, f.formfield()) for f in field_list if f.editable])
    return type('FormForFields', (BaseForm,), {'base_fields': fields})

class QuerySetIterator(object):
    def __init__(self, queryset, empty_label, cache_choices):
        self.queryset, self.empty_label, self.cache_choices = queryset, empty_label, cache_choices

    def __iter__(self):
        if self.empty_label is not None:
            yield (u"", self.empty_label)
        for obj in self.queryset:
            yield (obj._get_pk_val(), str(obj))
        # Clear the QuerySet cache if required.
        if not self.cache_choices:
            self.queryset._result_cache = None

class ModelChoiceField(ChoiceField):
    "A ChoiceField whose choices are a model QuerySet."
    # This class is a subclass of ChoiceField for purity, but it doesn't
    # actually use any of ChoiceField's implementation.
    def __init__(self, queryset, empty_label=u"---------", cache_choices=False,
            required=True, widget=Select, label=None, initial=None, help_text=None):
        self.queryset = queryset
        self.empty_label = empty_label
        self.cache_choices = cache_choices
        # Call Field instead of ChoiceField __init__() because we don't need
        # ChoiceField.__init__().
        Field.__init__(self, required, widget, label, initial, help_text)
        self.widget.choices = self.choices

    def _get_choices(self):
        # If self._choices is set, then somebody must have manually set
        # the property self.choices. In this case, just return self._choices.
        if hasattr(self, '_choices'):
            return self._choices
        # Otherwise, execute the QuerySet in self.queryset to determine the
        # choices dynamically. Return a fresh QuerySetIterator that has not
        # been consumed. Note that we're instantiating a new QuerySetIterator
        # *each* time _get_choices() is called (and, thus, each time
        # self.choices is accessed) so that we can ensure the QuerySet has not
        # been consumed.
        return QuerySetIterator(self.queryset, self.empty_label, self.cache_choices)

    def _set_choices(self, value):
        # This method is copied from ChoiceField._set_choices(). It's necessary
        # because property() doesn't allow a subclass to overwrite only
        # _get_choices without implementing _set_choices.
        self._choices = self.widget.choices = list(value)

    choices = property(_get_choices, _set_choices)

    def clean(self, value):
        Field.clean(self, value)
        if value in ('', None):
            return None
        try:
            value = self.queryset.model._default_manager.get(pk=value)
        except self.queryset.model.DoesNotExist:
            raise ValidationError(gettext(u'Select a valid choice. That choice is not one of the available choices.'))
        return value

class ModelMultipleChoiceField(ModelChoiceField):
    "A MultipleChoiceField whose choices are a model QuerySet."
    hidden_widget = MultipleHiddenInput
    def __init__(self, queryset, cache_choices=False, required=True,
            widget=SelectMultiple, label=None, initial=None, help_text=None):
        super(ModelMultipleChoiceField, self).__init__(queryset, None, cache_choices,
            required, widget, label, initial, help_text)

    def clean(self, value):
        if self.required and not value:
            raise ValidationError(gettext(u'This field is required.'))
        elif not self.required and not value:
            return []
        if not isinstance(value, (list, tuple)):
            raise ValidationError(gettext(u'Enter a list of values.'))
        final_values = []
        for val in value:
            try:
                obj = self.queryset.model._default_manager.get(pk=val)
            except self.queryset.model.DoesNotExist:
                raise ValidationError(gettext(u'Select a valid choice. %s is not one of the available choices.') % val)
            else:
                final_values.append(obj)
        return final_values
<|endoftext|>


<commit_before>"""
This file is part of Arakoon, a distributed key-value store. Copyright
(C) 2010 Incubaid BVBA

Licensees holding a valid Incubaid license may use this file in
accordance with Incubaid's Arakoon commercial license agreement. For
more information on how to enter into this agreement, please contact
Incubaid (contact details can be found on www.arakoon.org/licensing).

Alternatively, this file may be redistributed and/or modified under
the terms of the GNU Affero General Public License version 3, as
published by the Free Software Foundation. Under this license, this
file is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.

See the GNU Affero General Public License for more details.
You should have received a copy of the
GNU Affero General Public License along with this program (file "COPYING").
If not, see <http://www.gnu.org/licenses/>.
"""

"""
Arakoon Cluster Setup
"""

from pylabs.InitBase import q
import sys
import optparse

parser = optparse.OptionParser()
parser.add_option("-s", "--stop", dest="stop", action="store_true", default=False)
parser.add_option("-p", "--port", dest="port", type="int", default=15500)
parser.add_option("-c", "--cluster", dest="cluster", default="phpclient")
(options, args) = parser.parse_args()

if options.stop:
    clu = q.manage.arakoon.getCluster(options.cluster)
    if "%s_0"%(options.cluster) in clu.getStatus():
        clu.tearDown()
        print "Cluster tear down successfully!"
    else:
        print "Cluster is not running!"
else:
    clu = q.manage.arakoon.getCluster(options.cluster)
    if "%s_0"%(options.cluster) not in clu.getStatus():
        clu.setUp(1, options.port)
        clu.start()
        print "Arakoon Server started!"
    else:
        print "Cluster Already running!"

<commit_msg>Arakoon PHP client: Fixed import to work on qbase3 in setup.py<commit_after>"""
This file is part of Arakoon, a distributed key-value store. Copyright
(C) 2010 Incubaid BVBA

Licensees holding a valid Incubaid license may use this file in
accordance with Incubaid's Arakoon commercial license agreement. For
more information on how to enter into this agreement, please contact
Incubaid (contact details can be found on www.arakoon.org/licensing).

Alternatively, this file may be redistributed and/or modified under
the terms of the GNU Affero General Public License version 3, as
published by the Free Software Foundation. Under this license, this
file is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.

See the GNU Affero General Public License for more details.
You should have received a copy of the
GNU Affero General Public License along with this program (file "COPYING").
If not, see <http://www.gnu.org/licenses/>.
"""

"""
Arakoon Cluster Setup
"""

try:
    from pylabs.InitBase import q
except ImportError:
    from pymonkey.InitBase import q

import sys
import optparse

parser = optparse.OptionParser()
parser.add_option("-s", "--stop", dest="stop", action="store_true", default=False)
parser.add_option("-p", "--port", dest="port", type="int", default=15500)
parser.add_option("-c", "--cluster", dest="cluster", default="phpclient")
(options, args) = parser.parse_args()

if options.stop:
    clu = q.manage.arakoon.getCluster(options.cluster)
    if "%s_0"%(options.cluster) in clu.getStatus():
        clu.tearDown()
        print "Cluster tear down successfully!"
    else:
        print "Cluster is not running!"
else:
    clu = q.manage.arakoon.getCluster(options.cluster)
    if "%s_0"%(options.cluster) not in clu.getStatus():
        clu.setUp(1, options.port)
        clu.start()
        print "Arakoon Server started!"
    else:
        print "Cluster Already running!"

<|endoftext|>


<commit_before>#!/usr/bin/env python3
# -*- coding:utf-8 -*-

'''
Middleware test specs
'''

import os
import sys

from unittest import TestCase
from django.http import HttpResponseRedirect

from ..tests import TESTS_DIR
from .request_mock import HTTPRequest
from ..middleware import AjaxRedirectionMiddleware

class TestAjaxMode(TestCase):
    '''
    Ajax Mode test case
    '''
    request = None

    def setUp(self):
        '''
        Setup function
        '''
        sys.path.append(TESTS_DIR)
        os.environ["DJANGO_SETTINGS_MODULE"] = "setting_mock"
        self.request = HTTPRequest(True, "/test")

    def tearDown(self):
        '''
        Tear Down function
        '''
        sys.path.remove(TESTS_DIR)
        del os.environ["DJANGO_SETTINGS_MODULE"]

    def test_not_redirection(self):
        '''
        The response should be None
        '''
        response = AjaxRedirectionMiddleware().process_request(self.request)
        self.assertIsNone(response, "Response should be None")

class TestNonAjaxMode(TestCase):
    '''
    Non-Ajax Mode test case
    '''
    request = None
    path = "/test"

    def setUp(self):
        '''
        Setup function
        '''
        self.request = HTTPRequest(False, self.path)
        sys.path.append(TESTS_DIR)
        os.environ["DJANGO_SETTINGS_MODULE"] = "setting_mock"

    def tearDown(self):
        '''
        Tear Down function
        '''
        sys.path.remove(TESTS_DIR)
        del os.environ["DJANGO_SETTINGS_MODULE"]

    def test_redirection_raw(self):
        '''
        The response should be HttpResponseRedirect prefixed with /#
        '''
        from django.conf import settings
        setattr(settings, "AJAX_REDIRECTION_PREFIX", None)
        response = AjaxRedirectionMiddleware().process_request(self.request)
        self.assertIsInstance(
            response,
            HttpResponseRedirect,
            "Response should be an instance of HttpResponseRedirect"
        )
        self.assertEqual(
            response.url,
            "/#" + self.path,
            "The redirect destination should be prefixed with /#/"
        )

    def test_redirection_raw_with_child(self):
        '''
        The response should be HttpResponseRedirect prefixed with /#
        '''
        from django.conf import settings
        setattr(settings, "AJAX_REDIRECTION_PREFIX", None)
        self.request = HTTPRequest(False, self.path + "/test2")
        response = AjaxRedirectionMiddleware().process_request(self.request)
        self.assertIsInstance(
            response,
            HttpResponseRedirect,
            "Response should be an instance of HttpResponseRedirect"
        )
        self.assertEqual(
            response.url,
            "/#" + self.path + "/test2",
            "The redirect destination should be prefixed with /#/"
        )

    def test_redirection_prefix(self):
        '''
        The response should be HttpResponseRedirect
        prefixed with /test_prefix/#
        '''
        from django.conf import settings
        setattr(settings, "AJAX_REDIRECTION_PREFIX", "/test_prefix")
        response = AjaxRedirectionMiddleware().process_request(self.request)
        self.assertIsInstance(
            response,
            HttpResponseRedirect,
            "The response should be HttpResponseRedirect"
        )
        self.assertEqual(
            response.url,
            "/test_prefix/#" + self.path,
            "The path should be prefixed with /test_prefix/#"
        )

    def test_redirection_prefix_with_child(self):
        '''
        The response should be HttpResponseRedirect
        prefixed with /test_prefix/#
        '''
        from django.conf import settings
        setattr(settings, "AJAX_REDIRECTION_PREFIX", "/test_prefix")
        self.request = HTTPRequest(False, self.path + "/test2")
        response = AjaxRedirectionMiddleware().process_request(self.request)
        self.assertIsInstance(
            response,
            HttpResponseRedirect,
            "Response should be an instance of HttpResponseRedirect"
        )
        self.assertEqual(
            response.url,
            "/test_prefix/#" + self.path + "/test2",
            "The redirect destination should be prefixed with /#/"
        )

    def test_root_not_redirect(self):
        '''
        The response shouldn't redirect when accessing root directory.
        '''
        from django.conf import settings
        setattr(settings, "AJAX_REDIRECTION_PREFIX", None)
        self.request = HTTPRequest(False, "/")
        response = AjaxRedirectionMiddleware().process_request(self.request)
        self.assertIsNone(response, "Response should be None")

    def test_prefix_not_redirect(self):
        '''
        The response shouldn't redirect when accessing root directory.
        '''
        from django.conf import settings
        setattr(settings, "AJAX_REDIRECTION_PREFIX", "/test")
        response = AjaxRedirectionMiddleware().process_request(self.request)
        self.assertIsNone(response, "Response should be None")
<commit_msg>Fixed pylint warn<commit_after>#!/usr/bin/env python3
# -*- coding:utf-8 -*-

'''
Middleware test specs
'''

import os
import sys

from unittest import TestCase
from django.http import HttpResponseRedirect

from ..tests import TESTS_DIR
from .request_mock import HTTPRequest
from ..middleware import AjaxRedirectionMiddleware

class TestAjaxMode(TestCase):
    '''
    Ajax Mode test case
    '''
    request = None

    def setUp(self):
        '''
        Setup function
        '''
        sys.path.append(TESTS_DIR)
        os.environ["DJANGO_SETTINGS_MODULE"] = "setting_mock"
        self.request = HTTPRequest(True, "/test")

    def tearDown(self):
        '''
        Tear Down function
        '''
        sys.path.remove(TESTS_DIR)
        del os.environ["DJANGO_SETTINGS_MODULE"]

    def test_not_redirection(self):
        '''
        The response should be None
        '''
        response = AjaxRedirectionMiddleware().process_request(self.request)
        self.assertIsNone(response, "Response should be None")

class TestNonAjaxMode(TestCase):
    '''
    Non-Ajax Mode test case
    '''
    request = None
    path = "/test"

    def setUp(self):
        '''
        Setup function
        '''
        self.request = HTTPRequest(False, self.path)
        sys.path.append(TESTS_DIR)
        os.environ["DJANGO_SETTINGS_MODULE"] = "setting_mock"

    def tearDown(self):
        '''
        Tear Down function
        '''
        sys.path.remove(TESTS_DIR)
        del os.environ["DJANGO_SETTINGS_MODULE"]

    def test_redirection_raw(self):
        '''
        The response should be HttpResponseRedirect prefixed with /#
        '''
        from django.conf import settings
        setattr(settings, "AJAX_REDIRECTION_PREFIX", None)
        response = AjaxRedirectionMiddleware().process_request(self.request)
        self.assertIsInstance(
            response,
            HttpResponseRedirect,
            "Response should be an instance of HttpResponseRedirect"
        )
        self.assertEqual(
            response.url,
            "/#" + self.path,
            "The redirect destination should be prefixed with /#/"
        )

    def test_child_redirection_raw(self):
        '''
        The response should be HttpResponseRedirect prefixed with /#
        '''
        from django.conf import settings
        setattr(settings, "AJAX_REDIRECTION_PREFIX", None)
        self.request = HTTPRequest(False, self.path + "/test2")
        response = AjaxRedirectionMiddleware().process_request(self.request)
        self.assertIsInstance(
            response,
            HttpResponseRedirect,
            "Response should be an instance of HttpResponseRedirect"
        )
        self.assertEqual(
            response.url,
            "/#" + self.path + "/test2",
            "The redirect destination should be prefixed with /#/"
        )

    def test_redirection_prefix(self):
        '''
        The response should be HttpResponseRedirect
        prefixed with /test_prefix/#
        '''
        from django.conf import settings
        setattr(settings, "AJAX_REDIRECTION_PREFIX", "/test_prefix")
        response = AjaxRedirectionMiddleware().process_request(self.request)
        self.assertIsInstance(
            response,
            HttpResponseRedirect,
            "The response should be HttpResponseRedirect"
        )
        self.assertEqual(
            response.url,
            "/test_prefix/#" + self.path,
            "The path should be prefixed with /test_prefix/#"
        )

    def test_child_redirection_prefix(self):
        '''
        The response should be HttpResponseRedirect
        prefixed with /test_prefix/#
        '''
        from django.conf import settings
        setattr(settings, "AJAX_REDIRECTION_PREFIX", "/test_prefix")
        self.request = HTTPRequest(False, self.path + "/test2")
        response = AjaxRedirectionMiddleware().process_request(self.request)
        self.assertIsInstance(
            response,
            HttpResponseRedirect,
            "Response should be an instance of HttpResponseRedirect"
        )
        self.assertEqual(
            response.url,
            "/test_prefix/#" + self.path + "/test2",
            "The redirect destination should be prefixed with /#/"
        )

    def test_root_not_redirect(self):
        '''
        The response shouldn't redirect when accessing root directory.
        '''
        from django.conf import settings
        setattr(settings, "AJAX_REDIRECTION_PREFIX", None)
        self.request = HTTPRequest(False, "/")
        response = AjaxRedirectionMiddleware().process_request(self.request)
        self.assertIsNone(response, "Response should be None")

    def test_prefix_not_redirect(self):
        '''
        The response shouldn't redirect when accessing root directory.
        '''
        from django.conf import settings
        setattr(settings, "AJAX_REDIRECTION_PREFIX", "/test")
        response = AjaxRedirectionMiddleware().process_request(self.request)
        self.assertIsNone(response, "Response should be None")
<|endoftext|>


<commit_before># coding=utf-8

from django.contrib import admin
from djangovirtualpos.models import VirtualPointOfSale, VPOSCeca, VPOSRedsys, VPOSSantanderElavon, VPOSPaypal

admin.site.register(VirtualPointOfSale)
admin.site.register(VPOSCeca)
admin.site.register(VPOSRedsys)
admin.site.register(VPOSPaypal)
admin.site.register(VPOSSantanderElavon)

<commit_msg>Register VPOSRefundOperation in admin zone<commit_after># coding=utf-8

from django.contrib import admin
from djangovirtualpos.models import VirtualPointOfSale, VPOSRefundOperation, VPOSCeca, VPOSRedsys, VPOSSantanderElavon, VPOSPaypal

admin.site.register(VirtualPointOfSale)
admin.site.register(VPOSRefundOperation)
admin.site.register(VPOSCeca)
admin.site.register(VPOSRedsys)
admin.site.register(VPOSPaypal)
admin.site.register(VPOSSantanderElavon)

<|endoftext|>