store_api.py 7.26 KB
Newer Older
Guba Sándor committed
1 2 3 4 5
from django.http import Http404
import json
import requests
import django.conf

Kálmán Viktor committed
6 7 8
from datetime import datetime
from sizefield.utils import filesizeformat

Guba Sándor committed
9 10 11
settings = django.conf.settings.STORE_SETTINGS


12 13 14 15
class Mock(object):
    pass


Guba Sándor committed
16 17 18 19 20
def get_host():
    return settings['store_url']


def post_request(url, payload):
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
    try:
        headers = {'content-type': 'application/json'}
        if settings['ssl_auth'] == 'True' and settings['basic_auth'] == 'True':
            r = requests.post(url, data=payload, headers=headers,
                              verify=settings['verify_ssl'] == 'True',
                              cert=(settings['store_client_cert'],
                                    settings['store_client_key']),
                              auth=(settings['store_client_user'],
                                    settings['store_client_pass'])
                              )
        elif settings['ssl_auth'] == 'True':
            r = requests.post(url, data=payload, headers=headers,
                              verify=settings['verify_ssl'] == 'True',
                              cert=(settings['store_client_cert'],
                                    settings['store_client_key'])
                              )
        elif settings['basic_auth'] == 'True':
            r = requests.post(url, data=payload, headers=headers,
                              verify=settings['verify_ssl'] == 'True',
                              auth=(settings['store_client_user'],
                                    settings['store_client_pass'])
                              )
        else:
            r = requests.post(url, data=payload, headers=headers,
                              verify=settings['verify_ssl'] == 'True'
                              )
        return r
    except:
        dummy = Mock()
        setattr(dummy, "status_code", 200)
        setattr(dummy, "content", "[]")
        return dummy
Guba Sándor committed
53 54 55


def get_request(url):
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
    try:
        headers = {'content-type': 'application/json'}
        if settings['ssl_auth'] == 'True' and settings['basic_auth'] == 'True':
            r = requests.get(
                url,
                headers=headers,
                verify=settings['verify_ssl'] == 'True',
                cert=(
                    settings['store_client_cert'],
                    settings['store_client_key']),
                auth=(
                    settings['store_client_user'],
                    settings['store_client_pass']))
        elif settings['ssl_auth'] == 'True':
            r = requests.get(
                url,
                headers=headers,
                verify=settings['verify_ssl'] == 'True',
                cert=(
                    settings['store_client_cert'],
                    settings['store_client_key']))
        elif settings['basic_auth'] == 'True':
            r = requests.get(
                url,
                headers=headers,
                verify=settings['verify_ssl'] == 'True',
                auth=(
                    settings['store_client_user'],
                    settings['store_client_pass']))
        else:
            r = requests.get(url, headers=headers,
                             verify=settings['verify_ssl'] == 'True'
                             )
            return r
    except:
        dummy = Mock()
        setattr(dummy, "status_code", 200)
        setattr(dummy, "content", "[]")
        return dummy
Guba Sándor committed
95 96 97 98 99 100 101 102


def listfolder(neptun, path):
    url = settings['store_url']+'/'+neptun
    payload = json.dumps({'CMD': 'LIST', 'PATH': path})
    r = post_request(url, payload)
    if r.status_code == requests.codes.ok:
        tupplelist = json.loads(r.content)
Kálmán Viktor committed
103
        return tupplelist
Guba Sándor committed
104 105 106 107 108 109 110 111 112 113
    else:
        raise Http404


def toplist(neptun):
    url = settings['store_url']+'/'+neptun
    payload = json.dumps({'CMD': 'TOPLIST'})
    r = post_request(url, payload)
    if r.status_code == requests.codes.ok:
        tupplelist = json.loads(r.content)
Kálmán Viktor committed
114
        return tupplelist
Guba Sándor committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
    else:
        raise Http404


def requestdownload(neptun, path):
    url = settings['store_url']+'/'+neptun
    payload = json.dumps({'CMD': 'DOWNLOAD', 'PATH': path})
    r = post_request(url, payload)
    response = json.loads(r.content)
    return response['LINK']


def requestupload(neptun, path):
    url = settings['store_url']+'/'+neptun
    payload = json.dumps({'CMD': 'UPLOAD', 'PATH': path})
    r = post_request(url, payload)
    response = json.loads(r.content)
    if r.status_code == requests.codes.ok:
        return response['LINK']
    else:
        raise Http404


def requestremove(neptun, path):
    url = settings['store_url']+'/'+neptun
    payload = json.dumps({'CMD': 'REMOVE', 'PATH': path})
    r = post_request(url, payload)
    if r.status_code == requests.codes.ok:
        return True
    else:
        return False


def requestnewfolder(neptun, path):
    url = settings['store_url']+'/'+neptun
    payload = json.dumps({'CMD': 'NEW_FOLDER', 'PATH': path})
    r = post_request(url, payload)
    if r.status_code == requests.codes.ok:
        return True
    else:
        return False


def requestrename(neptun, old_path, new_name):
    url = settings['store_url']+'/'+neptun
    payload = json.dumps(
        {'CMD': 'RENAME', 'NEW_NAME': new_name, 'PATH': old_path})
    r = post_request(url, payload)
    if r.status_code == requests.codes.ok:
        return True
    else:
        return False


def requestquota(neptun):
    url = settings['store_url']+'/'+neptun
    r = get_request(url)
    if r.status_code == requests.codes.ok:
        return json.loads(r.content)
    else:
        return False


def set_quota(neptun, quota):
    url = settings['store_url']+'/quota/'+neptun
    payload = json.dumps({'QUOTA': quota})
    r = post_request(url, payload)
    if r.status_code == requests.codes.ok:
        return True
    else:
        return False


def userexist(neptun):
    url = settings['store_url']+'/'+neptun
    r = get_request(url)
    if r.status_code == requests.codes.ok:
        return True
    else:
        return False


def createuser(neptun, password, key_list, quota):
    url = settings['store_url']+'/new/'+neptun
    payload = json.dumps(
        {'SMBPASSWD': password, 'KEYS': key_list, 'QUOTA': quota})
    r = post_request(url, payload)
    if r.status_code == requests.codes.ok:
        return True
    else:
        return False


def updateauthorizationinfo(neptun, password, key_list):
    url = settings['store_url']+'/set/'+neptun
    payload = json.dumps({'SMBPASSWD': password, 'KEYS': key_list})
    r = post_request(url, payload)
    if r.status_code == requests.codes.ok:
        return True
    else:
        return False
Kálmán Viktor committed
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238


def process_list(content):
    for d in content:
        d['human_readable_date'] = datetime.utcfromtimestamp(float(
            d['MTIME']))
        delta = (datetime.utcnow() - d['human_readable_date']).total_seconds()
        d['is_new'] = delta < 5 and delta > 0
        d['human_readable_size'] = (
            "directory" if d['TYPE'] == "D" else
            filesizeformat(float(d['SIZE'])))

        d['path'] = d['DIR']
        if len(d['path']) == 1 and d['path'][0] == ".":
            d['path'] = "/"
        else:
            d['path'] = "/" + d['path'] + "/"

        d['path'] += d['NAME']
        if d['TYPE'] == "D":
            d['path'] += "/"

    return sorted(content, key=lambda k: k['TYPE'])