Example #1
0
    def test_timesince(self):
        # real testing is done in timesince.py, where we can provide our own 'now'
        # NOTE: \xa0 avoids wrapping between value and unit
        self.assertEqual(
            timesince_filter(datetime.datetime.now() - datetime.timedelta(1)),
            '1\xa0day')

        self.assertEqual(
            timesince_filter(datetime.datetime(2005, 12, 29),
                             datetime.datetime(2005, 12, 30)), '1\xa0day')
Example #2
0
    def test_timesince(self):
        # real testing is done in timesince.py, where we can provide our own 'now'
        # NOTE: \xa0 avoids wrapping between value and unit
        self.assertEqual(
            timesince_filter(datetime.datetime.now() - datetime.timedelta(1)),
            '1\xa0day')

        self.assertEqual(
            timesince_filter(datetime.datetime(2005, 12, 29),
                             datetime.datetime(2005, 12, 30)),
            '1\xa0day')
Example #3
0
def post_time(value, arg):
    """
    timesince_filter formats a date as the time since that date (i.e. "4 days, 6 hours").
    By default this formatting is done between the current time and the 'value'.

    This function converts string obtained by timesince_filter to:
    1. minutes when timesince <  1 hour
    2. if timesince > 1 hour only keeps the hours part
    3. if timesince > 24 hours returns the original date of posting, formatted
    by the 'arg' parameter (achieved using the date filter)

    Use Case:
    Use for mimicing post times used by Facebook, Youtube etc.
    """
    result = timesince_filter(value)

    for string in time_strings:
        if string in result:
            return date(value, arg)

    if 'hour' in result:
        result = result.split(",")[0]
        return result + ' ' + 'ago'

    else:
        return result + ' ' + 'ago'
Example #4
0
    def process_request(t_r):
        t_r.show_retry_button = False

        if t_r.celery_task_id is None:
            try:
                t_torrent = get_trans_torrent(t_r.what_torrent)
                t_torrent.sync_t_torrent()
                if t_torrent.torrent_done == 1:
                    t_r.status = 'download complete. transcoding should start within 1 minute.'
                else:
                    t_r.status = 'downloading ({0:.0%})'.format(
                        t_torrent.torrent_done)
                downloading.append(t_r)
            except TransTorrent.DoesNotExist:
                t_r.status = 'torrent has been removed'
                failed.append(t_r)
        elif t_r.date_completed is not None:
            t_r.status = 'completed {0} ago'.format(
                timesince_filter(t_r.date_completed))
            succeeded.append(t_r)
        else:
            async_result = AsyncResult(t_r.celery_task_id)
            if async_result.state == states.PENDING:
                t_r.status = 'pending transcoding'
                pending.append(t_r)
            elif async_result.state == states.STARTED:
                t_r.status = 'transcoding'
                transcoding.append(t_r)
            elif async_result.state == 'PROGRESS':
                t_r.status = 'transcoding: {0}'.format(
                    async_result.info['status_message'])
                transcoding.append(t_r)
            elif async_result.state == states.SUCCESS:
                t_r.status = 'completed'
                succeeded.append(t_r)
            elif async_result.state == states.FAILURE:
                t_r.show_retry_button = allow_retry
                t_r.status = 'failed - {0}({1})'.format(
                    type(async_result.result).__name__,
                    async_result.result.message)
                failed.append(t_r)
        what_client = get_what_client(request)
        t_r.status = t_r.status.replace(what_client.authkey,
                                        '<authkey>').replace(
                                            what_client.passkey, '<passkey>')
