def toEntry(self, pictures): # and now create the entry c = Config() e = Entry(source = self.source_id, url = "http://picasaweb.google.com/" + c.getKey('picasa_user')) # define whether we show a matrix with pictures (in case we've got more than one) or just a bigger thumbnail) if(len(pictures) > 1): html = "<div class=\"picasa-entry\">" html += "<ul class=\"picasa-entry-ul\">" #for pic in pictures: # html += self.pictureHTML(pic, True) html += "".join(map(lambda e: self.pictureHTML(e, True), pictures)) html += "</ul></div>" e.text = html e.title = "%s new photos (%s)" % (str(len(pictures)), self._getTodayDate()) else: pic = pictures.pop() # only one picture, we can show a bigger version of the picture # the markup uses different CSS classes so that we can control the styling separately e.title = "New photo upload (%s)" % (self._getTodayDate()) e.text = "<div class=\"picasa-single-entry\">" + self.pictureHTML(pic, False) + "</div>" return e
def detail(slug): if session.get('logged_in'): query = Entry.select() else: query = Entry.public() entry = get_object_or_404(query, Entry.slug == slug) return render_template('detail.jinja2', entry=entry)
def toEntry(self, video): e = Entry() if video.title != None: e.title = video.title.text.decode('UTF-8') if video.content != None: if video.media.content != None: e.text = '<div class="video">' + self.getFlashPlayerHTML( video.media.content[0].url ) + '</div>' if video.content.text != None: e.text += video.content.text.decode('UTF-8') e.source = self.source_id e.external_id = video.id.text e.created = parse( video.published.text ) e.url = video.link[0].href if video.media.keywords != None: # split the tags e.tags = video.media.keywords.text.decode('UTF-8').replace(' ','').split(',') # save the location data if available if video.geo: e.lat = str(video.geo.latitude()) e.lng = str(video.geo.longitude()) return e
def test_add_entry(self): entry = Entry(title='hello', text='hi') db.session.add(entry) db.session.commit() # print(db.session.query(Entry).count()) eq_(db.session.query(Entry).count(), 1)
def toEntry(self, item): e = Entry() e.external_id = item.id e.created = item.created_time e.source = self.source_id e.url = item.link e.title = item.caption.text e.text = self.makeInstagramText(item) # save the location data in case it's got any if 'location' in dir(item): e.lat = "%.15f" % item.location.point.latitude e.lng = "%.15f" % item.location.point.longitude e.location_name = item.location.name return e
def index(): search_query = request.args.get('q') if search_query: query = Entry.search(search_query) else: query = Entry.public().order_by(Entry.timestamp.desc()) # The `object_list` helper will take a base query and then handle # paginating the results if there are more than 20. For more info see # the docs: # http://docs.peewee-orm.com/en/latest/peewee/playhouse.html#object_list return object_list( 'index.jinja2', query, search=search_query, check_bounds=False)
def get(self, entry_id): e = Entry.get( entry_id ) if e == None: # entry not found self.writeResponse({'error': True, 'message': 'Entry not found'}) else: self.writeResponse({'error': False, 'entry': e, 'entry_id': entry_id})
def places(self): # this action generates different content depending on how it is called view_data = {} if self.request.get('f') == 'json': # select those entries that have location data query = Entry.gql('WHERE lat != :lat AND deleted = :deleted', lat=None, deleted=False) view_data = { 'entries': query } return 'places.html', view_data
def test_show_entries(self): entry = Entry(title='hello', text='hi') db.session.add(entry) db.session.commit() response = self.client.get('/') print(response.data) eq_(response.status_code, 200)
def entry(self, entry_slug): # see if we can find the entry by slug entry = Entry.all().filter('slug =', entry_slug ).filter('deleted = ', False).get() #entry = self.getEntryQuery({'slug = ': entry_slug}).get() # entry not found, let's try by id if entry == None: try: entry = Entry.get(entry_slug) except BadKeyError: entry = None if entry == None: #self.response.out.write( View(self.request, 'error.html').render ({ 'message': 'Entry could not be found '} )) return 'error.html', { 'message': 'Entry could not be found ', 'error': True} # if found, display it return 'entry.html', { 'entry': entry }
def toEntry(self, post): e = Entry() e.external_id = post['hash'] e.url = post['href'] e.title = post['description'] e.text = post['extended'] e.source = self.source_id e.created = parse( post['time'] ) # this is a bit weird but for some reason, the list of tags from post['tags'] will # report at least one element even if it's empty, so we need to protect against that e.tags = [] if len(post['tags'][0]) == 0 else post['tags'] return e
def isDuplicate( self, entry ): if self.source_id == '': raise AssertionError("Source.source_id cannot be empty before using the Source.isDuplicate method") query = Entry.gql( 'WHERE external_id = :id AND source = :source', id = str(entry.external_id), source = self.source_id ) value = False; if query.count() > 0: value = True logging.debug( 'duplicate check: source = ' + self.source_id + ', id = ' + str(entry.external_id) + ', result = ' + str(value)) return value
def es_rebuild(): for entry in Entry.select(): es.index(index=app.config.get('ES_INDEX_NAME'), doc_type=app.config.get('ES_TYPE_NAME'), id=entry.id, body={ 'title': entry.title, 'content': entry.content } ) app.logger.info('[ES] Index post {}: {}'.format(entry.id, entry.title)) return jsonify({'status': 'success'})
def post(self): form = EntryForm( self.request.POST ) if form.is_valid(): # validation successful, we can save the data e = Entry() print(form.clean_data) e.title = form.clean_data['title'] e.text = form.clean_data['text'] e.tags = form.clean_data['tags'].split(' ') e.lat = form.clean_data['lat'] e.lng = form.clean_data['lng'] e.source = 'blog' e.put() # redirect to the main page self.redirect( '/' ) else: # form not valid, must show again with the errors self.writeResponse( 'new_blog_post.html', { 'form': form.render() } )
def toEntry(self, item): e = Entry() e.title = item.title e.url = item.link e.created = parse(item.published) e.source = self.source_id e.external_id = item.id # if there's an annotation, let's use it as the body for the post if 'content' in item: if len(item.content) > 1: e.text = item.content[1].value return e
def get(self, entry_id): try: entry = Entry.get(entry_id) except BadKeyError: entry = None if entry == None: self.writeResponse('error.html', { 'message': 'Entry could not be found '} ) else: # if found, display it self.writeResponse('new_blog_post.html', { 'entry': entry, 'entry_id': entry.key(), 'form': EntryForm(instance=entry).render() })
def create(): if request.method == 'POST': if request.form.get('title') and request.form.get('content'): entry = Entry.create( title=request.form['title'], content=request.form['content'], published=request.form.get('published') or False) flash('Entry created successfully.', 'success') if entry.published: return redirect(url_for('detail', slug=entry.slug)) else: return redirect(url_for('edit', slug=entry.slug)) else: flash('Title and Content are required.', 'danger') return render_template('create.jinja2')
def delete(self, entry_id): e = Entry.get( entry_id ) if e == None: # entry not found self.writeResponse({'error': True, 'message': 'Entry not found'}) else: # otherwise, mark it as deleted and return success e.deleted = True e.put() # reset the data cache since there's been some changes from google.appengine.api import memcache memcache.flush_all() self.writeResponse({'error': False, 'message': 'Entry successfully deleted', 'entry_id': entry_id})
def es_search2(): if not app.config.get('IS_ES_INDEX'): return 'Sorry, you need enable Elasticsearch first.' app.logger.info('{} - {}'.format(request.remote_addr, request.url)) query = request.args.get('q') results = es.search(index=app.config.get('ES_INDEX_NAME'), doc_type=app.config.get('ES_TYPE_NAME'), q=query) hits = results['hits']['hits'] entries = [] for hit in hits: entries.append(Entry.get(Entry.id == hit['_id'])) return render_template('search.jinja2', entries=entries, search=query)
def _updateEntry(self, form, entry_id): logging.debug("query string: " + self.request.query_string) logging.debug("query body: " + self.request.body) e = Entry.get( entry_id ) if e == None: # entry not found self.response.out.write( View(None, self.request).render( {'error': True, 'message': 'Entry not found'}, force_renderer='json')) form = EntryForm( self.request.POST ) if form.is_valid(): # validation succesful e.title = form.clean_data['title'] e.text = form.clean_data['text'] e.tags = form.clean_data['tags'].split(' ') e.lat = form.clean_data['lat'] e.lng = form.clean_data['lng'] e.put() # reset the data cache since there's new content from google.appengine.api import memcache memcache.flush_all() data = { 'error': False, 'entry': e, 'entry_id': entry_id, 'entry_link': e.permalink(), 'message': 'Entry successfully updated. <a href="%s">Link to the entry</a>.' % e.permalink(), } else: # form not valid, must show again with the errors data = { 'error': True, 'errors': {}} # (the Form object is actually iterable) for field in form: if field.errors: data['errors'][field.name] = field.errors # return the view self.writeResponse(data)
def toEntry(self, status): from app.utils import StringHelper e = Entry(external_id=str(status['id']), source = self.source_id, text = Utils.links_to_anchors(Utils.twitpic_to_img(status['text'])), title = StringHelper().remove_new_lines(status['text']), url='http://twitter.com/' + str(status['user']['screen_name']) + '/statuses/' + str(status['id'])) e.created = parse(status['created_at']) # extract the tags e.tags = StringHelper().extract_twitter_tags(status['text']) # process the location coordinates if they're available if status['coordinates'] != None: e.lat = str(status['coordinates']['coordinates'][1]) e.lng = str(status['coordinates']['coordinates'][0]) # is this entry a reply-to? logging.debug(e.text[0]) e.twitter_reply = (e.text[0] == '@') return(e)
def drafts(): query = Entry.drafts().order_by(Entry.timestamp.desc()) return object_list('index.jinja2', query, check_bounds=False)
def _addNewEntry(self, form): if form.is_valid(): # validation successful, we can save the data e = Entry() e.title = form.clean_data['title'] e.text = form.clean_data['text'] e.tags = form.clean_data['tags'].split(' ') e.lat = form.clean_data['lat'] if e.lat == "": e.lat = None e.lng = form.clean_data['lng'] if e.lng == "": e.lng = None e.source = 'blog' e.put() e = Entry.get(e.key()) # reset the data cache since there's new content from google.appengine.api import memcache memcache.flush_all() # return successful creation view_data = { 'message': 'New blog entry added successfully. <a href="%s">Link to the entry</a>.' % e.permalink(), 'error': False, 'entry_link': e.permalink(), 'entry': e } self.writeResponse(view_data) else: # form not valid, must show again with the errors data = { 'error': True, 'errors': {}} # (the Form object is actually iterable) for field in form: if field.errors: data['errors'][field.name] = field.errors self.writeResponse(data)
def getMostRecentEntry(self): query = Entry.gql( 'WHERE source = :source ORDER BY created DESC', source=self.source_id) if query.count() == 0: return None return(query.fetch(1)[0])
def add_entry(): entry = Entry(title=request.form['title'], text=request.form['text']) db.session.add(entry) db.session.commit() flash('New entry was successfully posted') return redirect(url_for('entry.show_entries'))
def wrapper(s, entry_id,**kwargs): entry = Entry.get_by_id(entry_id) if entry: return func(s, entry, **kwargs) else: raise NotFound('not found')
def post(self,json_data=None, **kwargs): entry = Entry(**json_data) entry.put() return entry.to_json()
def getEntryQuery(self, extraFilters = {}): return Entry.getPagedQueryWithBasicFilters(extraFilters)