<s> from datetime import datetime <EOL> from django . core . exceptions import PermissionDenied <EOL> from django . utils . translation import ugettext as _ <EOL> from django . conf import settings <EOL> from django . db . models . signals import post_save , pre_delete <EOL> from django . contrib . sites . models import Site <EOL> from django . contrib import messages <EOL> from notification import models as notification <EOL> from transifex . resources . models import Resource <EOL> from transifex . resources . utils import invalidate_template_cache <EOL> from transifex . teams . models import Team <EOL> from transifex . txcommon . log import logger <EOL> from txcron . signals import cron_daily , cron_hourly <EOL> from transifex . projects . signals import pre_submit_translation , post_submit_translation <EOL> from lotte . signals import lotte_init , lotte_done <EOL> from models import Lock , LockError <EOL> def pre_handler ( sender , resource = None , language = None , user = None , <EOL> instance = None , ** kwargs ) : <EOL> if not resource or not language or not user : <EOL> return <EOL> lock = Lock . objects . get_valid ( resource , language ) <EOL> if not lock : <EOL> return <EOL> if lock . owner != user : <EOL> raise PermissionDenied <EOL> def post_handler ( sender , request = None , resource = None , language = None , <EOL> user = None , instance = None , ** kwargs ) : <EOL> if '<STR_LIT>' in request . POST and request . POST [ '<STR_LIT>' ] : <EOL> if user : <EOL> Lock . objects . create_update ( resource , language , user ) . expires <EOL> def lotte_init_handler ( sender , request , resources = None , language = None , <EOL> ** kwargs ) : <EOL> user = request . user <EOL> logger . debug ( "<STR_LIT>" ) <EOL> for resource in resources : <EOL> try : <EOL> lock = Lock . objects . create_update ( resource , language , user ) <EOL> logger . debug ( "<STR_LIT>" % lock ) <EOL> except LockError , e : <EOL> logger . debug ( "<STR_LIT>" % e . message ) <EOL> messages . error ( request , <EOL> _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) ) <EOL> def lotte_done_handler ( sender , request , resources = None , language = None , <EOL> ** kwargs ) : <EOL> user = request . user <EOL> logger . debug ( "<STR_LIT>" ) <EOL> for resource in resources : <EOL> lock = Lock . objects . get_valid ( resource , language ) <EOL> if lock : <EOL> try : <EOL> lock . delete_by_user ( user ) <EOL> logger . debug ( "<STR_LIT>" % lock ) <EOL> except LockError , e : <EOL> logger . debug ( "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> ( user , e . message ) ) <EOL> def expiration_notification ( sender , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> logger . debug ( "<STR_LIT>" ) <EOL> if not settings . ENABLE_NOTICES : <EOL> logger . debug ( "<STR_LIT>" ) <EOL> return <EOL> current_site = Site . objects . get_current ( ) <EOL> locks = Lock . objects . expiring ( ) <EOL> nt = '<STR_LIT>' <EOL> for lock in locks : <EOL> context = { '<STR_LIT>' : lock . rlstats . resource , <EOL> '<STR_LIT>' : lock . rlstats . language , <EOL> '<STR_LIT>' : lock . rlstats . resource . project , <EOL> '<STR_LIT:user>' : lock . owner , <EOL> '<STR_LIT>' : lock . expires , <EOL> '<STR_LIT>' : current_site } <EOL> logger . debug ( "<STR_LIT>" % lock ) <EOL> notification . send_now ( [ lock . owner , ] , nt , context ) <EOL> lock . notified = True <EOL> lock . save ( ) <EOL> def db_cleanup ( sender , ** kwargs ) : <EOL> logger . debug ( "<STR_LIT>" ) <EOL> locks = Lock . objects . expired ( ) <EOL> for lock in locks : <EOL> logger . debug ( "<STR_LIT>" % lock ) <EOL> lock . delete ( ) <EOL> def invalidate_cache ( sender , instance , created = True , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if created : <EOL> logger . debug ( "<STR_LIT>" % instance ) <EOL> invalidate_template_cache ( '<STR_LIT>' , <EOL> instance . rlstats . resource . project . slug , <EOL> instance . rlstats . resource . slug , <EOL> instance . rlstats . language . code ) <EOL> invalidate_template_cache ( '<STR_LIT>' , <EOL> instance . rlstats . resource . project . slug , <EOL> instance . rlstats . resource . slug ) <EOL> team = Team . objects . get_or_none ( instance . rlstats . resource . project , <EOL> instance . rlstats . language . code ) <EOL> if team : <EOL> invalidate_template_cache ( '<STR_LIT>' , team . id , <EOL> instance . rlstats . resource . id ) <EOL> def connect ( ) : <EOL> pre_submit_translation . connect ( pre_handler , sender = Resource ) <EOL> post_submit_translation . connect ( post_handler , sender = Resource ) <EOL> lotte_init . connect ( lotte_init_handler ) <EOL> lotte_done . connect ( lotte_done_handler ) <EOL> cron_hourly . connect ( db_cleanup ) <EOL> cron_hourly . connect ( expiration_notification ) <EOL> post_save . connect ( invalidate_cache , sender = Lock ) <EOL> pre_delete . connect ( invalidate_cache , sender = Lock ) </s>
