Add SAR script for elections

Signed-off-by: Pierre-Yves Chibon <pingou@pingoured.fr>
This commit is contained in:
Pierre-Yves Chibon 2018-05-17 15:57:24 +02:00
parent eb0b1276c0
commit c29ffd217f
3 changed files with 81 additions and 0 deletions

View file

@ -10,3 +10,8 @@ volgroup: /dev/vg_guests
eth0_ip: 10.5.126.107
vmhost: virthost19.phx2.fedoraproject.org
datacenter: phx2
# GDPR SAR variables - datanommer/datagrepper
sar_script: /usr/local/bin/elections_sar.py
sar_script_user: apache
sar_output_file: elections.json

View file

@ -74,3 +74,15 @@
tags:
- selinux
- elections
- name: Install the SAR script for elections
template: src={{ item.file }}
dest="{{ item.location }}/{{ item.file }}"
owner=apache group=apache mode=0700
with_items:
- { file: elections_sar.py, location: /usr/local/bin/elections_sar.py }
tags:
- config
- elections
- SAR
- GDPR

View file

@ -0,0 +1,64 @@
#!/usr/bin/python
from __future__ import unicode_literals, print_function
import json
import os
import sys
import sqlalchemy
if 'FEDORA_ELECTIONS_CONFIG' not in os.environ \
and os.path.exists('/etc/fedora-elections/fedora-elections.cfg'):
os.environ['FEDORA_ELECTIONS_CONFIG'] = '/etc/fedora-elections/'\
'fedora-elections.cfg'
from fedora_elections import SESSION
from fedora_elections import models
def get_candidate_users(session, username):
''' Return all fedora_elections.models.Candidate related to the username
provided
'''
query = SESSION.query(
models.Candidate
).filter(
sqlalchemy.or_(
models.Candidate.name.ilike('%{0}%'.format(username)),
models.Candidate.fas_name.ilike('%s%'.format(username))
)
)
return query.all()
def main():
''' Prints out all the election involving the username specified in the
SAR_USERNAME environment variable.
'''
username = os.getenv('SAR_USERNAME')
if not username:
print('An username is required to query datagrepper')
return 1
output = {}
output['candidates'] = []
for candidate in get_candidate_users(SESSION, username):
tmp = candidate.to_json()
tmp['election'] = candidate.election.to_json()
output['candidates'].append(tmp)
SESSION.remove()
print(json.dumps(
output, sort_keys=True, indent=4, separators=(',', ': ')
).encode('utf-8'))
if __name__ == '__main__':
sys.exit(main())