Example #5
0
    def process_request(t_r):
        t_r.show_retry_button = False

        if t_r.celery_task_id is None:
            try:
                t_torrent = get_trans_torrent(t_r.what_torrent)
                t_torrent.sync_t_torrent()
                if t_torrent.torrent_done == 1:
                    t_r.status = 'download complete. transcoding should start within 1 minute.'
                else:
                    t_r.status = 'downloading ({0:.0%})'.format(t_torrent.torrent_done)
                downloading.append(t_r)
            except TransTorrent.DoesNotExist:
                t_r.status = 'torrent has been removed'
                failed.append(t_r)
        elif t_r.date_completed is not None:
            t_r.status = 'completed {0} ago'.format(timesince_filter(t_r.date_completed))
            succeeded.append(t_r)
        else:
            async_result = AsyncResult(t_r.celery_task_id)
            if async_result.state == states.PENDING:
                t_r.status = 'pending transcoding'
                pending.append(t_r)
            elif async_result.state == states.STARTED:
                t_r.status = 'transcoding'
                transcoding.append(t_r)
            elif async_result.state == 'PROGRESS':
                t_r.status = 'transcoding: {0}'.format(async_result.info['status_message'])
                transcoding.append(t_r)
            elif async_result.state == states.SUCCESS:
                t_r.status = 'completed'
                succeeded.append(t_r)
            elif async_result.state == states.FAILURE:
                t_r.show_retry_button = allow_retry
                t_r.status = 'failed - {0}({1})'.format(type(async_result.result).__name__,
                                                        async_result.result.message)
                failed.append(t_r)
        what_client = get_what_client(request)
        t_r.status = t_r.status.replace(what_client.authkey, '<authkey>').replace(
            what_client.passkey, '<passkey>')
Example #6
0
 def test_explicit_date(self):
     self.assertEqual(
         timesince_filter(datetime(2005, 12, 29), datetime(2005, 12, 30)),
         '1\xa0day')
Example #7
0
 def test_since_now(self):
     self.assertEqual(timesince_filter(datetime.now() - timedelta(1)),
                      '1\xa0day')
Example #8
0
 def test_explicit_date(self):
     self.assertEqual(timesince_filter(datetime(2005, 12, 29), datetime(2005, 12, 30)), '1\xa0day')
Example #9
0
 def test_since_now(self):
     self.assertEqual(timesince_filter(datetime.now() - timedelta(1)), '1\xa0day')
Example #10
0
 def last_synchronized_formatted(self, obj):
     return _('{} ({} ago)'.format(date(obj.last_synchronized),
                                   timesince_filter(obj.last_synchronized)))
