Statistics
| Branch: | Tag: | Revision:

root / snf-pithos-app / pithos / api / dispatch.py @ 75cf66bf

History | View | Annotate | Download (2.7 kB)

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

    
8
from django.core.mail import send_mail
9
from django.utils.translation import ugettext as _
10

    
11
from astakos.im.settings import DEFAULT_FROM_EMAIL
12

    
13
import socket
14
from smtplib import SMTPException
15

    
16
def update_md5(m):
17
    if m['resource'] != 'object' or m['details']['action'] != 'object update':
18
        return
19
    
20
    backend = connect_backend(db_module=BACKEND_DB_MODULE,
21
                              db_connection=BACKEND_DB_CONNECTION,
22
                              block_module=BACKEND_BLOCK_MODULE,
23
                              block_path=BACKEND_BLOCK_PATH,
24
                              queue_module=BACKEND_QUEUE_MODULE,
25
                              queue_connection=BACKEND_QUEUE_CONNECTION)
26
    backend.default_policy['quota'] = BACKEND_QUOTA
27
    backend.default_policy['versioning'] = BACKEND_VERSIONING
28
    
29
    path = m['value']
30
    account, container, name = path.split('/', 2)
31
    version = m['details']['version']
32
    meta = None
33
    try:
34
        meta = backend.get_object_meta(account, account, container, name, 'pithos', version)
35
        if meta['checksum'] == '':
36
            size, hashmap = backend.get_object_hashmap(account, account, container, name, version)
37
            checksum = hashmap_md5(backend, hashmap, size)
38
            backend.update_object_checksum(account, account, container, name, version, checksum)
39
            print 'INFO: Updated checksum for path "%s"' % (path,)
40
    except Exception, e:
41
        print 'WARNING: Can not update checksum for path "%s" (%s)' % (path, e)
42
    
43
    backend.close()
44

    
45
def send_sharing_notification(m):
46
    if m['resource'] != 'sharing':
47
        return
48
    
49
    members = m['details']['members']
50
    user = m['details']['user']
51
    path = m['value']
52
    account, container, name = path.split('/', 2)
53
    
54
    subject = 'Invitation to a Pithos+ shared object'
55
    from_email = DEFAULT_FROM_EMAIL
56
    recipient_list = members
57
    message = 'User %s has invited you to a Pithos+ shared object. You can view it under "Shared to me" at "%s".' %(user, path)
58
    try:
59
        send_mail(subject, message, from_email, recipient_list)
60
        print 'INFO: Sharing notification sent for path "%s" to %s' % (path, ','.join(recipient_list))
61
    except (SMTPException, socket.error) as e:
62
        print 'WARNING: Can not update send email for sharing "%s" (%s)' % (path, e)