Commit bbf36566 authored by Matthieu Boileau's avatar Matthieu Boileau
Browse files

Merge remote-tracking branch 'origin/WIP-job-form' into apache

parents 69205ebb cf95cae9
......@@ -21,5 +21,6 @@ apache:
- pelican
only:
- apache
- WIP-job-form
script:
- pelican -s publishconf.py -o /var/www/html
import datetime
import gitlab
import os
# General configuration
gitlab_url = "https://gitlab.math.unistra.fr"
gitlab_token = "KF9uNHkJfFT54s8bcJAq"
gitlab_target_id = 309
gitlab_target_branch = 'WIP-job-form'
gitlab_source_id = 310
class JobOffer(object):
""" Job offer class. """
def __init__(self,
title,
description,
time=datetime.datetime.now(),
attachment=None):
self.title = title
self.description = description
self.date = time
self.attachment = attachment
# File, salary, etc...
def __hash__(self):
return hash((self.title, self.description, self.date))
def to_markdown(self, attachment_stored_name=None):
attachment_md = "[attachment]({{filename}}{})".format(attachment_stored_name) if attachment_stored_name is not None and self.has_attachment() else ''
return (
"Title: {}\n".format(self.title) +
"Date: {}\n".format(self.date.strftime('%Y-%m-%d %H:%M')) +
"\n" +
"{}\n".format(self.description) +
attachment_md
)
def has_attachment(self):
return self.attachment is not None
def get_attachment_extension(self):
filename, file_extension = os.path.splitext(self.attachment)
return file_extension
def get_attachment_content(self):
with open(self.attachment, 'rb') as fh:
return fh.read()
def __str__(self):
return self.to_markdown()
def create_job_request(gl, job_offer):
""" Creates a merge request for the given job offer.
TODO: generates job id in this function.
"""
# Generating job id
job_id = gen_job_id(job_offer)
branch_name = 'job_{}'.format(job_id)
job_name = 'job_{}.md'.format(job_id)
if job_offer.has_attachment():
attachment_name = 'job_{}_attachment{}'.format(job_id, job_offer.get_attachment_extension())
else:
attachment_name = None
# Accessing project
project = gl.projects.get(gitlab_source_id)
# Creating new branch
branch = project.branches.create({"branch" : branch_name, "ref" : 'master'})
# Creating commit
data = {
"branch" : branch_name,
"commit_message" : "Adding new job offer {}".format(branch_name),
"actions" : [
{
"action" : "create",
"file_path" : "content/job_offers/{}".format(job_name),
"content" : job_offer.to_markdown(attachment_name)
}
]
}
# Adding attachment
if job_offer.has_attachment():
import base64
data['actions'].append({
'action': 'create',
'file_path': 'content/job_attachments/{}'.format(attachment_name),
'encoding': 'base64',
'content': base64.b64encode(job_offer.get_attachment_content()).decode()
})
# Committing
commit = project.commits.create(data)
# Creating merge request
project.mergerequests.create({
'source_branch': branch_name,
'target_branch': gitlab_target_branch,
'target_project_id': gitlab_target_id,
'title': 'New job offer {}'.format(branch_name),
'description': str(job_offer),
'remove_source_branch': True
})
def gen_job_id(job_offer):
""" Generates an unique id for the given job offer. """
return hex(abs(hash(job_offer)))
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('title', help='Offer title')
parser.add_argument('description', help='Offer description')
parser.add_argument('--attachment', default=None, help='File attachment')
args = parser.parse_args()
# Connecting to Gitlab
gl = gitlab.Gitlab(gitlab_url, private_token=gitlab_token, api_version=4)
# Checking connexion ?
print("Creating job-offer request...")
job_offer = JobOffer(args.title, args.description, attachment=args.attachment)
create_job_request(gl, job_offer)
print("Done.")
Title: test
Date: 2018-01-10 18:15
qsdllpazrpoiglkdsjgs--attachment
Title: Super boulot
Date: 2018-01-10 11:27
bien payé !
Title: Test
Date: 2018-01-11 09:23
looking for a PHP developer
[attachment]({filename}job_0x9d153f715366f76_attachment)
\ No newline at end of file
:Title: Offre d'emploi
:Save_as: job_offer_form.php
:template: job_offer_form
:status: hidden
Formulaire pour l'ajout d'une offre d'emploi.
......@@ -8,6 +8,8 @@ SITEURL = ''
PATH = 'content'
STATIC_PATHS = ['job_offers']
TIMEZONE = 'Europe/Paris'
DEFAULT_LANG = 'fr'
......@@ -43,6 +45,7 @@ PLUGINS = ['ipynb.markup', 'pelican_dynamic',]
MENUITEMS = [('formations', '/category/formations.html'),
('ateliers', '/category/ateliers.html'),
('journees', '/category/journees.html')
('journees', '/category/journees.html'),
("offre d'emploi", '/job_offer_form.php'),
]
......@@ -10,7 +10,8 @@ import sys
sys.path.append(os.curdir)
from pelicanconf import *
SITEURL = 'https://groupe-calcul.pages.math.unistra.fr'
#SITEURL = 'https://groupe-calcul.pages.math.unistra.fr'
SITEURL = 'https://calcul.math.unistra.fr'
RELATIVE_URLS = False
FEED_ALL_ATOM = 'feeds/all.atom.xml'
......
{% extends "index.html" %}
{% block title %}Formulaire de dépôt d'une offre d'emploi{% endblock %}
{% block ogtitle %}Formulaire de dépôt d'une offre d'emploi{% endblock %}
{% block ogurl %}{{ SITEURL }}/contact.html{%endblock%}
{% block canonicalurl %}{{ SITEURL }}/contact.html{%endblock%}
{% block content %}
<?php
$form_success = false;
$title = $contact_name = $email = $website = $description = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$contact_name = $_POST["contact_name"];
$email = $_POST["email"];
$website = $_POST["website"];
$title = $_POST["title"];
$description = $_POST["description"];
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'] ) : '' )
);
$form_success = true;
}
}
if (!$form_success)
{
?>
<!-- Formulaire -->
<p>
<span class="error">* required field.</span>
</p>
<form enctype="multipart/form-data" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Titre: <input type="text" name="title" value="<?php echo $title;?>"><br><br>
Nom du contact: <input type="text" name="contact_name" value="<?php echo $contact_name;?>"><br><br>
E-mail: <input type="email" name="email" value="<?php echo $email;?>"><br><br>
Site web: <input type="url" name="website" value="<?php echo $website;?>"><br><br>
Description: <textarea name="description" rows="5" cols="40"><?php echo $description;?></textarea><br><br>
Fichier: <input type="file" name="file">
<input type="submit" name="submit" value="Soumettre">
</form>
<?php
} else {
?>
<p>
Youpi !!!<br>
Nom du contact: <?php echo htmlspecialchars($contact_name); ?><br>
E-mail: <?php echo htmlspecialchars($email); ?><br>
Site Web: <?php echo htmlspecialchars($website); ?><br>
Description: <?php echo htmlspecialchars($description); ?><br>
</p>
<?php
}
?>
{% endblock %}
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