def export_articles(): """ Export all articles to HTML or JSON. """ user = UserController(g.user.id).get(id=g.user.id) if request.args.get('format') == "HTML": # Export to HTML try: archive_file, archive_file_name = export.export_html(user) except: flash(gettext("Error when exporting articles."), 'danger') return redirect(redirect_url()) response = make_response(archive_file) response.headers['Content-Type'] = 'application/x-compressed' response.headers['Content-Disposition'] = 'attachment; filename=%s' \ % archive_file_name elif request.args.get('format') == "JSON": # Export to JSON try: json_result = export.export_json(user) except: flash(gettext("Error when exporting articles."), 'danger') return redirect(redirect_url()) response = make_response(json_result) response.mimetype = 'application/json' response.headers["Content-Disposition"] \ = 'attachment; filename=account.json' else: flash(gettext('Export format not supported.'), 'warning') return redirect(redirect_url()) return response
def edit(shelter_id=0, section_name=""): shelter = Shelter.query.filter(Shelter.id == shelter_id).first() if current_user.is_admin: pass elif shelter is not None and current_user.id == shelter.user_id: pass else: flash('Only Admin or shelter creater have edit permission', 'warning') # render_template('errors/403.html'), 403 return redirect(url_for('join')) user = User.query.filter(User.id == shelter.user_id).first() sections = Section.query.filter() try: shelter, section, categories, pictures, documents = \ load_shelter_info(shelter_id, section_name) except Exception as e: flash(str(e), "warning") return redirect(redirect_url()) return render_template('edit.html', shelter=shelter, categories=categories, pictures=pictures, sections=sections, section=section, documents=documents, user=user)
def delete_attribute_picture(picture_id=None): picture = AttributePicture.query.filter(AttributePicture.id==picture_id).first() if picture: # TODO: delete physically db.session.delete(picture) db.session.commit() return redirect(redirect_url())
def details(shelter_id=0, section_name="", to_pdf=None): sections = Section.query.filter() try: shelter, section, categories, pictures, documents = \ load_shelter_info(shelter_id, section_name) except Exception as e: flash(str(e), "warning") return redirect(redirect_url()) if to_pdf == "to_pdf": try: pdf_file = create_pdf(render_template('pdf/template1.html', shelter=shelter, section=section, categories=categories, pictures=pictures)) response = make_response(pdf_file) response.headers['Content-Type'] = 'application/pdf' response.headers['Content-Disposition'] = \ 'attachment; filename=%s.pdf' % 'shelter' return response except Exception as e: print(e) flash('Error when generating PDF file.', 'danger') return render_template('details.html', shelter=shelter, categories=categories, pictures=pictures, sections=sections, section=section, documents=documents)
def delete_translation(language_code=None): """ Delete a translation. """ Translation.query.filter(Translation.language_code==language_code).delete() db.session.commit() return redirect(redirect_url())
def delete_user(user_id=None): """ Delete a user (with all its data). """ user = User.query.filter(User.id == user_id).first() db.session.delete(user) db.session.commit() return redirect(redirect_url())
def delete_user(user_id=None): """ Delete a user (with all its data). """ user = User.query.filter(User.id==user_id).first() db.session.delete(user) db.session.commit() return redirect(redirect_url())
def like(article_id=None): """ Mark or unmark an article as favorites. """ art_contr = ArticleController(g.user.id) article = art_contr.get(id=article_id) art_contr = art_contr.update({'id': article_id}, {'like': not article.like}) return redirect(redirect_url())
def get_multi_media(shelter_id=0, category_id=2, section='Identification'): """ Get pictures for the shelter sent by Dropzone via a POST request. """ first = False imgwidth = 800 shelter = Shelter.query.filter(Shelter.id == shelter_id).first() if not shelter: flash("No such shelter", "warning") return redirect(redirect_url()) for f in request.files: if request.files[f] and request.files[f].filename == '': flash('No selected file', 'warning') return redirect(request.url) if request.files[f] and allowed_file( request.files[f].filename, conf.ALLOWED_EXTENSIONS_PICTURE.union( conf.ALLOWED_EXTENSIONS_DOCUMENT)): path = os.path.join(conf.SHELTERS_PICTURES_SERVER_PATH, str(shelter.id)) if not os.path.exists(path): os.makedirs(path) file_extension = os.path.splitext(request.files[f].filename)[1] filename = str(shelter_id) + '_' + section + "_" + str( time.time()) + file_extension im = Image.open(request.files[f]) if im.size[0] > imgwidth: ratio = (imgwidth / float(im.size[0])) hsize = int((float(im.size[1]) * float(ratio))) print((imgwidth, hsize)) resized_im = im.resize((imgwidth, hsize), Image.BILINEAR) resized_im.save(os.path.join(path, filename), "JPEG", quality=90) else: im.save(os.path.join(path, filename), "JPEG", quality=90) print("Category id '{}' ...".format(category_id)) if category_id: new_media = ShelterPicture(file_name=filename, is_main_picture=False, shelter_id=shelter.id, category_id=category_id) db.session.add(new_media) db.session.commit() first = True return redirect(request.url)
def delete_user(user_id=None): """ Delete a user (with all its data). """ try: user = UserController().delete(user_id) flash(gettext('User %(nick)s successfully deleted', nick=user.nickname), 'success') except Exception as error: flash(gettext('An error occured while trying to delete a user: '******'%(error)', error=error), 'danger') return redirect(redirect_url())
def publish_shelter(shelter_id=None): """ Publish/Unpublish a shelter. """ shelter = Shelter.query.filter(Shelter.id == shelter_id).first() if shelter: shelter.is_published = request.args.get('publish', not shelter.is_published) db.session.add(shelter) db.session.commit() return redirect(redirect_url())
def fetch(feed_id=None): """ Triggers the download of news. News are downloaded in a separated process, mandatory for Heroku. """ if conf.CRAWLING_METHOD == "classic" \ and (not conf.ON_HEROKU or current_user.is_admin): utils.fetch(current_user.id, feed_id) flash(gettext("Downloading articles..."), "info") else: flash(gettext("The manual retrieving of news is only available " + "for administrator, on the Heroku platform."), "info") return redirect(redirect_url())
def user(user_id=None): """ See information about a user (stations, etc.). """ user = UserController().get(id=user_id) if user is not None: article_contr = ArticleController(user_id) return render_template('/admin/user.html', user=user, feeds=user.feeds, article_count=article_contr.count_by_feed(), unread_article_count=article_contr.count_by_feed(readed=False)) else: flash(gettext('This user does not exist.'), 'danger') return redirect(redirect_url())
def fetch(feed_id=None): """ Triggers the download of news. News are downloaded in a separated process, mandatory for Heroku. """ if conf.CRAWLING_METHOD == "classic" \ and (not conf.ON_HEROKU or current_user.is_admin): utils.fetch(current_user.id, feed_id) flash(gettext("Downloading articles..."), "info") else: flash( gettext("The manual retrieving of news is only available " + "for administrator, on the Heroku platform."), "info") return redirect(redirect_url())
def expire(): """ Delete articles older than the given number of weeks. """ current_time = datetime.utcnow() weeks_ago = current_time - timedelta(int(request.args.get('weeks', 10))) art_contr = ArticleController(g.user.id) query = art_contr.read(__or__={'date__lt': weeks_ago, 'retrieved_date__lt': weeks_ago}) count = query.count() query.delete() flash(gettext('%(count)d articles deleted', count=count), 'info') return redirect(redirect_url())
def delete_shelter(shelter_id=None): """ Delete a shelter. """ ShelterPicture.query.filter(ShelterPicture.shelter_id==shelter_id).delete() properties = Property.query.filter(Property.shelter_id==shelter_id) for property in properties: Association.query.filter(Association.property_id==property.id).delete() db.session.delete(property) Shelter.query.filter(Shelter.id==shelter_id).delete() db.session.commit() return redirect(redirect_url())
def edit(shelter_id=0, section_name=""): sections = Section.query.filter() try: shelter, section, categories, pictures, documents = \ load_shelter_info(shelter_id, section_name) except Exception as e: flash(str(e), "warning") return redirect(redirect_url()) return render_template('edit.html', shelter=shelter, categories=categories, pictures=pictures, sections=sections, section=section, documents=documents)
def delete_user(user_id=None): """ Delete a user (with all its data). """ try: user = UserController().delete(user_id) flash( gettext('User %(nick)s successfully deleted', nick=user.nickname), 'success') except Exception as error: flash( gettext( 'An error occured while trying to delete a user: '******'%(error)', error=error), 'danger') return redirect(redirect_url())
def user(user_id=None): """ See information about a user (stations, etc.). """ user = UserController().get(id=user_id) if user is not None: article_contr = ArticleController(user_id) return render_template( '/admin/user.html', user=user, feeds=FeedController().read(user_id=user_id).order_by('title'), article_count=article_contr.count_by_feed(), unread_article_count=article_contr.count_by_feed(readed=False)) else: flash(gettext('This user does not exist.'), 'warn') return redirect(redirect_url())
def delete(shelter_id=0, section_name=""): """ Delete a shelter. if created by requesting user """ shelter = Shelter.query.filter(Shelter.id==shelter_id, Shelter.user_id==current_user.id) if shelter is not None: ShelterPicture.query.filter(ShelterPicture.shelter_id==shelter_id).delete() properties = Property.query.filter(Property.shelter_id==shelter_id) for property in properties: Association.query.filter(Association.property_id==property.id).delete() db.session.delete(property) shelter.delete() db.session.commit() return redirect(redirect_url())
def get_media(shelter_id=0, section_name=""): """ Get the media (pictures or documents) for the shelter sent via a POST request. """ shelter = Shelter.query.filter(Shelter.id == shelter_id).first() if not shelter: flash("No such shelter", "warning") return redirect(redirect_url()) file = request.files.get('mediafile', None) if file and file.filename == '': flash('No selected file', 'warning') return redirect(request.url) if file and allowed_file( file.filename, conf.ALLOWED_EXTENSIONS_PICTURE.union( conf.ALLOWED_EXTENSIONS_DOCUMENT)): if 'pictures' in request.form: path = os.path.join(conf.SHELTERS_PICTURES_SERVER_PATH, str(shelter.id)) if 'documents' in request.form: path = os.path.join(conf.SHELTERS_DOCUMENTS_SERVER_PATH, str(shelter.id)) if not os.path.exists(path): os.makedirs(path) filename = secure_filename(file.filename) file.save(os.path.join(path, filename)) category_id = request.form['category_id'] if category_id: if 'pictures' in request.form: new_media = ShelterPicture(file_name=filename, shelter_id=shelter.id, category_id=category_id) if 'documents' in request.form: new_media = ShelterDocument(file_name=filename, shelter_id=shelter.id, category_id=category_id) db.session.add(new_media) db.session.commit() return redirect(request.url)
def mark_as(new_value='read', feed_id=None, article_id=None): """ Mark all unreaded articles as read. """ readed = new_value == 'read' art_contr = ArticleController(g.user.id) filters = {'readed': not readed} if feed_id is not None: filters['feed_id'] = feed_id message = 'Feed marked as %s.' elif article_id is not None: filters['id'] = article_id message = 'Article marked as %s.' else: message = 'All article marked as %s.' art_contr.update(filters, {"readed": readed}) flash(gettext(message % new_value), 'info') if readed: return redirect(redirect_url()) return redirect('home')
def get_media(shelter_id=0, section_name=""): """ Get the media (pictures or documents) for the shelter sent via a POST request. """ shelter = Shelter.query.filter(Shelter.id==shelter_id).first() if not shelter: flash("No such shelter", "warning") return redirect(redirect_url()) file = request.files.get('mediafile', None) if file and file.filename == '': flash('No selected file', 'warning') return redirect(request.url) if file and allowed_file(file.filename, conf.ALLOWED_EXTENSIONS_PICTURE.union( conf.ALLOWED_EXTENSIONS_DOCUMENT)): if 'pictures' in request.form: path = os.path.join(conf.SHELTERS_PICTURES_SERVER_PATH, str(shelter.id)) if 'documents' in request.form: path = os.path.join(conf.SHELTERS_DOCUMENTS_SERVER_PATH, str(shelter.id)) if not os.path.exists(path): os.makedirs(path) filename = secure_filename(file.filename) file.save(os.path.join(path , filename)) category_id = request.form['category_id'] if category_id: if 'pictures' in request.form: new_media = ShelterPicture(file_name=filename, shelter_id=shelter.id, category_id=category_id) if 'documents' in request.form: new_media = ShelterDocument(file_name=filename, shelter_id=shelter.id, category_id=category_id) db.session.add(new_media) db.session.commit() return redirect(request.url)
def delete(category_id=None): category = CategoryController(g.user.id).delete(category_id) flash(gettext("Category %(category_name)s successfully deleted.", category_name=category.name), 'success') return redirect(redirect_url())