def users(request): """This view handles adding, deleting, and editing users.""" session = DBSession() context = get_context(request) message = '' if 'action' in request.POST: action = request.POST.getone('action') else: action = None if action == 'add': n_uid = request.POST.getone('uid') passwd = request.POST.getone('passwd') repeat = request.POST.getone('repeat') fullname = request.POST.getone('fullname') admin = 'admin' in request.POST if session.query(User).get(n_uid): message = 'User %s already exists.' % n_uid else: try: if passwd != repeat: raise ValueError("Passwords do not match.") user = User(n_uid, passwd, admin, fullname) session.add(user) message = "Added user %s" % user.uid #transaction.commit() except ValueError, msg: message = str(msg)
def files(request): session = DBSession() context = get_context(request) context['page_name'] = '*Files' context['page_subtitle'] = 'List of uploaded files' if 'data' in request.POST: field = request.POST.getone('data') name = field.filename if '\\' in name: name = name.split('\\')[-1] if '/'in name: name = name.split('/')[-1] file = File(name, context['user'].uid) session.add(file) session.flush() path = os.path.join(request.registry.settings['file_path'], str(file.id)) out_file = open(path,'w') field.file.seek(0) data = field.file.read(1<<16) total_size = len(data) while data and total_size < FILE_SIZE_LIMIT: out_file.write(data) data = field.file.read(1<<16) total_size += len(data) out_file.close() file.size = total_size session.add(file) session.flush() if total_size > FILE_SIZE_LIMIT: # We limit the size of files, just in case. os.remove(path) context['msg'].append('File is too large.') transaction.abort() else: context['msg'].append('File submitted successfully.') #transaction.commit() files = session.query(File) files_by_name = {} for file in files: file_list = files_by_name.get(file.name, []) file_list.append(file) files_by_name[file.name] = file_list for file_list in files_by_name.values(): file_list.sort(lambda a,b: cmp(a.changed, b.changed)) context['files'] = files_by_name return context
def delete(request): session = DBSession() context = get_context(request) id = request.matchdict['id'] page = request.matchdict['page'] post = session.query(Post).get(id) title = post.title hist = Post_History(context['uid'], post) session.delete(post) session.add(hist) context['msg'].append("Deleted Post: %s" % title) return context
def submit_content(request): """Handles actually submitting new post content.""" session = DBSession() context = get_context(request) uid = context['user'].uid mode = request.matched_route.name title = request.params.get('title') content = request.params.get('content') sticky = request.params.has_key('sticky') page = request.matchdict['page'] if mode == 'edit': # Handle the editing of existing posts. id = request.matchdict['id'] entry = session.query(Post).get(id) if not entry: raise NotFound("No such post to edit: %s, %d" % (page, id)) hist = entry.edit(uid, title, content, sticky) session.add(hist) msg = 'Post edited succesfully.' elif mode == 'add': entry = Post(uid, page, title, content, sticky) session.add(entry) msg = 'Post added successfully.' else: msg = "Invalid mode: %s" % mode session.flush() context['msg'].append(msg) msgs = [('msg', m) for m in context['msg']] return HTTPFound(location=request.route_url('post', id=entry.id, page=page, _query=msgs))