Statistics
| Branch: | Tag: | Revision:

root / snf-pithos-app / pithos / api / dispatch.py @ 8c7e1398

History | View | Annotate | Download (3.5 kB)

1 91884d63 Giorgos Korfiatis
# Copyright 2012, 2013 GRNET S.A. All rights reserved.
2 91884d63 Giorgos Korfiatis
#
3 91884d63 Giorgos Korfiatis
# Redistribution and use in source and binary forms, with or
4 91884d63 Giorgos Korfiatis
# without modification, are permitted provided that the following
5 91884d63 Giorgos Korfiatis
# conditions are met:
6 91884d63 Giorgos Korfiatis
#
7 91884d63 Giorgos Korfiatis
#   1. Redistributions of source code must retain the above
8 91884d63 Giorgos Korfiatis
#      copyright notice, this list of conditions and the following
9 91884d63 Giorgos Korfiatis
#      disclaimer.
10 91884d63 Giorgos Korfiatis
#
11 91884d63 Giorgos Korfiatis
#   2. Redistributions in binary form must reproduce the above
12 91884d63 Giorgos Korfiatis
#      copyright notice, this list of conditions and the following
13 91884d63 Giorgos Korfiatis
#      disclaimer in the documentation and/or other materials
14 91884d63 Giorgos Korfiatis
#      provided with the distribution.
15 91884d63 Giorgos Korfiatis
#
16 91884d63 Giorgos Korfiatis
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 91884d63 Giorgos Korfiatis
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 91884d63 Giorgos Korfiatis
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 91884d63 Giorgos Korfiatis
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 91884d63 Giorgos Korfiatis
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 91884d63 Giorgos Korfiatis
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 91884d63 Giorgos Korfiatis
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 91884d63 Giorgos Korfiatis
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 91884d63 Giorgos Korfiatis
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 91884d63 Giorgos Korfiatis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 91884d63 Giorgos Korfiatis
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 91884d63 Giorgos Korfiatis
# POSSIBILITY OF SUCH DAMAGE.
28 91884d63 Giorgos Korfiatis
#
29 91884d63 Giorgos Korfiatis
# The views and conclusions contained in the software and
30 91884d63 Giorgos Korfiatis
# documentation are those of the authors and should not be
31 91884d63 Giorgos Korfiatis
# interpreted as representing official policies, either expressed
32 91884d63 Giorgos Korfiatis
# or implied, of GRNET S.A.
33 91884d63 Giorgos Korfiatis
34 b1dadd0e Sofia Papagiannaki
#from pithos.backends import connect_backend
35 b1dadd0e Sofia Papagiannaki
from pithos.api.util import hashmap_md5, get_backend
36 b3155065 Antony Chazapis
37 a74ba506 Sofia Papagiannaki
from django.core.mail import send_mail
38 a74ba506 Sofia Papagiannaki
39 18cd931f Sofia Papagiannaki
from django.conf import settings
40 a74ba506 Sofia Papagiannaki
41 75cf66bf Sofia Papagiannaki
import socket
42 75cf66bf Sofia Papagiannaki
from smtplib import SMTPException
43 75cf66bf Sofia Papagiannaki
44 d50ed8d4 Sofia Papagiannaki
45 b3155065 Antony Chazapis
def update_md5(m):
46 b3155065 Antony Chazapis
    if m['resource'] != 'object' or m['details']['action'] != 'object update':
47 b3155065 Antony Chazapis
        return
48 d50ed8d4 Sofia Papagiannaki
49 b1dadd0e Sofia Papagiannaki
    backend = get_backend()
50 3248f20a Sofia Papagiannaki
    backend.pre_exec()
51 d50ed8d4 Sofia Papagiannaki
52 b3155065 Antony Chazapis
    path = m['value']
53 b3155065 Antony Chazapis
    account, container, name = path.split('/', 2)
54 b3155065 Antony Chazapis
    version = m['details']['version']
55 b3155065 Antony Chazapis
    meta = None
56 b3155065 Antony Chazapis
    try:
57 d50ed8d4 Sofia Papagiannaki
        meta = backend.get_object_meta(
58 d50ed8d4 Sofia Papagiannaki
            account, account, container, name, 'pithos', version)
59 b3155065 Antony Chazapis
        if meta['checksum'] == '':
60 d50ed8d4 Sofia Papagiannaki
            size, hashmap = backend.get_object_hashmap(
61 d50ed8d4 Sofia Papagiannaki
                account, account, container, name, version)
62 b3155065 Antony Chazapis
            checksum = hashmap_md5(backend, hashmap, size)
63 d50ed8d4 Sofia Papagiannaki
            backend.update_object_checksum(
64 d50ed8d4 Sofia Papagiannaki
                account, account, container, name, version, checksum)
65 b3155065 Antony Chazapis
            print 'INFO: Updated checksum for path "%s"' % (path,)
66 b3155065 Antony Chazapis
    except Exception, e:
67 b3155065 Antony Chazapis
        print 'WARNING: Can not update checksum for path "%s" (%s)' % (path, e)
68 d50ed8d4 Sofia Papagiannaki
69 3248f20a Sofia Papagiannaki
    backend.post_exec()
70 b3155065 Antony Chazapis
    backend.close()
71 a74ba506 Sofia Papagiannaki
72 d50ed8d4 Sofia Papagiannaki
73 a74ba506 Sofia Papagiannaki
def send_sharing_notification(m):
74 a74ba506 Sofia Papagiannaki
    if m['resource'] != 'sharing':
75 a74ba506 Sofia Papagiannaki
        return
76 d50ed8d4 Sofia Papagiannaki
77 a74ba506 Sofia Papagiannaki
    members = m['details']['members']
78 a74ba506 Sofia Papagiannaki
    user = m['details']['user']
79 a74ba506 Sofia Papagiannaki
    path = m['value']
80 a74ba506 Sofia Papagiannaki
    account, container, name = path.split('/', 2)
81 d50ed8d4 Sofia Papagiannaki
82 a74ba506 Sofia Papagiannaki
    subject = 'Invitation to a Pithos+ shared object'
83 18cd931f Sofia Papagiannaki
    from_email = settings.SERVER_EMAIL
84 a74ba506 Sofia Papagiannaki
    recipient_list = members
85 18cd931f Sofia Papagiannaki
    message = ("User %s has invited you to a Pithos+ shared object."
86 18cd931f Sofia Papagiannaki
               "You can view it under \"Shared to me\" at \"%s\".")
87 18cd931f Sofia Papagiannaki
    message = message % (user, path)
88 a74ba506 Sofia Papagiannaki
    try:
89 a74ba506 Sofia Papagiannaki
        send_mail(subject, message, from_email, recipient_list)
90 d50ed8d4 Sofia Papagiannaki
        print 'INFO: Sharing notification sent for path "%s" to %s' % (
91 d50ed8d4 Sofia Papagiannaki
            path, ','.join(recipient_list))
92 a74ba506 Sofia Papagiannaki
    except (SMTPException, socket.error) as e:
93 d50ed8d4 Sofia Papagiannaki
        print 'WARNING: Can not update send email for sharing "%s" (%s)' % (
94 d50ed8d4 Sofia Papagiannaki
            path, e)