def submitcheckLogin(request): """ submit checklogin """ user = User.get(request.matchdict['userid']) flashError = "Sorry dude : wrong login or password" if not request.POST['password'].strip(): request.session.flash(flashError) return HTTPFound(location=request.route_path('login')) try: user = User.get(request.POST['login']) except couchdbkit.exceptions.ResourceNotFound: request.session.flash(flashError) return HTTPFound(location=request.route_path('login')) if bcrypt.hashpw(request.POST['password'].encode('utf-8'), user.password) != user.password: request.session.flash(flashError) return HTTPFound(location=request.route_path('login')) request.session.flash(u"Welcome %s, you have confirm your account" % user.name) user.checked = True user.save() headers = remember(request, user._id) request.session['username'] = user.name request.session['login'] = user._id request.session['is_admin'] = user.is_admin request.session.save() return HTTPFound(location=request.route_path('home'), headers=headers)
def setAdmin(): """ """ parser = argparse.ArgumentParser() parser.add_argument('--conf', help='wsgi conf file') parser.add_argument('--userid', help='user id') args = parser.parse_args() config = ConfigParser.RawConfigParser() config.read(args.conf) server = couchdbkit.Server(config.get('app:main', 'couchdb.url')) db = server.get_or_create_db(config.get('app:main','couchdb.db')) User.set_db(db) try: user = User.get(args.userid) user.is_admin = True user.save() print "%s is now admin" % args.userid except couchdbkit.exceptions.ResourceNotFound: print "%s not found" % args.userid
def submitlink(request): """ Submit a link. """ # TODO check if not already submit by user tags = [tag.strip() for tag in request.POST['tags'].split(',')] link = Link() link.url = request.POST['link'].strip() link.title = request.POST['title'].strip() link.created = datetime.datetime.now() link.comment = request.POST['comment'].strip() link.userID = request.session['login'] link.username = request.session['username'] link.private = False # TODO link.tags = tags if 'private' in request.POST: link.private = True link.save() if not link.private: user = User.get(request.session['login']) user.links[link._id] = link.created user.save() request.session.flash("link added !") return HTTPFound(location=request.route_path('home'))
def submitLogin(request): """ Action on login page. """ flashError = "Sorry dude : wrong login or password" if not request.POST['password'].strip(): request.session.flash(flashError) return HTTPFound(location=request.route_path('login')) try: user = User.get(request.POST['login']) except couchdbkit.exceptions.ResourceNotFound: request.session.flash(flashError) return HTTPFound(location=request.route_path('login')) if bcrypt.hashpw(request.POST['password'].encode('utf-8'), user.password) != user.password: request.session.flash(flashError) return HTTPFound(location=request.route_path('login')) if not user.checked: request.session.flash(u"please confirm you mail before") return HTTPFound(location=request.route_path('login')) request.session.flash(u"welcome %s, you are logged" % user.name) headers = remember(request, user._id) request.session['username'] = user.name request.session['login'] = user._id request.session['is_admin'] = user.is_admin request.session.save() return HTTPFound(location=request.route_path('home'), headers=headers)
def contacts(request): """ """ limit, page = limitAndPage(request) users = User.view('viewFollowers/all', limit=limit, descending=True, skip=limit*page, key=request.session['login']) return {"users": users, 'limit': limit, 'page': page}
def admin_list(request): """ """ limit, page = limitAndPage(request) skip = limit*page users = User.view('user/all', skip=skip, limit=limit, descending=True) return {'users': users, 'page': page}
def delAndPurge(): """ """ parser = argparse.ArgumentParser() parser.add_argument('--conf', help='wsgi conf file') parser.add_argument('--userid', help='user id') args = parser.parse_args() config = ConfigParser.RawConfigParser() config.read(args.conf) server = couchdbkit.Server(config.get('app:main', 'couchdb.url')) db = server.get_or_create_db(config.get('app:main','couchdb.db')) User.set_db(db) Link.set_db(db) push('couchdb/_design/purge', db) try: user = User.get(args.userid) user.delete() print "%s is now deleted" % args.userid except couchdbkit.exceptions.ResourceNotFound: print "%s not found" % args.userid links = Link.view('purge/all', key=args.userid, include_docs=True) print "%d links found" % len(links) for link in links: link.delete() print "job done"
def unfollow(request): """ Unfollow a contact. """ try: user = User.get(request.matchdict['userid'].strip()) except couchdbkit.exceptions.ResourceNotFound: request.session.flash("Sorry, we don't find your buddy.") return HTTPFound(location=request.route_path('contacts')) return {"user": user}
def avatar(request): """ """ try: user = User.get(request.matchdict['userid'].strip()) except couchdbkit.exceptions.ResourceNotFound: raise HTTPNotFound() response = Response(content_type='image/jpeg', body=user.fetch_attachment('avatar')) return response
def checkLogin(request): """ Validate inscription """ user = User.get(request.matchdict['userid']) if user.checked: request.session.flash(u"Already confirmed!") return HTTPFound(location=request.route_path('home')) if user.random == int(request.matchdict['randomid']): return {'user' : user} return HTTPFound(location=request.route_path('home'))
def userrss(request): """ """ try: user = User.get(request.matchdict['userid']) except couchdbkit.exceptions.ResourceNotFound: return HTTPNotFound() links = Link.view( 'user_link/all', limit=10, descending=True, key=user._id) return {'links': links, 'user': user}
def coherence(): """ """ parser = argparse.ArgumentParser() parser.add_argument('--conf', help='wsgi conf file') parser.add_argument('--userid', help='user id') args = parser.parse_args() config = ConfigParser.RawConfigParser() config.read(args.conf) server = couchdbkit.Server(config.get('app:main', 'couchdb.url')) db = server.get_or_create_db(config.get('app:main','couchdb.db')) User.set_db(db) Link.set_db(db) users = User.view('user/all', descending=True) for user in users: print "user %s" % user._id links = Link.view('purge/all', key=user._id, include_docs=True) for link in links: print "checking %s" % link._id if link._id not in user.links: print "adding %s" % link._id user.links[link._id] = link.created user.save()
def admin_user(request): """ """ user = User.get(request.matchdict['user']) # TODO @cyp to @Mika64 need to restrain view_config to POST ? if request.method == 'POST': user.name = request.POST.get('name') user.description = request.POST.get('description') if request.POST.get('admin') == 'on': user.is_admin = True else: user.is_admin = False user.save() return {'user': user}
def rmlink(request): """ Delete a link. """ link = Link.get(request.matchdict['link']) if not link.private: user = User.get(request.session['login']) del(user.links[request.matchdict['link']]) user.save() link.delete() return HTTPFound(location=request.route_path('mylinks'))
def user(request): """ """ try: user = User.get(request.matchdict['userid']) except couchdbkit.exceptions.ResourceNotFound: return HTTPNotFound() limit, page = limitAndPage(request) links = Link.view('user_link/all', limit=limit, skip=limit*page, descending=True, startkey=[user._id, {}], endkey=[user._id], include_docs=True) return {'links': links, 'user': user, 'limit': limit, 'page': page}
def confirmUnfollow(request): """ Confirm page to unfollow a contact. """ try: user = User.get(request.matchdict['userid'].strip()) except couchdbkit.exceptions.ResourceNotFound: request.session.flash("Sorry, we don't find your buddy.") return HTTPFound(location=request.route_path('contacts')) user.followers = [follower for follower in user.followers if follower != request.session['login']] user.save() request.session.flash("You don't follower %s anymore" % user.name) return HTTPFound(location=request.route_path('contacts'))
def profile(request): """ View profile page. """ user = User.get(request.session['login']) if request.method =='POST': flashError = "Sorry dude : wrong password" if not request.POST['initPassword'].strip(): request.session.flash('No password provided') return {'user':user} elif not bcrypt.hashpw(request.POST['initPassword'].encode('utf-8'), user.password) == user.password: request.session.flash(flashError) return {'user':user} if request.POST['submitDelete']: mailer = Mailer() message = Message(subject="Account deleted", sender=settings['mail_from'], recipients=[user.mail], body="Your account have been deleted") mailer.send_immediately(message, fail_silently=False) user.delete() request.session.delete() return HTTPFound(location=request.route_path('home')) if request.POST['newPassword'].strip(): if request.POST['newPassword'] == request.POST['confirmPassword']: password = bcrypt.hashpw(request.POST['newPassword'].encode('utf-8'), bcrypt.gensalt()) user.password = password else: request.session.flash(u"Password not confirm") return {'user' : user} user.name = request.POST['name'] user.description = request.POST['description'] user.mail = request.POST['email'] user.save() request.session.flash(u"Modification saved !") return {'user':user}
def submitContact(request): """ """ if not(request.POST['contactid'].strip()): request.session.flash("contact id is required") HTTPFound(location=request.route_path('contacts')) try: user = User.get(request.POST['contactid'].strip()) except couchdbkit.exceptions.ResourceNotFound: request.session.flash("Sorry, we don't find your buddy.") return HTTPFound(location=request.route_path('contacts')) if request.session['login'] in user.followers: request.session.flash("You already follow %s." % user.name) return HTTPFound(location=request.route_path('contacts')) user.followers.append(request.session['login']) user.save() request.session.flash("You follow %s." % user.name) return HTTPFound(location=request.route_path('contacts'))
import couchdbkit from couchdbkit.designer import push from PIL import Image from wsgiwars.models.user import User from wsgiwars.models.link import Link from wsgiwars.resources import linkAjax settings = get_current_registry().settings server = couchdbkit.Server(settings['couchdb.url']) db = server.get_or_create_db(settings['couchdb.db']) User.set_db(db) Link.set_db(db) for view in ['couchdb/_design/user', 'couchdb/_design/public', 'couchdb/_design/user_link', 'couchdb/_design/my_link', 'couchdb/_design/viewTag', 'couchdb/_design/viewFollowers', 'couchdb/_design/contacts_links', 'couchdb/_design/tags', ]: push(view, db) avatarSize = 128,128
def delete(request): """ """ user = User.get(request.matchdict['user']) user.delete() return HTTPFound(location=request.route_url('admin_list', page="0"))
def submitSignup(request): """ Action on submit page. """ try: User.get(request.POST['login']) except couchdbkit.exceptions.ResourceNotFound: pass else: request.session.flash(u"Username already exist") return HTTPFound(location=request.route_path('signup')) if not request.POST['password'].strip(): request.session.flash(u"You realy need a password") return HTTPFound(location=request.route_path('signup')) if not len(request.POST['password'].strip()) >= 8: request.session.flash(u"Password must have at least 8 characters") return HTTPFound(location=request.route_path('signup')) if request.POST['password'] == request.POST['confirmPassword']: password = bcrypt.hashpw(request.POST['password'].encode('utf-8'), bcrypt.gensalt()) user = User(password=password, name=request.POST['name'], description=request.POST['description'], mail=request.POST['email'], random=random.randint(1,1000000000), checked = False ) user._id = request.POST['login'] user.save() if hasattr(request.POST['avatar'], 'filename'): tmph, originImage = tempfile.mkstemp(dir=settings['tmp'], \ suffix="original") os.close(tmph) tmph, thumbImage = tempfile.mkstemp(dir=settings['tmp'], \ suffix="thumb") os.close(tmph) with open(originImage, 'wb') as tmp: tmp.write(request.POST['avatar'].file.read()) fullSize = Image.open(originImage) fullSize.thumbnail(avatarSize, Image.ANTIALIAS) fullSize.save(thumbImage , "JPEG") with open(thumbImage, 'rb') as thumb: user.put_attachment(thumb, 'avatar') os.remove(originImage) os.remove(thumbImage) confirm_link = request.route_url('checkLogin', userid = user._id, randomid = user.random) mailer = Mailer() message = Message(subject="Your subsription !", sender=settings['mail_from'], recipients=[request.POST['email']], body="Confirm the link\n\n%s" % confirm_link) # TODO add link mailer.send_immediately(message, fail_silently=False) return {'name': request.POST['name']} else: return HTTPFound(location=request.route_path('signup'))