def add_files(root_folder, user, path): ret = [] for f in os.listdir(path): p = os.path.join(path, f) if os.path.isdir(p): ret.extend(add_files(root_folder, user, p)) continue pk, fname = split_file_name(f) if pk is False: try: file = ZornaFile( description='', owner=user, modifier=user, folder=root_folder) file.save() dest = os.path.join(path, u"%s,%s" % (file.pk, f)) os.rename(p, dest) ret.append(p) except: continue return ret
def fm_multiple_upload_view(request): pathfile = request.REQUEST.get('dir', None) ret = {} if pathfile: ppath = urllib.unquote(pathfile.rstrip('/')) path_dest = clean_path(ppath) if path_dest != ppath: ret['message'] = gettext(u"Incorrect path") ret['status'] = 'error' else: buser, bmanager = get_user_access_to_path(request.user, path_dest) folder = get_shared_folder(path_dest) if bmanager is False: ret['message'] = gettext(u"Access denied") ret['status'] = 'error' else: cdir_components = get_path_components(path_dest) form = ZornaFileUploadForm(request.POST, request.FILES) if request.method == 'POST': root_path = get_upload_library() if form.is_valid(): path = u"%s/%s" % ( root_path, urllib.unquote(path_dest)) uploaded_file = request.FILES['file'] chunk = request.REQUEST.get('chunk', '0') chunks = request.REQUEST.get('chunks', '0') name = uploaded_file.name temp_file = '%s/%s.part' % (path, name) with open(temp_file, ('wb' if chunk == '0' else 'ab')) as f: for content in uploaded_file.chunks(): f.write(content) if int(chunk) + 1 >= int(chunks): s = os.path.splitext(name) file = ZornaFile( owner=request.user, modifier=request.user, description='', folder=path_dest.split('/')[0]) file.save() destination = u"%s/%s,%s" % (path, file.pk, name) shutil.move(temp_file, destination) if request.is_ajax(): response = HttpResponse( '{"jsonrpc" : "2.0", "result" : null, "id" : "id"}') response[ 'Expires'] = 'Mon, 1 Jan 2000 01:00:00 GMT' response[ 'Cache-Control'] = 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0' response['Pragma'] = 'no-cache' return response else: ret['status'] = 'success' else: ret['status'] = 'success' t = loader.get_template( 'fileman/upload_multiple_documents.html') c = RequestContext(request, { 'form': form, 'cdir_components': cdir_components, 'folder_dest': pathfile, 'folder_content_url': get_url_folder_content(pathfile), 'folder': folder, 'language': settings.LANGUAGE_CODE[0:2]}) ret['data'] = t.render(c) else: ret['message'] = gettext(u"Invalid file path") ret['status'] = 'error' json_data = simplejson.dumps(ret) return HttpResponse(json_data)
def fm_upload_view(request): pathfile = request.REQUEST.get('dir', None) ret = {} if pathfile: ppath = urllib.unquote(pathfile.rstrip('/')) path_dest = clean_path(ppath) if path_dest != ppath: ret['message'] = gettext(u"Incorrect path") ret['status'] = 'error' else: buser, bmanager = get_user_access_to_path(request.user, path_dest) folder = get_shared_folder(path_dest) if bmanager is False: ret['message'] = gettext(u"Access denied") ret['status'] = 'error' else: cdir_components = get_path_components(path_dest) if request.method == 'POST': root_path = get_upload_library() fa_set = formset_factory(ZornaFileAddForm, extra=2) form_set = fa_set(request.POST, request.FILES) if form_set.is_valid(): upload_files = {} ifiles = [] for i in range(0, form_set.total_form_count()): form = form_set.forms[i] try: f = request.FILES['form-' + str(i) + '-file'] s = os.path.splitext(f.name) fname = u"%s%s" % (slugify(s[0]), s[1]) upload_files[fname] = {'pk': '', 'file': f, 'description': form.cleaned_data['description'], 'tags': form.cleaned_data['tags']} except: continue path = u"%s/%s" % ( root_path, urllib.unquote(path_dest)) bupload = False for f, info in upload_files.iteritems(): file = ZornaFile(owner=request.user, modifier=request.user, description=info[ 'description'], tags=info['tags'], folder=path_dest.split('/')[0]) file.save() destination = open(u"%s/%s,%s" % ( path, file.pk, f), 'wb+') bupload = True for chunk in info['file'].chunks(): destination.write(chunk) destination.close() ifiles.append({'name': f, 'description': info[ 'description']}) if bupload: ret['message'] = gettext( u"Files uploaded successfully") # notify users bnotify = request.POST.get('notify_users', 0) if folder and folder.email_notification and (folder.email_notification == 1 or bnotify): notify_users(request, folder, ifiles, True) else: ret['message'] = gettext(u"No file uploaded") ret['status'] = 'success' json_data = simplejson.dumps(ret) return HttpResponse('<textarea>' + json_data + '</textarea>') else: ret['message'] = gettext(u"Invalid form") ret['status'] = 'error' t = loader.get_template( 'fileman/upload_documents.html') c = RequestContext(request, {'form_set': form_set, 'cdir_components': cdir_components, 'folder_dest': pathfile, 'folder_content_url': get_url_folder_content(pathfile)}) ret['data'] = t.render(c) json_data = simplejson.dumps(ret) return HttpResponse('<textarea>' + json_data + '</textarea>') else: fa_set = formset_factory(ZornaFileAddForm, extra=2) form_set = fa_set() ret['status'] = 'success' t = loader.get_template('fileman/upload_documents.html') c = RequestContext(request, {'form_set': form_set, 'cdir_components': cdir_components, 'folder_dest': pathfile, 'folder_content_url': get_url_folder_content(pathfile), 'folder': folder, 'manager': bmanager, }) ret['data'] = t.render(c) else: ret['message'] = gettext(u"Invalid file path") ret['status'] = 'error' json_data = simplejson.dumps(ret) return HttpResponse(json_data)
def save(self, request): message = self.cleaned_data['message'] send_to = self.cleaned_data['send_to'] upload_to = [] calendar_owners = [] dest = [] ao = get_allowed_objects(request.user, Community, ['manage', 'member']) if send_to: send_to = send_to.split(',') for item in send_to: item = item.split('-') if item[0] == 'u': # user user = User.objects.get(pk=item[1]) # if user recipient member of any current user communities ao_member_user = get_allowed_objects( user, Community, ['member', 'manage']) inter = [k for k in ao if k in ao_member_user] if len(inter): dest.append(user) calendar_owners.append(user) upload_to.append(u"U%s" % user.pk) else: community = Community.objects.get(pk=item[1]) if community.pk in ao: dest.append(community) calendar_owners.append(community) upload_to.append(u"C%s" % community.pk) users_emails = [] if len(dest): m = MessageCommunity(message=message) m.owner = m.modifier = request.user m.save() for k in dest: if isinstance(k, User): m.users.add(k) users_emails.append(k.email) else: m.communities.add(k) if k.email_notification: users = list(chain(get_acl_by_object( k, 'member'), get_acl_by_object(k, 'manage'))) users_emails.extend([u.email for u in users]) else: return None files = request.FILES.getlist("attachments") if len(upload_to) and len(files): try: path_library = get_upload_library() path = os.path.join(get_upload_communities(), "%s" % m.pk) if not os.path.isdir(path): os.makedirs(path) for f in request.FILES.getlist("attachments"): s = os.path.splitext(f.name) fname = slugify(s[0]) fext = s[1] destination = open(u"%s/%s" % ( path, u"%s%s" % (fname, fext)), 'wb+') for chunk in f.chunks(): destination.write(chunk) destination.close() for d in upload_to: destpath = os.path.join(path_library, "%s" % d) if not os.path.isdir(destpath): os.makedirs(destpath) try: libfile = ZornaFile( owner=request.user, modifier=request.user) libfile.save() fsrc = u"%s/%s/%s,%s%s" % ( path_library, d, str(libfile.pk), fname, fext) shutil.copy2(u"%s/%s" % ( path, u"%s%s" % (fname, fext)), fsrc) except Exception as e: print(e) except Exception as e: pass # send email notification if len(users_emails): users_emails = list(set(users_emails)) if users_emails: email = ZornaEmail() url = request.build_absolute_uri(reverse( 'communities_home_page', args=[])) + '?all_msg=message&message_id=%s' % m.pk ec = {"message": m, 'url': url, 'user': request.user} body_text = render_to_string( 'communities/email_notification_text.html', ec) body_html = render_to_string( 'communities/email_notification_html.html', ec) subject = _( u'A new message has been posted in communities') step = getattr(settings, "ZORNA_MAIL_MAXPERPACKET", 25) for n in range(0, len(users_emails) / step + 1): email.append(subject, body_text, body_html, settings.DEFAULT_FROM_EMAIL, bcc=users_emails[ n * step:(n + 1) * step]) email.send() return m return None