def display_story_tree(resp, story_id):
	user = resp.get_secure_cookie('username')
	story = Story.find('id', story_id)[0]
	author = User.find('id', story.author_id)[0]
	if user is not None:
		user = User.find('username', str(user, 'utf-8'))[0]
	else:
		user = None
	try:
		resp.write(template.render_file('view_story_tree.html', {'node': create_tree(story), 'story': story, 'author': author, 'user': user}))
	except Exception as e:
		story.delete()
Пример #2
0
def view_story(response, id):
    user = response.get_secure_cookie('username')
    story = Story.find('id', id)
    author = User.find('id', story[0].author_id)

    if user is not None:
        context = {'current_user':User.find('username', str(user, 'utf-8'))[0], 'story':story[0], 'author':author[0]}
    else:
        context = {'current_user':None, 'story':story[0], 'author':author[0]}

    html = template.render_file('templates/viewingstory.html', context)
    response.write(html)
Пример #3
0
def profiles(response):
	users = User.find('all')

	if response.get_secure_cookie('username') is not None:
		current_user = User.find('username', str(response.get_secure_cookie('username'), 'utf-8'))[0]
	else:
		current_user = None

	context = {
		'users':users,
		'current_user':current_user
	}

	html = template.render_file('templates/profilelist.html', context)
	response.write(html)
Пример #4
0
def user(response, username):
	user = User.get('username', username)

	if response.get_secure_cookie('username') is not None:
		current_user = User.get('username', str(response.get_secure_cookie('username'), 'utf-8'))
	else:
		current_user = None

	context = {
	  	'user':user,
	    'current_user': current_user,
	    'requested_user': username
	}

	html = template.render_file('templates/profile.html', context)
	response.write(html)
def add_to_story_tree(response, story_id):
	try:
		story_id = int(story_id)

		username = response.get_secure_cookie('username')
		user = User.find('username', str(username,'utf-8'))[0]
		addition_to_story = response.get_argument('paragraph')
		story = Story.find('id', story_id)[0]

		if False:#not Rules.check(addition_to_story, story.id):
			#return to story view without updating.... TODO: show error
			response.write('DR')
			response.redirect('/view_story/{}'.format(story_id))
			return

		response.write('aoeuhotnaeuhaoe ')
		parent = Paragraph.find('id',int(response.get_argument('parentId')))[0]
		
		if parent:
			parent.chain_paragraph(user, addition_to_story).save()

		response.redirect('/view_story/{}'.format(story_id))
	
	except Exception as e:
		response.write(str(e))
		raise e
Пример #6
0
def new_story(response):
    user = response.get_secure_cookie('username')
    if user is not None:
        context = {'current_user':User.find('username', str(user, 'utf-8'))[0]}
    else:
        context = {'current_user':None}

    html = template.render_file('templates/newstory.html', context)
    response.write(html)
def add_comment(response, story_id):
    username = response.get_secure_cookie('username')
    user = User.find('username', str(username, 'utf-8'))
    if not user:
        raise Exception("Expected user account when adding to story")
    user = user[0]
    story = Story.find('id', story_id)[0]
    story.add_comment(user, response.get_argument('commentbox')).save()
    response.redirect('/view_story/{}'.format(story_id))
Пример #8
0
def check_username(response, username):
        import json
        json_response = {
                "status":0, "username_available":True 
                }
        
        if User.find('username', username):
                json_response["username_available"] = False

        response.write(json.dumps(json_response))
Пример #9
0
def authenticated(response):
    username = response.get_secure_cookie('username')
    context = {}
    if username is not None:
        context.update({'current_user': User.find('username', str(username, 'utf-8'))[0]})
    else:
        context['current_user'] = None
    if context['current_user'] and context['current_user'].admin_level >= 1:
         return (True, context)
    else:
         return (False, context)
def register(response):
    if response.get_secure_cookie('username') is not None:
        current_user = User.find('username', str(response.get_secure_cookie('username'), 'utf-8'))[0]
    else:
        current_user = None

    context = {
        'current_user':current_user
    }

    html = template.render_file('templates/registration.html', context)
    response.write(html)
Пример #11
0
def index(response):
    username = response.get_secure_cookie('username')
    if username is not None:
        context = {'current_user': User.find('username', str(username, 'utf-8'))[0]}
    else:
        context = {'current_user': None}

    context['error'] = (response.get_secure_cookie('error_msg') or b'').decode()
    response.clear_cookie('error_msg')

    html = template.render_file('templates/index.html', context)
    response.write(html)
Пример #12
0
def view_story_list(response):
    user = response.get_secure_cookie('username')

    stories = Story.find('all', '')

    if user is not None:
        context = {'current_user':User.find('username', str(user, 'utf-8'))[0], 'stories':stories}
    else:
        context = {'current_user':None, 'stories':stories}

    html = template.render_file('templates/storylist.html', context)
    response.write(html)
