summaryrefslogtreecommitdiffstats
path: root/polynum_blackboard/management/commands/send-to-mycourse.py
blob: b5f14261a87291d187f69313bccd4b942d3b3d75 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# vim:spell:spelllang=fr
# -*- encoding: utf-8 -*-

import logging
import os
from optparse import make_option

from django.core.management.base import BaseCommand
from django.db import transaction

from ... import models, django_ws

class Command(BaseCommand):
    '''
       Envoyer les demandes blackboard
    '''
    can_import_django_settings = True
    requires_model_validation = True
    args = ''
    help = 'Envoyer les demandes blackboard'

    option_list = BaseCommand.option_list + (
            make_option("--send", action="store_true"),
            make_option("--list", action="store_true"),
            make_option("--delete", action="store", type="string"))

    @transaction.commit_on_success
    def handle(self, *args, **options):
        logger = logging.getLogger(__name__)
        bb_pushs = models.BlackBoardPush.objects.filter(sent=False)
        if options['delete']:
            qs = models.BlackBoardPush.objects.filter(id__in=options['delete'].split(','), request__isnull=Falsoe)
            for push in qs:
                print ' - deleted push request %s for document %s' % (push.id, push.request.id)
            qs.delete()
        if options['list']:
            for push in bb_pushs:
                if not push.request or not push.request.uploadfile:
                    continue
                print ' -', push.id, 'document', os.path.basename(push.request.uploadfile.name).encode('utf-8'), '(%s)' % \
                    push.request.id, 'de', push.request.user.display_name().encode('utf-8'), 'dans le cours', push.course_name.encode('utf-8'),
                if push.visible_to_students:
                    print '(visible aux étudiants)',
                print
        if options['send']:
            conn = django_ws.get_bb_conn()
            for push in bb_pushs:
                logger.info('sending request %s to mycourse', push.request.pk)
                logger.info('from user %s', push.request.user.display_name().encode('utf-8'))
                logger.info('into course %s', push.course_name.encode('utf-8'))
                if push.visible_to_students:
                    logger.info('visible to students')
                try:
                    ok, result = conn.send_file(push.request.uploadfile, [push.course_name], push.visible_to_students)
                    push.sent = True
                    if ok:
                        logger.info('sending succeeded')
                    else:
                        push.failure = result
                        logger.error('sending failed: %r', result.encode('utf-8'))
                except Exception, e:
                    logger.exception('sending failed')
                    push.failure = unicode(e)
                push.sent = True
                push.save()