def get(self, **kwargs): my_id = kwargs['id'] obj = models.JournalEntry.query.filter_by(id=my_id).first() context = dict(entry=obj, delete_url=flask.url_for('delete_entry', id=my_id), back_url=api.link_for_entry(obj)) return flask.render_template('delete_entry.html', context=context)
def get(self, **kwargs): u = flask_login.current_user latest = u.get_latest_entry() print('latest', latest) print( 'user', u.query_all_entries().order_by( models.JournalEntry.create_date.desc()).first()) if latest is not None: return flask.redirect(api.link_for_entry(latest)) else: flask.abort(404)
def get(self, **kwargs): found = models.JournalEntry.query.filter_by( id=kwargs['id'], owner=flask_login.current_user).first() if not found: flask.abort(404) form = forms.JournalEntryEditForm(**api.object_as_dict(found)) context = dict(create=False, heading='Edit Entry') context['heading'] = 'Edit entry' context.update( dict(back=api.link_for_entry(found), action=flask.url_for('edit_entry', id=form.id.data), form=form)) context.update(kwargs) db.session.close() return flask.render_template('edit_entry.html', context=context)
def parse_entry(self, e: 'models.JournalEntry') -> 'iterable[str]': """Find all dates mentioned in the entry body.""" logger.disabled = False seen = set() cal = pdt.Calendar() db.session.add(e) parsed_dates = cal.nlp(e.contents, e.create_date.timetuple()) if parsed_dates is None: return grouped_dates = dict() for t in parsed_dates: found_date = api.strip_datetime(t[0]) original_case = t[-1] human_strs = grouped_dates.get(found_date, []) human_strs.append(shorten_phrase(original_case)) # print('original case', original_case) grouped_dates.update({found_date: human_strs}) for date, date_str in grouped_dates.items(): found_entry = models.JournalEntry.query.filter( models.JournalEntry.create_date >= date).order_by( models.JournalEntry.create_date).first() dates_group = ' | '.join(date_str) if not found_entry: yield dict(html=f'<del>{dates_group}: {prettify(date)}</del>', url='') else: # get a link to the entry referred to by the found date url = api.link_for_entry(found_entry) # plugins may return HTML. if found_entry.create_date == date: item = f'<a href="{url}">{dates_group}: {prettify(date)}</a>' else: item = f'<a href="{url}">{dates_group}: <del>{prettify(date)} </del> ➤ {prettify(found_entry.create_date)}</a>' yield dict( html=item, url=url, ) seen.add(found_date)
def post(self, **kwargs): form = forms.JournalEntryEditForm(**flask.request.form) cu = flask_login.current_user context = dict(errors=list()) if form.validate_on_submit(): if form.owner_id.data == cu.id: obj = models.JournalEntry.query.filter_by(id=form.id.data, owner=cu).first() form.populate_obj(obj) session = db.session() session.add(obj) session.commit() return flask.redirect(api.link_for_entry(obj)) else: flask.abort(403) else: context['errors'].append('form invalid') return self.get(**context)
def post(self, **kwargs): form = forms.JournalEntryCreateForm(**flask.request.form) context = dict(form=form, errors=[]) context.update(**kwargs) if form.validate_on_submit(): obj = models.JournalEntry(owner=flask_login.current_user) form.populate_obj(obj) print('obj', obj.owner) session = db.session() try: session.add(obj) session.commit() except sqlalchemy.exc.IntegrityError: session.rollback() context['errors'].append('That date already has an entry!') return self.get(**context) return flask.redirect( api.link_for_entry( flask_login.current_user.get_latest_entry())) else: return self.get(**context)
def get_context(self, request): data = request.args plugin = extensions.name_search pdict = plugin.to_dict() context = dict(plugin=pdict, plugin_and_preference=dict( plugin=pdict, preference=plugin.get_preference_model( flask_login.current_user))) context['page'] = int(data.get('page', 0)) context['search'] = data.get('search', '') if 'search' in data: pagination = self.get_objects(context) context['pagination'] = pagination context['pagination_annotated'] = [ dict(item=item, link=api.link_for_entry(item)) for item in pagination.items ] else: args = flask.request.args self.get_summary(context) return context
def test_view_all_entries(self): with self.client: es = db.session.query(models.JournalEntry).all() for e in es: response = self.client.get(api.link_for_entry(e)) self.assertEqual(response._status_code, 200)
def get_url(self, entry): return link_for_entry(entry)