def controlAssistencia_clean( instance ):
    ( user, l4)  = instance.credentials if hasattr( instance, 'credentials') else (None,None,)
    
    if l4: return    
    
    isUpdate = instance.pk is not None    
    instance.instanceDB = None if not isUpdate else instance.__class__.objects.get( pk = instance.pk )
    
    errors = {}
    
    tutors = [ tutor for tutor in instance.alumne.tutorsDeLAlumne() ]
    if user: instance.professor = User2Professor( user )

    #
    # Només es poden modificar assistències 
    #
    nMaxDies = settings.CUSTOM_PERIODE_MODIFICACIO_ASSISTENCIA
    if isUpdate and instance.impartir.dia_impartir < ( dt.date.today() - dt.timedelta( days = nMaxDies) ):
        errors.setdefault(NON_FIELD_ERRORS, []).append( u'''Aquest controll d'assistència és massa antic per ser modificat (Té més de {0} dies)'''.format(nMaxDies) )
        
    #todo: altres controls:
    socTutor = hasattr(instance, 'professor') and instance.professor and instance.professor in tutors
    daqui_2_hores = dt.datetime.now() + dt.timedelta( hours = 2)
    daqui_15_dies = dt.datetime.now() + dt.timedelta( days = 15)
    if isUpdate and (not socTutor and instance.impartir.diaHora() > daqui_2_hores): 
        errors.setdefault(NON_FIELD_ERRORS, []).append( u'''Encara no es pot entrar aquesta assistència 
                                    (Falta {0} per poder-ho fer )'''.format(
                                      timesince_filter( dt.datetime.now(), instance.impartir.diaHora() - dt.timedelta( hours = 2) ) ) )
    if isUpdate and (socTutor and instance.impartir.diaHora() > daqui_15_dies): 
        errors.setdefault(NON_FIELD_ERRORS, []).append( u'''Encara no es pot entrar aquesta assistència 
                                    (Falta {0} per poder-ho fer )'''.format(
                                      timesince_filter( dt.datetime.now(), instance.impartir.diaHora() - dt.timedelta( days = 15) ) ) )

    #Una falta justificada pel tutor no pot ser matxacada per un professor
    socTutor = hasattr(instance, 'professor') and instance.professor and instance.professor in tutors    
    justificadaDB = instance.instanceDB and instance.instanceDB.estat and instance.instanceDB.estat.codi_estat.upper() == 'J'
    justificadaAra = instance.estat and instance.estat.codi_estat.upper() == 'J'
    posat_pel_tutor = instance.instanceDB and instance.instanceDB.professor and instance.instanceDB.professor in tutors
    
    if not socTutor and justificadaDB and posat_pel_tutor and not justificadaAra:
        errors.setdefault(NON_FIELD_ERRORS, []).append( u'''
                                  La falta d'en {0} no es pot modificar. El tutor Sr(a) {1} ha justificat la falta.  
                                                            '''.format(
                                       instance.alumne, instance.instanceDB.professor ) )
        
    #No es poden justificar faltes si s'ha enviat una carta.
    if not justificadaDB and justificadaAra:
        data_control_mes_3 = instance.impartir.dia_impartir + timedelta( days = 3 )
        dins_ambit_carta = ( CartaAbsentisme
                             .objects
                             .exclude( carta_esborrada_moment__isnull = False )
                             .filter( alumne = instance.alumne,
                                      data_carta__gte = data_control_mes_3
                                     )
                             .exists()
                            )
        if dins_ambit_carta:
            errors.setdefault(NON_FIELD_ERRORS, []).append( u'''
                                  La falta d'en {0} no es pot modificar. El tutor ha inclòs la falta en una Carta.  
                                                            '''.format(
                                       instance.alumne ) )
                
    #Només el tutor, el professor de guardia o el professor titular pot modificar un control d'assistència:
    if user:
        professors_habilitats = tutors
        if instance.professor: professors_habilitats.append( instance.professor.pk )
        if instance.impartir.horari.professor: professors_habilitats.append( instance.impartir.horari.professor.pk )
        if instance.impartir.professor_guardia: professors_habilitats.append( instance.impartir.professor_guardia.pk )
        if user.pk not in professors_habilitats:
            errors.setdefault(NON_FIELD_ERRORS, []).append( u'''Només el professor de l'assignatura, 
                                            el professor de guardia que ha passat llista o el tutor poden variar una assistència. 
                                                            ''' )         
    
    if len( errors ) > 0:
        raise ValidationError(errors)

    #Justificada: si el tutor l'havia justificat deixo al tutor com el que ha desat la falta:
    if justificadaDB and posat_pel_tutor:
        instance.professor = instance.instanceDB.professor
