Commit ba041227 authored by Calcul Bot's avatar Calcul Bot
Browse files

refactoring ...

parent 6d1309cd
......@@ -16,13 +16,19 @@ class JobOffer(object):
title,
description,
time=datetime.datetime.now(),
attachment=None):
attachment=None,
attachment_ext=None):
self.title = title
self.description = description
self.date = time
self.attachment = attachment
if self.attachment_ext is None and self.attachment is not None:
_, self.attachment_ext = os.path.splitext(self.attachment)
# File, salary, etc...
def __hash__(self):
return hash((self.title, self.description, self.date))
......@@ -30,8 +36,7 @@ class JobOffer(object):
return self.attachment is not None
def get_attachment_extension(self):
filename, file_extension = os.path.splitext(self.attachment)
return file_extension
return self.attachment_ext
def get_attachment_content(self):
with open(self.attachment, 'rb') as fh:
......@@ -58,9 +63,9 @@ def job_offer_to_markdown(job_offer, attachment=None, slug=None):
return (
"Title: {}\n".format(job_offer.title) +
"Date: {}\n".format(job_offer.date.strftime('%Y-%m-%d %H:%M')) +
slug_md +
slug_md +
"\n" +
"{}\n".format(job_offer.description) +
"{}\n".format(job_offer.description) +
attachment_md
)
......@@ -71,6 +76,8 @@ def create_job_request(gl, job_offer):
TODO: generates job id in this function.
"""
# TODO: html and pelican special chars
# Generating job id
job_id = gen_job_id(job_offer)
base_name = 'job_{}'.format(job_id)
......@@ -84,9 +91,9 @@ def create_job_request(gl, job_offer):
# Accessing project
project = gl.projects.get(gitlab_source_id)
# Creating new branch
branch = project.branches.create({"branch" : branch_name, "ref" : 'master'})
branch = project.branches.create({"branch" : branch_name, "ref" : 'master'})
# Creating commit
data = {
......@@ -114,7 +121,7 @@ def create_job_request(gl, job_offer):
# Committing
commit = project.commits.create(data)
# Creating merge request
project.mergerequests.create({
'source_branch': branch_name,
......@@ -138,6 +145,7 @@ if __name__ == '__main__':
parser.add_argument('title', help='Offer title')
parser.add_argument('description', help='Offer description')
parser.add_argument('--attachment', default=None, help='File attachment')
parser.add_argument('--attachment_ext', default=None, help='Overwrite file attachment extension')
args = parser.parse_args()
# Connecting to Gitlab
......@@ -146,7 +154,7 @@ if __name__ == '__main__':
# Checking connexion ?
print("Creating job-offer request...")
job_offer = JobOffer(args.title, args.description, attachment=args.attachment)
job_offer = JobOffer(args.title, args.description, attachment=args.attachment, attachment_ext=args.attachment_ext)
create_job_request(gl, job_offer)
print("Done.")
......
......@@ -12,22 +12,52 @@
$title = $contact_name = $email = $website = $description = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Extract and cleaning parameters
// TODO: trim
$contact_name = $_POST["contact_name"];
$email = $_POST["email"];
$website = $_POST["website"];
$title = $_POST["title"];
$title = $_POST["title"];
$description = $_POST["description"];
echo "tmp_name = " . $_FILES['file']['tmp_name'] . "<br>";
// Checking parameters validity
// TODO
// TODO: checking attachment mime type ?
$is_form_valid = !empty($contact_name);
if (!empty($contact_name)) {
system('python3 job_offers/add_job_offer.py'
. ' ' . escapeshellarg(htmlspecialchars($title))
. ' ' . escapeshellarg(htmlspecialchars($description))
. (! empty($_FILES['file']['tmp_name']) ? ( ' --attachment ' . $_FILES['file']['tmp_name'] ) : '' )
);
if ($is_form_valid) {
// Base command-line
$cmd_line = 'python3 job_offers/add_job_offer.py'
. ' ' . escapeshellarg($title)
. ' ' . escapeshellarg($description);
$form_success = true;
// Attachment
if (!empty($_FILES['file']['tmp_name']) {
$attachment_ext = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
if (is_null(attachment_ext)) {
$attachment_ext = '';
} else {
$attachment_ext = '.' . $attachment_ext;
}
$cmd_line .= ' --attachment ' . escapeshellarg($_FILES['file']['tmp_name']);
$cmd_line .= ' --attachment_ext ' . escapeshellarg($attachment_ext);
// TODO: quid des caractères spéciaux Pelican dans le nom de fichier ?! Dans le script python ?
// TODO: tout échapper mardown & html dans python !
}
// Launching python script
exec($cmd_line, $output, $return_var);
// DEBUG
echo '<p>Command line: ' . htmlspecialchars($cmd_line) . '</p><br>';
echo '<p>Output: ' . htmlspecialchars(join('<br>', $output)) . '</p><br>';
echo '<p>Return code: ' . htmlspecialchars($return_var) . '</p><br>';
// Checking return code or/and output
// TODO: message in case of error (like "retry later or contact")
$form_success = $return_var == 0;
}
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment