Statistics
| Branch: | Tag: | Revision:

root / snf-pithos-app / pithos / api / dispatch.py @ 87835e94

History | View | Annotate | Download (2 kB)

1
#from pithos.backends import connect_backend
2
from pithos.api.util import hashmap_md5, get_backend
3

    
4
from django.core.mail import send_mail
5

    
6
from django.conf import settings
7

    
8
import socket
9
from smtplib import SMTPException
10

    
11

    
12
def update_md5(m):
13
    if m['resource'] != 'object' or m['details']['action'] != 'object update':
14
        return
15

    
16
    backend = get_backend()
17
    backend.pre_exec()
18

    
19
    path = m['value']
20
    account, container, name = path.split('/', 2)
21
    version = m['details']['version']
22
    meta = None
23
    try:
24
        meta = backend.get_object_meta(
25
            account, account, container, name, 'pithos', version)
26
        if meta['checksum'] == '':
27
            size, hashmap = backend.get_object_hashmap(
28
                account, account, container, name, version)
29
            checksum = hashmap_md5(backend, hashmap, size)
30
            backend.update_object_checksum(
31
                account, account, container, name, version, checksum)
32
            print 'INFO: Updated checksum for path "%s"' % (path,)
33
    except Exception, e:
34
        print 'WARNING: Can not update checksum for path "%s" (%s)' % (path, e)
35

    
36
    backend.post_exec()
37
    backend.close()
38

    
39

    
40
def send_sharing_notification(m):
41
    if m['resource'] != 'sharing':
42
        return
43

    
44
    members = m['details']['members']
45
    user = m['details']['user']
46
    path = m['value']
47
    account, container, name = path.split('/', 2)
48

    
49
    subject = 'Invitation to a Pithos+ shared object'
50
    from_email = settings.SERVER_EMAIL
51
    recipient_list = members
52
    message = ("User %s has invited you to a Pithos+ shared object."
53
               "You can view it under \"Shared to me\" at \"%s\".")
54
    message = message % (user, path)
55
    try:
56
        send_mail(subject, message, from_email, recipient_list)
57
        print 'INFO: Sharing notification sent for path "%s" to %s' % (
58
            path, ','.join(recipient_list))
59
    except (SMTPException, socket.error) as e:
60
        print 'WARNING: Can not update send email for sharing "%s" (%s)' % (
61
            path, e)