Example #1
0
def signup_authentication(response):
    username = response.get_field("username")
    password = response.get_field("password")
    c_password = response.get_field("confirm_password")
    fname = response.get_field("fname")
    lname = response.get_field("lname")
    email = response.get_field("email")
    user = User.find(username)
    context = {"error": None}
    if user:
        context["error"] = "Username taken"
    elif not username or not password or not email:
        context["error"] = "Username, password and email are required"
    elif password != c_password:
        context["error"] = "Passwords do not match"
    elif not re.match(r"^[0-9a-zA-Z_\.]+$", username):
        context["error"] = "Invalid username, please use only letters, numbers, underscores and periods"
    elif User.get_email(email) == email:
        context["error"] = "Email already taken"
    elif len(password) < 8:
        context["error"] = "Password must be at least 8 characters"
    else:
        User(username, password, None, email, fname, lname).create()
        response.set_secure_cookie("username", username)
        response.redirect("/")
        return None
    render_page("signup.html", response, context)
Example #2
0
    def create_task(self, user_id, body):
        user = User.find_by_id(user_id)
        if user is None:
            raise InvalidUsage("No such user", 404)

        if 'content' not in body:
            raise InvalidUsage("Task must have content")

        newTask = Task(content=body['content'])
        User.add_task(user, newTask)

        return newTask.serialize()
Example #3
0
def profile_handler(response, username=None):
    if username is None:
        user_object = User.find(get_login(response))
    else:
        user_object = User.find(username)
        if user_object is None:
            error_handler(response)
            return
    context = {}
    user_locations = Location.find_user_locations(user_object.id)
    context["results"] = user_locations
    context["user_object"] = user_object
    render_page("account.html", response, context)
Example #4
0
def location_editor(response, id):
    # file_input = response.get_file('picture')
    # filename_hash = hashlib.sha1(file_input[2]).hexdigest()

    # file_output = open('./static/place-images/{}'.format(filename_hash), 'wb')
    # file_output.write(file_input[2])
    # file_output.close()

    context = {"error": None}
    location = Location.find_id(id)
    orig_name = location.name
    context["location"] = location
    if location is None:
        context["error"] = "Place does not exist"
        render_page("edit_location.html", response, context)
        return
    name = response.get_field("name")
    if orig_name != name:
        if Location.find_name(name):
            context["error"] = "Place already exists"
            render_page("edit_location.html", response, context)
            return None

    description = response.get_field("description")
    address = response.get_field("address")
    username = get_login(response)
    user = User.find(username)
    Location.change_location(id, name, description, location.picture, address, location.latitude, location.longitude)
    response.redirect("/location/" + id)
Example #5
0
def createUser(userId, name, ses=Session()):
    dbUser = getUserById(userId, ses)
    if dbUser is not None:
        return

    user = User(message_count=0, karma=0, name=name, id=userId)
    ses.add(user)
    ses.commit()
Example #6
0
 def post_context(self):
     username = self.get_field('username')
     password = self.get_field('password')
     user = User.find(username)
     if user and username == user.username and password == user.password:
         self.set_secure_cookie('username', username)
         self.redirect('/')
     else:
         self.write('Incorrect username or password')
Example #7
0
    def execute(self):

        try:
            user = User.get(id=self.user_id)
            user.nickname = self.nickname if self.nickname else user.nickname
            user.head_img_url = self.head_img_url if self.head_img_url else user.head_img_url
            user.save()
        except Exception, e:
            return self.mk_response(False, str(e))
    def execute(self):

        try:
            user = User.get(openid=self.openid)
        except DoesNotExist:
            user = User(openid=self.openid, )
            user.save()
            return self.mk_response(UserInfo(user_id=user.id))
        except:
            return self.mk_response(None)

        user_info = UserInfo(user_id=user.id,
                             openid=user.openid,
                             nickname=user.nickname,
                             head_img_url=user.head_img_url,
                             create_time=utils.datetime2timestamp(
                                 user.create_time))

        return self.mk_response(user_info)
Example #9
0
def render_page(filename, response, context):
    context["logged_in"] = get_login(response)
    if context["logged_in"]:
        user = User.find(context["logged_in"])
        context["user"] = user
    if "query" not in context:
        context["query"] = None
    if "tags" not in context:
        context["tags"] = None
    html = render(filename, context)
    response.write(html)
Example #10
0
def users_new():
    print(request.form)
    if 'email' not in request.form.keys():
        return error('Missing email')
    session = Session()
    session.add(User(email=request.form['email']))
    try:
        session.commit()
    except sqlalchemy.exc.IntegrityError as why:
        return error('Email already exists')
    return ok('Registered user')
