Skip to content

Commit

Permalink
Recherche séance + tag interventions résultat scrutins
Browse files Browse the repository at this point in the history
  • Loading branch information
njoyard committed Jun 29, 2018
1 parent 5705826 commit 09d6b2e
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 7 deletions.
3 changes: 2 additions & 1 deletion batch/scrutin/parse_scrutins.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ def parse_scrutins(legislature, data):
for item in data["scrutins"]["scrutin"]:
scrutin = parse_scrutin(item, seances, groupes)

basename = "scrutin_%s_%s" % (legislature, scrutin["numero"])
numero = ("00000%s" % scrutin["numero"])[-5:]
basename = "scrutin_%s_%s" % (legislature, numero)
hash_file = os.path.join(SCRUTINS_DIR, "%s.sha1" % basename)
json_file = os.path.join(SCRUTINS_DIR, "%s.json" % basename)

Expand Down
50 changes: 44 additions & 6 deletions lib/model/doctrine/Scrutin.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,57 @@ public function setNumero($numero) {
return $this->_set('numero', $numero);
}

public function setSeance($idseance) {
$seance = Doctrine::getTable('Seance')->findOneByTODO($idseance);
public function setSeance($id_jo) {
$seance = Doctrine::getTable('Seance')->findOneByIDJO($id_jo);
if (!$seance) {
throw new Exception("Aucune séance trouvée avec l'id $idseance");
throw new Exception("Aucune séance trouvée avec l'id JO $id_jo");
}
$seance_id = $seance->id;
$seance->free();

$ret = $this->_set('seance_id', $seance->id)
$ret = $this->_set('seance_id', $seance_id)
&& $this->_set('date', $seance->date)
&& $this->_set('numero_semaine', $seance->numero_semaine)
&& $this->_set('annee', $seance->annee);

$seance->free();
return $ret;
}

public function tagInterventions() {
// Comptage des scrutins avec numéro inférieur dans la même séance
$avant = $this->createQuery('s')
->select('count(1) as cnt')
->where('s.seance_id = ?', $this->seance_id)
->andWhere('s.numero < ?', $this->numero)
->fetchOne()['cnt'];

// Recherche de l'intervention avec un tableau de votants qui correspond
$inter = Doctrine::getTable('Intervention')
->createQuery('i')
->where('i.seance_id = ?', $this->seance_id)
->andWhere("i.intervention LIKE '%table class=\"scrutin\"%'")
->orderBy('i.timestamp')
->offset($avant)
->getFirst();

$found = FALSE;
if ($inter) {
$found = TRUE;

// Vérification du nombre de pour/contre
$text = $inter->intervention;
if (preg_match('/pour[^<]*<\/td><td>(\d+)/', $text, &$match) == 0
|| intval($match[0]) != $this->nombre_pours
|| preg_match('/contre[^<]*<\/td><td>(\d+)/', $text, &$match) == 0
|| intval($match[0]) != $this->nombre_contres) {
$found = FALSE;
} else {
$inter->addTag("scrutin:numero={$this->numero}");
}
}

if (!$found) {
throw new Exception("Scrutin {$this->numero} non trouvé dans les interventions de la séance $seance_id");
}
}

public function setDemandeur($demandeur) {
Expand Down
13 changes: 13 additions & 0 deletions lib/model/doctrine/SeanceTable.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,19 @@ public function findOneOrCreateIt($type, $date, $heure, $session) {
return $s;
}

public function findOneByIDJO($id_jo) {
$seance = Doctrine::getTable('Intervention')->createQuery('i')
->select('i.seance_id')
->where('i.source LIKE ?', "%/$id_jo.asp%")
->fetchOne();

if ($seance) {
return $this->find($seance['seance_id'])
}

return NULL;
}

public function getPager($request, $query = NULL)
{
$pager = new sfDoctrinePager('Seance',30);
Expand Down
22 changes: 22 additions & 0 deletions lib/task/loadScrutinsTask.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ protected function execute($arguments = array(), $options = array())
$dir = dirname(__FILE__).'/../../batch/scrutin/scrutin/';
$backupdir = dirname(__FILE__).'/../../batch/scrutin/loaded/';
$manager = new sfDatabaseManager($this->configuration);
$seances_manquantes = 0;

if (!is_dir($backupdir)) {
mkdir($backupdir, 0777, TRUE);
}

if (is_dir($dir)) {
foreach (scandir($dir) as $file) {
Expand All @@ -41,6 +46,12 @@ protected function execute($arguments = array(), $options = array())

try {
$scrutin->setSeance($data->seance);
} catch (Exception $e) {
$seances_manquantes++;
continue;
}

try {
$scrutin->setDemandeur($data->demandeur);
$scrutin->setTitre($data->titre);
$scrutin->setType($data->type);
Expand All @@ -56,11 +67,22 @@ protected function execute($arguments = array(), $options = array())
}

$scrutin->save();

try {
$scrutin->tagInterventions();
} catch(Exception $e) {
echo "ERREUR $file (tag interventions) : {$e->getMessage()}\n";
}

$scrutin->setVotes($data->parlementaires);
$scrutin->free();

rename($dir . $file, $backupdir . $file);
}

if ($seances_manquantes > 0) {
echo "WARNING: $seances_manquantes séances non trouvées\n";
}
}
}
}

0 comments on commit 09d6b2e

Please sign in to comment.