Cronjob to award the fas "lifecycle" badges.
This commit is contained in:
parent
dd4aa27279
commit
b563090860
4 changed files with 121 additions and 0 deletions
112
roles/badges-backend/files/cron/award-lifecycle-badges
Normal file
112
roles/badges-backend/files/cron/award-lifecycle-badges
Normal file
|
@ -0,0 +1,112 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
import __main__
|
||||||
|
# This is going to require sqlalchemy 0.8 sooner than later.
|
||||||
|
__main__.__requires__ = __requires__ = ["tahrir-api", "sqlalchemy>=0.7"];
|
||||||
|
import pkg_resources
|
||||||
|
pkg_resources.require(__requires__)
|
||||||
|
|
||||||
|
import datetime
|
||||||
|
import time
|
||||||
|
import urllib
|
||||||
|
import socket
|
||||||
|
|
||||||
|
from tahrir_api.dbapi import TahrirDatabase
|
||||||
|
import transaction
|
||||||
|
|
||||||
|
_fas_cache = {}
|
||||||
|
|
||||||
|
import logging
|
||||||
|
log = logging.getLogger()
|
||||||
|
logging.basicConfig()
|
||||||
|
import fedora.client.fas2
|
||||||
|
|
||||||
|
import fedmsg
|
||||||
|
import fedmsg.config
|
||||||
|
|
||||||
|
fm_config = fedmsg.config.load_config()
|
||||||
|
fm_config['cert_prefix'] = 'fedbadges'
|
||||||
|
fm_config['name'] = 'relay_inbound'
|
||||||
|
fm_config['active'] = True
|
||||||
|
fedmsg.init(**fm_config)
|
||||||
|
|
||||||
|
import fedbadges.utils
|
||||||
|
|
||||||
|
|
||||||
|
def get_fas_userlist(fas_credentials, lookup, **config):
|
||||||
|
creds = fas_credentials
|
||||||
|
|
||||||
|
fasclient = fedora.client.fas2.AccountSystem(
|
||||||
|
username=creds['username'],
|
||||||
|
password=creds['password'],
|
||||||
|
)
|
||||||
|
|
||||||
|
timeout = socket.getdefaulttimeout()
|
||||||
|
socket.setdefaulttimeout(600)
|
||||||
|
try:
|
||||||
|
log.info("Downloading FAS cache")
|
||||||
|
request = fasclient.send_request('/user/list',
|
||||||
|
req_params={'search': '*'},
|
||||||
|
auth=True)
|
||||||
|
finally:
|
||||||
|
socket.setdefaulttimeout(timeout)
|
||||||
|
|
||||||
|
# We don't actually check for CLA+1, just "2 groups"
|
||||||
|
return [p for p in request['people'] if len(p.memberships) > 1]
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
year = datetime.timedelta(days=365.5)
|
||||||
|
mapping = {
|
||||||
|
'egg': year * 1,
|
||||||
|
'embryo': year * 2,
|
||||||
|
'tadpole': year * 3,
|
||||||
|
'tadpole-with-legs': year * 5,
|
||||||
|
'froglet': year * 7,
|
||||||
|
'adult frog': year * 10,
|
||||||
|
}
|
||||||
|
|
||||||
|
# First, some validation that the badge ids actually exist.
|
||||||
|
for badge_id, delta in mapping.items():
|
||||||
|
badge = tahrir.get_badge(badge_id=badge_id)
|
||||||
|
assert(badge.id)
|
||||||
|
|
||||||
|
# Then, do a long query against FAS for our candidates.
|
||||||
|
results = get_fas_userlist(fas_credentials=fm_config['fas_credentials'])
|
||||||
|
|
||||||
|
for badge_id, delta in mapping.items():
|
||||||
|
badge = tahrir.get_badge(badge_id=badge_id)
|
||||||
|
for person in results:
|
||||||
|
if now - person.creation > delta:
|
||||||
|
hit_em_up(badge, person)
|
||||||
|
|
||||||
|
|
||||||
|
def hit_em_up(badge, fas_user):
|
||||||
|
email = fas_user.username + "@fedoraproject.org"
|
||||||
|
user = tahrir.get_person(email)
|
||||||
|
|
||||||
|
if not user:
|
||||||
|
return
|
||||||
|
|
||||||
|
if tahrir.assertion_exists(badge.id, email):
|
||||||
|
print email, "already has", badge.id, "skipping."
|
||||||
|
return
|
||||||
|
|
||||||
|
time.sleep(1)
|
||||||
|
print "awarding", badge.id, "to", email
|
||||||
|
try:
|
||||||
|
transaction.begin()
|
||||||
|
tahrir.add_assertion(badge.id, email, None)
|
||||||
|
transaction.commit()
|
||||||
|
except Exception as e:
|
||||||
|
transaction.abort()
|
||||||
|
print "Failure:", e
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
uri = fm_config['badges_global']['database_uri']
|
||||||
|
tahrir = TahrirDatabase(
|
||||||
|
uri,
|
||||||
|
notification_callback=fedbadges.utils.notification_callback,
|
||||||
|
)
|
||||||
|
main()
|
|
@ -0,0 +1 @@
|
||||||
|
40 2 * * 3 fedmsg /usr/share/badges/cronjobs/award-lifecycle-badges >> /var/log/fedmsg/cron-award-lifecycle-badges.log 2>&1
|
|
@ -10,6 +10,12 @@
|
||||||
rotate 4
|
rotate 4
|
||||||
weekly
|
weekly
|
||||||
}
|
}
|
||||||
|
/var/log/fedmsg/cron-award-lifecycle-badges.log {
|
||||||
|
missingok
|
||||||
|
notifempty
|
||||||
|
rotate 4
|
||||||
|
weekly
|
||||||
|
}
|
||||||
/var/log/fedmsg/cron-award-mirror-badge.log {
|
/var/log/fedmsg/cron-award-mirror-badge.log {
|
||||||
missingok
|
missingok
|
||||||
notifempty
|
notifempty
|
||||||
|
|
|
@ -94,6 +94,7 @@
|
||||||
- award-libravatar-badge
|
- award-libravatar-badge
|
||||||
- award-flock-paparazzi-badge
|
- award-flock-paparazzi-badge
|
||||||
- award-mirror-badge
|
- award-mirror-badge
|
||||||
|
- award-lifecycle-badges
|
||||||
tags:
|
tags:
|
||||||
- config
|
- config
|
||||||
- cron
|
- cron
|
||||||
|
@ -110,6 +111,7 @@
|
||||||
- award-libravatar-badge
|
- award-libravatar-badge
|
||||||
- award-flock-paparazzi-badge
|
- award-flock-paparazzi-badge
|
||||||
- award-mirror-badge
|
- award-mirror-badge
|
||||||
|
- award-lifecycle-badges
|
||||||
tags:
|
tags:
|
||||||
- config
|
- config
|
||||||
- cron
|
- cron
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue