def set_permissions(request,share,json=None): from smtplib import SMTPException emailed=[] created=[] failed=[] # if not request.user.has_perm('admin',share): # return json_response({'status':'error','error':'You do not have permission to write to this share.'}) if json.has_key('groups'): for group, permissions in json['groups'].iteritems(): g = Group.objects.get(id=group) current_perms = get_perms(g,share) removed_perms = list(set(current_perms) - set(permissions)) added_perms = list(set(permissions) - set(current_perms)) for u in g.user_set.all(): if len(share.get_user_permissions(u,user_specific=True)) == 0 and len(added_perms) > 0 and json['email']: email_users([u],'share/share_subject.txt','share/share_email_body.txt',{'user':u,'share':share,'sharer':request.user,'site_url':SITE_URL}) emailed.append(u.username) for perm in removed_perms: remove_perm(perm,g,share) for perm in added_perms: assign_perm(perm,g,share) if json.has_key('users'): for username, permissions in json['users'].iteritems(): try: u = User.objects.get(username=username) if len(share.get_user_permissions(u,user_specific=True)) == 0 and json['email']: try: email_users([u],'share/share_subject.txt','share/share_email_body.txt',{'user':u,'share':share,'sharer':request.user,'site_url':SITE_URL}) emailed.append(username) except: failed.append(username) except: if len(permissions) > 0: password = User.objects.make_random_password() u = User(username=username,email=username) u.set_password(password) u.save() try: email_users([u],'share/share_subject.txt','share/share_new_email_body.txt',{'user':u,'password':password,'share':share,'sharer':request.user,'site_url':SITE_URL}) created.append(username) except: failed.append(username) u.delete() current_perms = share.get_user_permissions(u,user_specific=True) print 'CURRENT' print current_perms print 'PERMISSIONS' print permissions removed_perms = list(set(current_perms) - set(permissions)) added_perms = list(set(permissions) - set(current_perms)) print 'ADDING: ' print added_perms print 'REMOVING: ' print removed_perms for perm in removed_perms: if u.username not in failed: remove_perm(perm,u,share) for perm in added_perms: if u.username not in failed: assign_perm(perm,u,share) data = share.get_permissions(user_specific=True) data['messages']=[] if len(emailed) > 0: data['messages'].append({'type':'info','content':'%s has/have been emailed'%', '.join(emailed)}) if len(created) > 0: data['messages'].append({'type':'info','content':'Accounts has/have been created and emails have been sent to the following email addresses: %s'%', '.join(created)}) if len(failed) > 0: data['messages'].append({'type':'info','content':'Delivery has failed to the following addresses: %s'%', '.join(failed)}) data['json']=json ShareFTPUser.update_share_ftp_users(share) return json_response(data)
def update_share(request,share,json=None): share.secure = json['secure'] share.save() ShareFTPUser.update_share_ftp_users(share) return json_response({'status':'okay'})