Statistics
| Branch: | Tag: | Revision:

root / snf-pithos-app / pithos / api / dispatch.py @ f4fbb0fa

History | View | Annotate | Download (2.9 kB)

1
from pithos.api.settings import (BACKEND_DB_MODULE, BACKEND_DB_CONNECTION,
2
                                 BACKEND_BLOCK_MODULE, BACKEND_BLOCK_PATH,
3
                                 BACKEND_BLOCK_UMASK,
4
                                 BACKEND_QUEUE_MODULE, BACKEND_QUEUE_HOSTS,
5
                                 BACKEND_QUEUE_EXCHANGE,
6
                                 BACKEND_QUOTA, BACKEND_VERSIONING)
7
from pithos.backends import connect_backend
8
from pithos.api.util import hashmap_md5
9

    
10
from django.core.mail import send_mail
11
from django.utils.translation import ugettext as _
12

    
13
from astakos.im.settings import DEFAULT_FROM_EMAIL
14

    
15
import socket
16
from smtplib import SMTPException
17

    
18

    
19
def update_md5(m):
20
    if m['resource'] != 'object' or m['details']['action'] != 'object update':
21
        return
22

    
23
    backend = connect_backend(db_module=BACKEND_DB_MODULE,
24
                              db_connection=BACKEND_DB_CONNECTION,
25
                              block_module=BACKEND_BLOCK_MODULE,
26
                              block_path=BACKEND_BLOCK_PATH,
27
                              block_umask=BACKEND_BLOCK_UMASK,
28
                              queue_module=BACKEND_QUEUE_MODULE,
29
                              queue_hosts=BACKEND_QUEUE_HOSTS,
30
                              queue_exchange=BACKEND_QUEUE_EXHANGE)
31
    backend.default_policy['quota'] = BACKEND_QUOTA
32
    backend.default_policy['versioning'] = BACKEND_VERSIONING
33

    
34
    path = m['value']
35
    account, container, name = path.split('/', 2)
36
    version = m['details']['version']
37
    meta = None
38
    try:
39
        meta = backend.get_object_meta(
40
            account, account, container, name, 'pithos', version)
41
        if meta['checksum'] == '':
42
            size, hashmap = backend.get_object_hashmap(
43
                account, account, container, name, version)
44
            checksum = hashmap_md5(backend, hashmap, size)
45
            backend.update_object_checksum(
46
                account, account, container, name, version, checksum)
47
            print 'INFO: Updated checksum for path "%s"' % (path,)
48
    except Exception, e:
49
        print 'WARNING: Can not update checksum for path "%s" (%s)' % (path, e)
50

    
51
    backend.close()
52

    
53

    
54
def send_sharing_notification(m):
55
    if m['resource'] != 'sharing':
56
        return
57

    
58
    members = m['details']['members']
59
    user = m['details']['user']
60
    path = m['value']
61
    account, container, name = path.split('/', 2)
62

    
63
    subject = 'Invitation to a Pithos+ shared object'
64
    from_email = DEFAULT_FROM_EMAIL
65
    recipient_list = members
66
    message = 'User %s has invited you to a Pithos+ shared object. You can view it under "Shared to me" at "%s".' % (user, path)
67
    try:
68
        send_mail(subject, message, from_email, recipient_list)
69
        print 'INFO: Sharing notification sent for path "%s" to %s' % (
70
            path, ','.join(recipient_list))
71
    except (SMTPException, socket.error) as e:
72
        print 'WARNING: Can not update send email for sharing "%s" (%s)' % (
73
            path, e)