Exemplo n.º 1
0
def save_item(request, *args, **kwargs):
	result = {}
	logging.info('item_id: %s', request.get('item_id'))
	if request.get('item_id'):
		link = Link.get(request.get('item_id'))
	else:
		link = Link()
		link.created = datetime.datetime.now()
		link.created_day = datetime.date.today().day
		link.created_month = datetime.date.today().month
		link.created_year = datetime.date.today().year
		link.owner = users.get_current_user().user_id()

	link.link = request.get('link')
	link.description = request.get('description')
	link.is_actual = request.get('is_actual') == '1' 
	link.put()
	
	_update_search_index(link, request.get('tags'))

	#saving tags
	for tag in LinkTag.all().filter('link = ', link):
		tag.delete()
	if request.get('tags'):
		tags = request.get('tags').split(',')
		for tag_name in tags:
			tag_name = tag_name.strip()
			if len(tag_name):
				tag = Tag.gql('where name=:1',tag_name).get()
				if tag == None:
					tag = Tag(name=tag_name).put()
				LinkTag(link=link, tag=tag, is_actual=link.is_actual, owner=link.owner).put()
			
	result['ok'] = 1
	return webapp2.Response(json.dumps(result), content_type='application/json')
Exemplo n.º 2
0
def show_tags_list(request, *args, **kwargs):
	params = {}
	tags = Tag.all().order('name').fetch(1000)
	for tag in tags:
		tag.num_links = LinkTag.all().filter('tag = ', tag).filter('is_actual = ', True).filter('owner = ', users.get_current_user().user_id()).count()

	min_count = 1
	max_count = 1
	min_size = 10
	max_size = 20
	for tag in tags:
		if min_count > tag.num_links:
			min_count = tag.num_links 
		if max_count < tag.num_links:
			max_count = tag.num_links
			
	for tag in tags:
		tag.size = _get_size(min_size, max_size, tag.num_links-(min_count-1), max_count+1)

	params['tags'] = tags
	params['page_title'] = 'список тэгов'
	params['menu_items'] = get_menu_items()
	params['user'] = users.get_current_user().nickname()
	
	return _render_to_response(params, 'tags');
Exemplo n.º 3
0
def edit_item(request, *args, **kwargs):		
	params = {}
	params['item_id'] = request.get('item_id')
	if params['item_id']:
		link = Link.get(params['item_id'])
		link.tags = []
		for tag in LinkTag.all().filter('link = ', link).fetch(10):
			link.tags.append(tag.tag.name)
		params['link'] = link
	params['tags'] = Tag.all()
	return _render_to_response(params,'edit')
Exemplo n.º 4
0
def _get_list(params):
	params['limit'] = 5
	q = db.Query(Link)
	q.filter('owner = ', users.get_current_user().user_id())
	
	if params.has_key('not_actual'):
		q.filter('is_actual =', False)
	else:
		q.filter('is_actual =', True)
	
	if params.has_key('year'):
		q.filter('created_year = ', params['year'])
	if params.has_key('month'):
		q.filter('created_month = ', params['month'])
	if params.has_key('day'):
		q.filter('created_day = ', params['day'])
		
	if params.has_key('tag'):
		linktags = []
		for lt in LinkTag.all().filter('tag = ', params['tag']):
			linktags.append(lt.link.key())
		q.filter('__key__ in ', linktags)

	if params.has_key('keys'):
		q.filter('__key__ in ', params['keys'])
		
	total = q.count()
	q.order('-created')
	links = q.fetch(limit=params['limit'], offset=(params['page']-1)*params['limit'])

	#for (i, link) in enumerate(links):
	for link in links:
		link.tags = LinkTag.all().filter('link = ', link).fetch(10)

	#for i,linktag in enumerate(LinkTag.all()):
	#	print i, linktag.key(), linktag.link.key(), linktag.tag.key()
	#	logging.info('%s %s', linktag.link.key(), linktag.tag.name)
	return {'list': links, 'total': total, 'pages': int(math.ceil(total/params['limit']))}
Exemplo n.º 5
0
def delete_item(request, *args, **kwargs):
	result = {}
	if request.get('item_id'):
		result['item_id'] = request.get('item_id')
		link = Link.get(result['item_id'])
	if link:
		search.Index(name='links').delete(result['item_id'])
		for tag in LinkTag.all().filter('link = ', link):
			tag.delete()
		link.delete()
		result['ok'] = 1
		
	else:
		result['error'] = 1
	return webapp2.Response(json.dumps(result), content_type='application/json')
Exemplo n.º 6
0
def expire_item(request, *args, **kwargs):
	result = {}
	if request.get('item_id'):
		result['item_id'] = request.get('item_id')
		link = Link.get(result['item_id'])
	if link:
		link.is_actual = False
		link.put()
		tags = []
		for tag in LinkTag.all().filter('link = ', link):
			tags.append(tag.tag.name)
		_update_search_index(link, ','.join(tags))
		result['ok'] = 1
		
	else:
		result['error'] = 1
	return webapp2.Response(json.dumps(result), content_type='application/json')