|
1 | | -# Copyright (c) 2018, Dolores Juliana Fdez Martin |
| 1 | +# Copyright (c) 2018-2019, Dolores Juliana Fdez Martin |
2 | 2 | # License: GNU General Public License v3. See license.txt |
3 | 3 | # |
4 | 4 | # This file is part of Pibiapp_Nextcloud. |
|
23 | 23 | import requests |
24 | 24 | from json import dumps |
25 | 25 | from frappe.modules.utils import get_doctype_module, get_module_app |
| 26 | +from frappe.desk.tags import DocTags |
26 | 27 | from pibiapp.nextcloud import nextcloud_apis |
27 | 28 | import json |
28 | 29 | import os |
29 | 30 | import time |
| 31 | +import sys |
30 | 32 |
|
31 | 33 | class nextcloud_link(): |
32 | 34 | def __init__(self): |
@@ -56,13 +58,9 @@ def __init__(self): |
56 | 58 |
|
57 | 59 | def tagging(self, doc, idfile, relational): |
58 | 60 | self.actualizetags() |
59 | | - doctype = doc.attached_to_doctype |
60 | | - module = get_doctype_module(doctype) |
61 | | - name = doc.attached_to_name |
62 | | - self.puttag( doctype, idfile) |
63 | | - self.puttag( module, idfile) |
64 | | - self.puttag( name, idfile) |
65 | | - if relational: self.relationaltags(doctype, name, idfile) |
| 61 | + lista = self.listtags(doc, idfile, relational) |
| 62 | + for display_name in lista: |
| 63 | + self.puttag( display_name, idfile) |
66 | 64 |
|
67 | 65 | def puttag(self, display_name, idfile): |
68 | 66 | idtag = self.searchtag( display_name) |
@@ -107,68 +105,150 @@ def inserttag(self, idtag, display_name): |
107 | 105 | def relationaltags(self, doctype, name, idfile): |
108 | 106 | docntrans = frappe.get_doc(doctype, name) |
109 | 107 | meta = frappe.get_meta(doctype) |
| 108 | + lista = "" |
110 | 109 | for lf in meta.get_link_fields(): |
111 | | - name = docntrans.get(lf.fieldname) |
112 | | - if name != "" and name != None: |
113 | | - self.puttag( name, idfile) |
| 110 | + tag = docntrans.get(lf.fieldname) |
| 111 | + if tag != "" and tag != None: |
| 112 | + lista = lista + " # " + tag |
| 113 | + lista = lista + DocTags(doctype).get_tags(name).replace("," , " # ") |
| 114 | + return lista |
| 115 | + |
| 116 | + def deletetags(self, doc, idfile, relational=True): |
| 117 | + lista = self.listtags(doc, idfile, relational) |
| 118 | + status_code = self.webdav.deletetags(idfile, nodelete=lista) |
| 119 | + |
| 120 | + def listtags(self, doc, idfile, relational): |
| 121 | + doctype = doc.attached_to_doctype |
| 122 | + module = get_doctype_module(doctype) |
| 123 | + name = doc.attached_to_name |
| 124 | + lista = doctype + " # " + module + " # " + name |
| 125 | + if relational: lista = lista + self.relationaltags(doctype, name, idfile) |
| 126 | + return lista.split(" # ") |
| 127 | + |
| 128 | + def shareModule(self, doc): |
| 129 | + # add group for module |
| 130 | + data_json = doc.nc.ocs.getGroup(doc.nc.module) |
| 131 | + data_string = json.dumps(data_json) |
| 132 | + decoded = json.loads(data_string) |
| 133 | + isgroup = str(decoded["ocs"]["meta"]["statuscode"]) |
| 134 | + if isgroup == '404': |
| 135 | + data_json = doc.nc.ocs.addGroup(doc.nc.module) |
| 136 | + # add Share group in Nextcloud |
| 137 | + shareType = 1 |
| 138 | + permit = 1 |
| 139 | + data_json = doc.nc.ocs.createShare(doc.nc.pathglobal,shareType,shareWith=doc.nc.module,publicUpload=True,password=None,permissions=permit) |
| 140 | + return data_json |
| 141 | + |
114 | 142 |
|
115 | 143 | @frappe.whitelist() |
116 | | -def nextcloud_insert(doc, method=None): |
| 144 | +def nextcloud_before_insert(doc, method=None): |
| 145 | + doc.flags.ignore_nc = True |
117 | 146 | nc = nextcloud_link() |
118 | 147 | if not nc.isconnect: return |
| 148 | + doc.flags.ignore_file_validate = True |
119 | 149 | doctype = doc.attached_to_doctype |
120 | 150 | module = get_doctype_module(doctype) |
121 | 151 | # Excluded module |
122 | 152 | if module in nc.excludedmodules: return |
| 153 | + # File previously attached to another transaction |
| 154 | + if not doc.file_name or doc.file_name == None: return |
| 155 | + if " NC/f/" in doc.file_name: return |
| 156 | + doc.flags.ignore_nc = False |
123 | 157 | site = frappe.local.site |
124 | 158 | if doc.is_private: local_fileobj = "./" + site + doc.file_url |
125 | 159 | else: local_fileobj = "./" + site + "/public" + doc.file_url |
126 | 160 | fileobj = local_fileobj.split('/') |
127 | 161 | uu = len(fileobj) - 1 |
128 | | - # get path |
129 | | - app = get_module_app(module) |
130 | | - path = nc.initialpath + "/" + app + "/" + module + "/" + doctype |
131 | | - pathglobal = path + "/" + fileobj[uu] |
| 162 | + doc.nc = nc |
| 163 | + doc.nc.module = module |
| 164 | + doc.nc.app = get_module_app(module) |
| 165 | + doc.nc.path = nc.initialpath + "/" + doc.nc.app + "/" + module + "/" + doctype |
| 166 | + doc.nc.pathglobal = doc.nc.path + "/" + fileobj[uu] |
| 167 | + doc.nc.local_fileobj = local_fileobj |
| 168 | + doc.nc.remote_fileobj=fileobj[uu] |
| 169 | + |
| 170 | + |
| 171 | +@frappe.whitelist() |
| 172 | +def nextcloud_insert(doc, method=None): |
| 173 | + if doc.flags.ignore_nc: return |
132 | 174 | # upload to nextcloud |
133 | | - nc.webdav.upload(local_fileobj, remote_fileobj=fileobj[uu], nc_path=path) |
134 | | - # add group for module |
135 | | - data_json = nc.ocs.getGroup(module) |
136 | | - data_string = json.dumps(data_json) |
137 | | - decoded = json.loads(data_string) |
138 | | - isgroup = str(decoded["ocs"]["meta"]["statuscode"]) |
139 | | - if isgroup == '404': |
140 | | - data_json = nc.ocs.addGroup(module) |
141 | | - # add Share group in Nextcloud |
142 | | - shareType = 1 |
143 | | - permit = 1 |
144 | | - data_json = nc.ocs.createShare(pathglobal,shareType,shareWith=module,publicUpload=True,password=None,permissions=permit) |
| 175 | + if not "http" in doc.nc.local_fileobj: |
| 176 | + doc.nc.webdav.upload(local_fileobj=doc.nc.local_fileobj, remote_fileobj=doc.nc.remote_fileobj, nc_path=doc.nc.path) |
| 177 | + else: |
| 178 | + data = frappe.db.get_value("File", {"file_url": doc.file_url , "file_name": ["like", "%NC/f/%"]}, ["attached_to_doctype", "name", "file_name"], as_dict=True) |
| 179 | + if data: |
| 180 | + if doc.attached_to_doctype != data.attached_to_doctype: |
| 181 | + doctype = data.attached_to_doctype |
| 182 | + module = get_doctype_module(doctype) |
| 183 | + app = get_module_app(module) |
| 184 | + doc.nc.pathglobal = doc.nc.initialpath + "/" + app + "/" + module + "/" + doctype + "/" + doc.file_name |
| 185 | + data_json = doc.nc.shareModule(doc) |
| 186 | + fname = data.file_name.replace(" NC/f/","#") |
| 187 | + doc.file_name = fname.split("#")[0] + " NC(" + data.name + ")/f/" + fname.split("#")[1] |
| 188 | + doc.save() |
| 189 | + return |
| 190 | + data_json = doc.nc.shareModule(doc) |
145 | 191 | # add public Share in Nextcloud |
146 | | - if nc.sharepublic or doc.is_private == False: |
| 192 | + if doc.nc.sharepublic or doc.is_private == False: |
147 | 193 | shareType = 3 |
148 | | - data_json = nc.ocs.createShare(pathglobal,shareType) |
| 194 | + data_json = doc.nc.ocs.createShare(doc.nc.pathglobal,shareType) |
149 | 195 | if data_json == "": |
150 | 196 | time.sleep(2) |
151 | | - data_json = nc.ocs.createShare(pathglobal,shareType) |
| 197 | + data_json = doc.nc.ocs.createShare(doc.nc.pathglobal,shareType) |
152 | 198 | data_string = json.dumps(data_json) |
153 | 199 | decoded = json.loads(data_string) |
154 | | - fileid = str(decoded["ocs"]["data"]["file_source"]) |
155 | | - if nc.sharepublic or doc.is_private == False: |
| 200 | + try: |
| 201 | + fileid = str(decoded["ocs"]["data"]["file_source"]) |
| 202 | + except TypeError: |
| 203 | + fname = frappe.db.get_value("File", {"file_name": ["like", doc.file_name + " NC/f/%"]}, "name") |
| 204 | + docorigin = frappe.get_doc('File', str(fname)) |
| 205 | + if docorigin: |
| 206 | + docorigin.content_hash = doc.content_hash |
| 207 | + docorigin.flags.ignore_file_validate = True |
| 208 | + docorigin.save() |
| 209 | + if doc.nc.enabletagging: |
| 210 | + fileid = str(docorigin.file_name.replace(" NC/f/","#").split("#")[1]) |
| 211 | + doc.nc.deletetags(docorigin, fileid, relational=doc.nc.relationaltagging) |
| 212 | + doc.nc.tagging(docorigin, fileid, relational=doc.nc.relationaltagging) |
| 213 | + os.remove(doc.nc.local_fileobj) |
| 214 | + doc.delete() |
| 215 | + frappe.db.commit() |
| 216 | + sys.exit() |
| 217 | + if doc.nc.sharepublic or doc.is_private == False: |
156 | 218 | urllink = str(decoded["ocs"]["data"]["url"]) |
157 | 219 | else: |
158 | | - urllink = nc.url + "/f/" + fileid |
| 220 | + urllink = doc.nc.url + "/f/" + fileid |
159 | 221 | # update doctype file |
160 | 222 | if urllink != None and urllink != "": |
161 | 223 | doc.file_url = urllink |
162 | 224 | doc.file_name = doc.file_name + " NC/f/" + fileid |
163 | 225 | doc.save() |
164 | | - # delete local file |
165 | | - os.remove(local_fileobj) |
| 226 | + # delete local file |
| 227 | + os.remove(doc.nc.local_fileobj) |
166 | 228 | # tagging |
167 | | - if nc.enabletagging: nc.tagging(doc, fileid, relational=nc.relationaltagging) |
| 229 | + if doc.nc.enabletagging: doc.nc.tagging(doc, fileid, relational=doc.nc.relationaltagging) |
168 | 230 |
|
| 231 | +@frappe.whitelist() |
| 232 | +def nextcloud_before_delete(doc, method=None): |
| 233 | + doc.flags.ignore_nc = True |
| 234 | + nc = nextcloud_link() |
| 235 | + if not nc.isconnect: return |
| 236 | + doc.flags.ignore_file_validate = True |
| 237 | + doctype = doc.attached_to_doctype |
| 238 | + module = get_doctype_module(doctype) |
| 239 | + # Excluded module |
| 240 | + if module in nc.excludedmodules: return |
| 241 | + # File previously attached to another transaction |
| 242 | + if not doc.file_name or doc.file_name == None: return |
| 243 | + if not " NC/f/" in doc.file_name: return |
| 244 | + doc.flags.ignore_nc = False |
| 245 | + data = frappe.db.get_value("File", {"file_url": doc.file_url , "file_name": ["like", "%NC(%"]}, ["attached_to_doctype", "attached_to_name"], as_dict=True) |
| 246 | + if data: |
| 247 | + frappe.throw(_("The file can not be deleted while it is related to this transaction: {0} {1}").format(data.attached_to_doctype, data.attached_to_name)) |
169 | 248 |
|
170 | 249 | @frappe.whitelist() |
171 | 250 | def nextcloud_delete(doc, method=None): |
| 251 | + if doc.flags.ignore_nc: return |
172 | 252 | nc = nextcloud_link() |
173 | 253 | if not nc.isconnect: return |
174 | 254 | doctype = doc.attached_to_doctype |
|
0 commit comments