Пример #13
0
def process_new_story(response):
    username = response.get_secure_cookie('username')
    user = User.find('username', str(username, 'utf-8'))[0]

    title = response.get_argument('title')
    story_text = response.get_argument('story')
    rule = response.get_argument('rule')
    comment = response.get_argument('comment')

    story = Story.create(user, title, comment)
    story.save()
    story.add_paragraph(user, story_text).save()

    response.redirect('/view_story/{}'.format(story.id))
Пример #14
0
 def get(response, user_id):
     output = {'success': None, 'msg': None}
     is_admin, _ = authenticated(response)
     if is_admin:
         user = User.find('id', user_id)
         if user:
             output['success'] = True
             user[0].delete()
         else:
             output['success'] = False
     else:
         output['success'] = False
         output['msg'] = 'not_administrator'            
     response.write(output)
 def get(response, user_id):
     output = {'success': None, 'msg': None}
     is_admin, _ = authenticated(response)
     if is_admin:
         user = User.find('id', user_id)
         if user:
             logging.info('Successfully deleted user with id {}'.format(user_id))
             output['success'] = True
             user[0].delete()
         else:
             logging.info('Did not successfully deleted user with id {}'.format(user_id))
             output['success'] = False
     else:
         logging.info('Non-administrator attempted to delete user {}'.format(user_id))
         output['success'] = False
         output['msg'] = 'not_administrator'
     response.write(json.dumps(output))
Пример #16
0
def search_results(response):
    context = defaultdict()

    user = response.get_secure_cookie('username')
    stories = Story.find('all', '')

    try:
        context.update({
            'current_user': User.find('username', str(user, 'utf-8'))[0]})
    except TypeError:
        context.update({
            'current_user': None})

    cursor = conn.cursor()

    if response.get_arguments('storyquery') != []:
        query = response.get_argument('storyquery')
        results = search(cursor, conn, query)
        stories = []
        for result in results:
            stories.append(Story.find('id', int(result[0]))[0])

        context['stories'] = stories
        context['query'] = query
        context['story'] = ''

        assert 'query' in context
        print("QUERY", repr(query))
        html = template.render_file('templates/storylist.html', context)
        # html = template.render_file('templates/minimal.html', context)
        response.write(html)

    elif response.get_arguments('sort') != []:
        # filter logic here
        pass
    else:
        context['stories'] = stories
        context['story'] = None
        html = template.render_file('templates/storylist.html', context)
        response.write(html)
def process_register(response):
    # Username should be 4 or more characters of only numbers and letters
    username_pat = re.compile(r'[a-zA-Z0-9]{4}[a-zA-Z0-9]*')
    
    firstname = response.get_argument('fname', default='')
    lastname = response.get_argument('lname', default='')
    username = response.get_argument('username')
    password = response.get_argument('password')
    repeat_password = response.get_argument('rpassword')
    email = response.get_argument('email')
    birthdate = response.get_argument('bday', default='')
    location = response.get_argument('location', default='')
    bio = response.get_argument('bio', default='')

    if username_pat.match(username) and password == repeat_password:
        user = User.create(firstname, lastname, username, password, birthdate, email, location, bio)
        user.save()
        
        response.set_secure_cookie('username', username)
        response.redirect('/user/{}'.format(username))
    else:
        response.write('Please check the username entered meets criteria and both passwords match.')
Пример #18
0
def add_to_story(response, id):
    username = response.get_secure_cookie('username')
    user = User.find('username',  str(username, 'utf-8'))

    if not user:
        raise Exception("Expected user account when adding to story")
    user = user[0]

    addition_to_story = response.get_argument('paragraph')
    story = Story.find('id', id)[0]

    if not Rules.check(addition_to_story, story.id):
        #reject the story
        print('Rejected')
        #return to story view without updating.... TODO: show error
        response.redirect('/view_story/{}'.format(id))
        return

    added_paragraph = story.add_paragraph(user, addition_to_story)
    added_paragraph.save()

    response.redirect('/view_story/{}'.format(id))
Пример #19
0
 def actual_get(response, context, *args, **kwargs):
     context['users'] = User.find('all', '')
     html = template.render_file('templates/admin_index.html', context)
     response.write(html)
Пример #20
0
def check_login(username, password):
    return User.login(username, password)
Пример #21
0
 def get_author(self):
     from dbapi.user import User
     try:
         return User.find('id', self.author_id)[0]
     except IndexError:
         return None
Пример #22
0
def profiles(response):
	users = User.get('all')

	for user in users:
		response.write('<a href="/user/{}">{}</a><br />'.format(user.username, user.username))