Example #12
0
def controlAssistencia_clean( instance ):
    ( user, l4)  = instance.credentials if hasattr( instance, 'credentials') else (None,None,)
    
    if l4: return    
    
    isUpdate = instance.pk is not None    
    instance.instanceDB = None if not isUpdate else instance.__class__.objects.get( pk = instance.pk )
    
    errors = {}
    
    tutors = [ tutor for tutor in instance.alumne.tutorsDeLAlumne() ]
    if user: instance.professor = User2Professor( user )

    #
    # Només es poden modificar assistències 
    #
    nMaxDies = settings.CUSTOM_PERIODE_MODIFICACIO_ASSISTENCIA
    if isUpdate and instance.impartir.dia_impartir < ( dt.date.today() - dt.timedelta( days = nMaxDies) ):
        errors.setdefault(NON_FIELD_ERRORS, []).append( u'''Aquest controll d'assistència és massa antic per ser modificat (Té més de {0} dies)'''.format(nMaxDies) )
        
    #todo: altres controls:
    socTutor = hasattr(instance, 'professor') and instance.professor and instance.professor in tutors
    daqui_2_hores = dt.datetime.now() + dt.timedelta( hours = 2)
    daqui_15_dies = dt.datetime.now() + dt.timedelta( days = 15)
    if isUpdate and bool(instance.estat) and (not socTutor and instance.impartir.diaHora() > daqui_2_hores): 
        errors.setdefault(NON_FIELD_ERRORS, []).append( u'''Encara no es pot entrar aquesta assistència 
                                    (Falta {0} per poder-ho fer )'''.format(
                                      timesince_filter( dt.datetime.now(), instance.impartir.diaHora() - dt.timedelta( hours = 2) ) ) )
    if isUpdate and bool(instance.estat) and (socTutor and instance.impartir.diaHora() > daqui_15_dies): 
        errors.setdefault(NON_FIELD_ERRORS, []).append( u'''Encara no es pot entrar aquesta assistència 
                                    (Falta {0} per poder-ho fer )'''.format(
                                      timesince_filter( dt.datetime.now(), instance.impartir.diaHora() - dt.timedelta( days = 15) ) ) )

    #Una falta justificada pel tutor no pot ser matxacada per un professor
    socTutor = hasattr(instance, 'professor') and instance.professor and instance.professor in tutors    
    justificadaDB = instance.instanceDB and instance.instanceDB.estat and instance.instanceDB.estat.codi_estat.upper() == 'J'
    justificadaAra = instance.estat and instance.estat.codi_estat.upper() == 'J'
    posat_pel_tutor = instance.instanceDB and instance.instanceDB.professor and instance.instanceDB.professor in tutors
    
    if not socTutor and justificadaDB and posat_pel_tutor and not justificadaAra:
        errors.setdefault(NON_FIELD_ERRORS, []).append( u'''
                                  La falta d'en {0} no es pot modificar. El tutor Sr(a) {1} ha justificat la falta.  
                                                            '''.format(
                                       instance.alumne, instance.instanceDB.professor ) )
        
    #No es poden justificar faltes si s'ha enviat una carta.
    if not justificadaDB and justificadaAra:
        data_control_mes_3 = instance.impartir.dia_impartir + timedelta( days = 3 )
        dins_ambit_carta = ( CartaAbsentisme
                             .objects
                             .exclude( carta_esborrada_moment__isnull = False )
                             .filter( alumne = instance.alumne,
                                      data_carta__gte = data_control_mes_3
                                     )
                             .exists()
                            )
        if dins_ambit_carta:
            errors.setdefault(NON_FIELD_ERRORS, []).append( u'''
                                  La falta d'en {0} no es pot modificar. El tutor ha inclòs la falta en una Carta.  
                                                            '''.format(
                                       instance.alumne ) )
                
    #Només el tutor, el professor de guardia o el professor titular pot modificar un control d'assistència:
    if user:
        professors_habilitats = tutors
        if instance.professor: professors_habilitats.append( instance.professor.pk )
        if instance.impartir.horari.professor: professors_habilitats.append( instance.impartir.horari.professor.pk )
        if instance.impartir.professor_guardia: professors_habilitats.append( instance.impartir.professor_guardia.pk )
        if user.pk not in professors_habilitats:
            errors.setdefault(NON_FIELD_ERRORS, []).append( u'''Només el professor de l'assignatura, 
                                            el professor de guardia que ha passat llista o el tutor poden variar una assistència. 
                                                            ''' )         
    
    if len( errors ) > 0:
        raise ValidationError(errors)

    #Justificada: si el tutor l'havia justificat deixo al tutor com el que ha desat la falta:
    if justificadaDB and posat_pel_tutor:
        instance.professor = instance.instanceDB.professor
Example #13
0
 def test_no_args(self):
     self.assertEqual(timesince_filter(None), '')
Example #14
0
 def last_update(self, subscription):
     return timesince_filter(subscription.updated_at)
Example #15
0
 def lease_expires_in(self, subscription):
     return timesince_filter(subscription.lease_expiration)
Example #16
0
 def test_no_args(self):
     self.assertEqual(timesince_filter(None), '')