Example #11
0
def login_authentication(response):
    username = response.get_field("username")
    password = response.get_field("password")
    user = User.find(username)
    context = {"login_error": None}
    if user and username == user.username and password == user.password:
        response.set_secure_cookie("username", username)
        response.redirect("/")
    else:
        context["login_error"] = "Incorrect username or password"
        render_page("login.html", response, context)
Example #12
0
def register():
    form = RegistrationForm()
    logging.info('form.validate_on_submit():%s' %form.validate_on_submit())
    if form.validate_on_submit():
        if User.query.filter_by(username=form.username.data).first() is None:
            user = User(username=form.username.data,
                        password=form.password.data)
            logging.info('username:%s' %user.username)
            db.db.session.add(user)
            db.db.session.commit()
            token = user.generate_confirmation_token()
            user.confirm(token)
            #send_email(user.email, 'Confirm Your Account',
            #           'auth/email/confirm', user=user, token=token)
            #flash('A confirmation email has been sent to you by email.')
            return redirect(url_for('login'))
        else:
            logging.info(u'注册IP:%s  账号 %s 已被注册,请更换用户名。' %(ip, form.username.data))
            flash(u'账号 %s 已被注册,请更换用户名。' %form.username.data)
            return redirect(url_for('register'))
    return render_template('register.html', form=form, ip = ip)
Example #13
0
def incUserMsgCount(user):
    ses = Session()
    dbUser = getUserById(str(user.id), ses)

    if dbUser is None:
        dbUser = User(message_count=1,
                      karma=0,
                      name=str(user),
                      id=str(user.id))
        ses.add(dbUser)
    else:
        dbUser.message_count += 1

    ses.commit()
Example #14
0
def location_handler(response, id):
    logged_in = get_login(response)
    context = {}
    user_object = User.find(get_login(response))
    location = Location.find_id(id)
    loc_tags = Tag.find_from_place(location.id)
    if logged_in:
        stars = location.get_user_rating(user_object.id)
        context["user_rating"] = stars
    if location:
        context["location"] = location
        context["loc_tags"] = loc_tags
        context["comments"] = Comment.find_place(location.id)
        render_page("location.html", response, context)
    else:
        error_handler(response)
    def execute(self):

        try:
            users = User.select().where(User.id.in_(self.ids))
        except:
            pass

        user_info_list = []
        for user in users:
            user_info_list.append(
                UserInfo(user_id=user.id,
                         openid=user.openid,
                         nickname=user.nickname,
                         head_img_url=user.head_img_url,
                         create_time=utils.datetime2timestamp(
                             user.create_time)))

        return self.mk_response(user_info_list)
Example #16
0
def location_creator(response):
    file_input = response.get_file("picture")
    filename_hash = hashlib.sha1(file_input[2]).hexdigest()

    file_output = open("./static/place-images/{}".format(filename_hash), "wb")
    file_output.write(file_input[2])
    file_output.close()

    context = {"error": None}

    name = response.get_field("name")
    description = response.get_field("description")
    address = response.get_field("address")
    username = get_login(response)
    user = User.find(username)

    try:
        lat = float(response.get_field("lat"))
        long = float(response.get_field("long"))
    except ValueError:
        context["error"] = "Invalid latitude or longitude"
        render_page("create_location.html", response, context)
        return
    if Location.find_name(name):
        context["error"] = "Place already exists"
        render_page("create_location.html", response, context)
    else:
        Location(name, description, filename_hash, user.id, address, long, lat).create()
        response.redirect("/location/{}".format(Location.find_name(name).id))

        tags = response.get_field("tags").split(",")
        if tags == [""]:
            tags = []
        for tag in tags:
            Tag(tag, Location.find_name(name).id).create()
    return
Example #17
0
 def list_users(self):
     return [u.serialize() for u in User.find_all()]
Example #18
0
def rating(response, location_id):
    if get_login(response):
        user_object = User.find(get_login(response))
        Rating(location_id, response.get_field("stars"), user_object.id).create()
Example #19
0
def comment_post(response, location_id):
    user_object = User.find(get_login(response))
    comment = response.get_field("comment")
    Comment(user_object.id, comment, location_id).create()
    response.redirect("/location/" + location_id)
Example #20
0
    def list_user_tasks(self, user_id: int):
        user = User.find_by_id(user_id)
        if user is None:
            raise InvalidUsage("No such user", 404)

        return [t.serialize() for t in user.tasks]