def new_news(): if not current_user.is_editor(): abort(404) form = NewsNewForm() if request.method == 'POST' and form.validate_on_submit(): if form.radec.data: ra, dec = parse_radec(form.radec.data) else: ra, dec = (None, None) news = News(lang_code=get_site_lang_code(), title=form.title.data, ra=ra, dec=dec, title_row=form.title_row.data, text=form.text.data, rating=form.rating.data, is_released=form.is_released.data, create_by=current_user.id, update_by=current_user.id, create_date=datetime.now(), update_date=datetime.now()) db.session.add(news) db.session.commit() flash('News successfully created', 'form-success') return redirect(url_for('main_news.news_edit', news_id=news.id)) return render_template('main/news/news_edit.html', form=form, is_new=True, countries=countries)
def constellation_edit(constellation_id): """Update constellation.""" if not current_user.is_editor(): abort(403) constellation = Constellation.query.filter_by(id=constellation_id).first() if constellation is None: abort(404) lang, editor_user = get_lang_and_editor_user_from_request( for_constell_descr=True) user_descr = None form = ConstellationEditForm() goback = False if editor_user: user_descr = UserConsDescription.query.filter_by( constellation_id=constellation.id, user_id=editor_user.id, lang_code=lang).first() if user_descr is None: user_descr = UserConsDescription( constellation_id=constellation_id, user_id=editor_user.id, common_name='', text='', lang_code=lang, create_by=current_user.id, update_by=current_user.id, create_date=datetime.now(), update_date=datetime.now(), ) if request.method == 'GET': form.common_name.data = user_descr.common_name form.text.data = user_descr.text elif form.validate_on_submit(): user_descr.common_name = form.common_name.data user_descr.text = form.text.data user_descr.update_by = current_user.id user_descr.update_date = datetime.now() db.session.add(user_descr) db.session.commit() flash(gettext('Constellation successfully updated'), 'form-success') if form.goback.data != 'true': return redirect( url_for('main_constellation.constellation_edit', constellation_id=constellation_id)) goback = True if goback: return redirect( url_for('main_constellation.constellation_info', constellation_id=constellation.iau_code)) author = _create_author_entry(user_descr.update_by, user_descr.update_date) return render_template('main/catalogue/constellation_edit.html', form=form, constellation=constellation, user_descr=user_descr, author=author)
def star_descr_info(star_descr_id): """View a star description info.""" lang, editor_user = get_lang_and_editor_user_from_request( for_constell_descr=True) user_descr = UserStarDescription.query.filter_by(id=star_descr_id, user_id=editor_user.id, lang_code=lang).first() if user_descr is None: abort(404) embed = request.args.get('embed') if embed: session['star_embed_seltab'] = 'info' season = request.args.get('season') editable = current_user.is_editor() if user_descr.star is not None: prev_star, next_star = _get_prev_next_star(user_descr.star) else: prev_star, next_star = (None, None) return render_template( 'main/catalogue/star_info.html', type='info', user_descr=user_descr, prev_star=prev_star, next_star=next_star, editable=editable, embed=embed, season=season, )
def star_edit(star_descr_id): """Update user star description object.""" if not current_user.is_editor(): abort(403) user_descr = UserStarDescription.query.filter_by(id=star_descr_id).first() if user_descr is None: abort(404) form = StarEditForm() if request.method == 'GET': form.common_name.data = user_descr.common_name form.text.data = user_descr.text elif form.validate_on_submit(): user_descr.common_name = form.common_name.data user_descr.text = form.text.data user_descr.update_by = current_user.id user_descr.update_date = datetime.now() db.session.add(user_descr) db.session.commit() flash('Star description successfully updated', 'form-success') if form.goback.data != 'true': return redirect( url_for('main_star.star_edit', star_descr_id=star_descr_id)) back = request.args.get('back') back_id = request.args.get('back_id') if back == 'constell': return redirect( url_for('main_constellation.constellation_info', constellation_id=back_id, _anchor='star' + str(user_descr.id))) return redirect( url_for('main_star.star_descr_info', star_descr_id=star_descr_id)) return render_template('main/catalogue/star_edit.html', form=form, user_descr=user_descr)
def news_delete(news_id): if not current_user.is_editor(): abort(404) news = News.query.filter_by(id=news_id).first() if news is None: abort(404) db.session.delete(news) flash('News was deleted', 'form-success') return redirect(url_for('main_news.news_list'))
def git_repository(): """Show ssh public key.""" form1 = GitSSHKeyForm() form1.git_repository.data = current_user.git_repository form1.ssh_public_key.data = current_user.git_ssh_public_key form2 = None if current_user.is_editor(): form2 = GitContentSSHKeyForm() form2.git_repository.data = current_user.git_content_repository form2.ssh_public_key.data = current_user.git_content_ssh_public_key return render_template('main/usersettings/usersettings_edit.html', form1=form1, form2=form2, type='git_repository')
def git_personal_repository(): """Post personal git repository settings.""" form1 = GitSSHKeyForm() form2 = None if current_user.is_editor(): form2 = GitContentSSHKeyForm() form2.git_repository.data = current_user.git_content_repository form2.ssh_public_key.data = current_user.git_content_ssh_public_key if form1.validate_on_submit(): current_user.git_repository = form1.git_repository.data db.session.add(current_user) db.session.commit() flash('Repository settings has been updated.', 'form-success') return render_template('main/usersettings/usersettings_edit.html', form1=form1, form2=form2, type='git_repository', subtype='personal_repo_key')
def constellation_stars(constellation_id): """View a constellation stars.""" constellation = _find_constellation(constellation_id) if constellation is None: abort(404) star_descriptions = None editable = current_user.is_editor() lang, editor_user = get_lang_and_editor_user_from_request( for_constell_descr=True) aster_descriptions = None if editor_user: star_descriptions = UserStarDescription.query.filter_by(user_id=editor_user.id, lang_code=lang)\ .filter_by(constellation_id=constellation.id) \ .all() star_descriptions = _sort_star_descr(star_descriptions) all_aster_descriptions = UserDsoDescription.query.filter_by(user_id=editor_user.id, lang_code=lang)\ .join(UserDsoDescription.deepskyObject, aliased=True) \ .filter(DeepskyObject.constellation_id == constellation.id, DeepskyObject.type == 'AST') \ .order_by(UserDsoDescription.rating.desc()) \ .all() existing = set() aster_descriptions = [] for dsod in all_aster_descriptions: if dsod.dso_id not in existing: existing.add(dsod.dso_id) aster_descriptions.append(dsod) common_name = _get_constellation_common_name(constellation) return render_template('main/catalogue/constellation_info.html', constellation=constellation, type='stars', star_descriptions=star_descriptions, aster_descriptions=aster_descriptions, editable=editable, common_name=common_name)
def news_edit(news_id): if not current_user.is_editor(): abort(404) news = News.query.filter_by(id=news_id).first() if news is None: abort(404) form = NewsEditForm() if request.method == 'POST': if form.validate_on_submit(): news.title = form.title.data if form.radec.data: news.ra, news.dec = parse_radec(form.radec.data) else: news.ra, news.dec = (None, None) news.lang_code = get_site_lang_code() news.title_row = form.title_row.data news.text = form.text.data news.rating = form.rating.data news.is_released = form.is_released.data news.update_by = current_user.id news.update_date = datetime.now() db.session.add(news) db.session.commit() flash('News successfully updated', 'form-success') else: form.title.data = news.title form.radec.data = radec_to_string_short(news.ra, news.dec) form.title_row.data = news.title_row form.text.data = news.text form.rating.data = news.rating form.is_released.data = news.is_released return render_template('main/news/news_edit.html', form=form, news=news, is_new=False, countries=countries)
def constellation_info(constellation_id): """View a constellation info.""" constellation = _find_constellation(constellation_id) if constellation is None: abort(404) user_descr = None common_name = None dso_descriptions = None star_descriptions = None title_images = None ug_bl_dsos = None lang, editor_user = get_lang_and_editor_user_from_request( for_constell_descr=True) lang_dso, editor_user_dso = get_lang_and_editor_user_from_request( for_constell_descr=False) cs_editor_user = get_cs_editor_user() if editor_user: ucd = UserConsDescription.query.filter_by(constellation_id=constellation.id, user_id=editor_user.id, lang_code=lang)\ .first() user_descr = ucd.text if ucd else None common_name = ucd.common_name if ucd else None star_descriptions = UserStarDescription.query.filter_by(user_id=editor_user.id, lang_code=lang)\ .filter_by(constellation_id=constellation.id) \ .all() star_descriptions = _sort_star_descr(star_descriptions) all_cs_dso_descriptions = UserDsoDescription.query.filter_by(user_id=cs_editor_user.id, lang_code='cs')\ .join(UserDsoDescription.deepskyObject, aliased=True) \ .filter(DeepskyObject.constellation_id == constellation.id, DeepskyObject.type != 'AST') \ .order_by(UserDsoDescription.rating.desc(), DeepskyObject.mag) \ .all() if lang != 'cs': # Show all objects that are in CS version plus UG-BL objects existing = set(dsod.dso_id for dsod in all_cs_dso_descriptions) all_dso_descriptions = [] available_dso_descriptions = UserDsoDescription.query.filter_by(user_id=editor_user_dso.id, lang_code=lang)\ .join(UserDsoDescription.deepskyObject, aliased=True) \ .filter(DeepskyObject.constellation_id == constellation.id, DeepskyObject.type != 'AST') \ .order_by(UserDsoDescription.rating.desc(), DeepskyObject.mag) \ .all() available_dso_descriptions_map = {} for dsod in available_dso_descriptions: available_dso_descriptions_map[dsod.dso_id] = dsod if dsod.dso_id in existing: all_dso_descriptions.append(dsod) elif dsod.deepskyObject.mag < 10.0: all_dso_descriptions.append(dsod) existing.add(dsod.dso_id) constell_ug_bl_dsos = get_ug_bl_dsos()[constellation.id] for dso_id in constell_ug_bl_dsos: dso = constell_ug_bl_dsos[dso_id] loading_dso_id = dso.master_id if dso.master_id is not None else dso_id if not dso_id in existing and loading_dso_id in available_dso_descriptions_map: all_dso_descriptions.append( available_dso_descriptions_map[loading_dso_id]) else: all_dso_descriptions = all_cs_dso_descriptions existing = set() dso_descriptions = [] title_images = {} for dsod in all_dso_descriptions: if dsod.dso_id not in existing: existing.add(dsod.dso_id) dso_descriptions.append(dsod) if not dsod.text or not dsod.text.startswith('![<]($IMG_DIR/'): image_info = get_dso_image_info_with_imgdir( dsod.deepskyObject.normalized_name_for_img()) if image_info is not None: title_images[dsod.dso_id] = image_info[0] dso_apert_descriptions = UserDsoApertureDescription.query.filter_by(user_id=editor_user_dso.id, lang_code=lang)\ .join(UserDsoApertureDescription.deepskyObject, aliased=True) \ .filter_by(constellation_id=constellation.id) \ .order_by(UserDsoApertureDescription.aperture_class, UserDsoApertureDescription.lang_code) \ .all() aperture_descr_map = {} for apdescr in dso_apert_descriptions: if apdescr.dso_id not in aperture_descr_map: aperture_descr_map[apdescr.dso_id] = [] dsoapd = aperture_descr_map[apdescr.dso_id] if apdescr.aperture_class not in [cl[0] for cl in dsoapd ] and apdescr.text: if apdescr.aperture_class == '<100': dsoapd.insert(0, (apdescr.aperture_class, apdescr.text)) else: dsoapd.append((apdescr.aperture_class, apdescr.text)) ug_bl_dsos = [] constell_ug_bl_dsos = get_ug_bl_dsos()[constellation.id] for dso_id in constell_ug_bl_dsos: if dso_id not in existing: dso = constell_ug_bl_dsos[dso_id] if dso.master_id not in existing: dso_image_info = get_dso_image_info( dso.normalized_name_for_img()) ug_bl_dsos.append({'dso': dso, 'img_info': dso_image_info}) ug_bl_dsos.sort(key=lambda x: x['dso'].mag) editable = current_user.is_editor() wish_list = None observed_list = None if current_user.is_authenticated: wish_list = [ item.dso_id for item in WishList.create_get_wishlist_by_user_id( current_user.id).wish_list_items ] observed_list = [ item.dso_id for item in ObservedList.create_get_observed_list_by_user_id( current_user.id).observed_list_items ] return render_template( 'main/catalogue/constellation_info.html', constellation=constellation, type='info', user_descr=user_descr, common_name=common_name, star_descriptions=star_descriptions, dso_descriptions=dso_descriptions, aperture_descr_map=aperture_descr_map, editable=editable, ug_bl_dsos=ug_bl_dsos, wish_list=wish_list, observed_list=observed_list, title_images=title_images, )
def _is_editable(news): return news.user_id == current_user.id or current_user.is_admin( ) or current_user.is_editor()
def deepskyobject_edit(dso_id): """Update deepsky object.""" if not current_user.is_editor(): abort(403) dso = DeepskyObject.query.filter_by(id=dso_id).first() if dso is None: abort(404) lang, editor_user = get_lang_and_editor_user_from_request( for_constell_descr=False) user_descr = None form = DeepskyObjectEditForm() if editor_user: user_descr = UserDsoDescription.query.filter_by( dso_id=dso.id, user_id=editor_user.id, lang_code=lang).first() authors = {} is_new = False if not user_descr: user_descr = UserDsoDescription( dso_id=dso_id, user_id=editor_user.id, rating=form.rating.data, lang_code=lang, common_name='', text='', references='', cons_order=1, create_by=current_user.id, create_date=datetime.now(), ) is_new = True all_user_apert_descrs = UserDsoApertureDescription.query.filter_by(dso_id=dso.id, user_id=editor_user.id, lang_code=lang) \ .order_by(UserDsoApertureDescription.aperture_class) user_apert_descriptions = [] # create missing UserDsoApertureDescription for aperture_class in SHOWN_APERTURE_DESCRIPTIONS: for ad in all_user_apert_descrs: if ad.aperture_class == aperture_class: user_apert_descriptions.append(ad) break else: ad = UserDsoApertureDescription( dso_id=dso_id, user_id=editor_user.id, rating=1, lang_code=lang, aperture_class=aperture_class, text='', create_by=current_user.id, create_date=datetime.now(), ) user_apert_descriptions.append(ad) if request.method == 'GET': form.common_name.data = user_descr.common_name form.text.data = user_descr.text form.references.data = user_descr.references form.rating.data = user_descr.rating for ad in user_apert_descriptions: adi = form.aperture_descr_items.append_entry() adi.aperture_class.data = ad.aperture_class adi.text.data = ad.text adi.is_public.data = ad.is_public adi.text.label = ad.aperture_class elif form.validate_on_submit(): was_text_changed = user_descr.text != form.text.data has_descr = False for adi in form.aperture_descr_items: for ad in user_apert_descriptions: if ad.aperture_class == adi.aperture_class.data: if adi.text.data: ad.text = adi.text.data ad.is_public = adi.is_public.data ad.update_by = current_user.id ad.update_date = datetime.now() db.session.add(ad) has_descr = True elif ad.id is not None: db.session.delete(ad) if is_new or was_text_changed or user_descr.common_name != form.common_name.data or \ user_descr.references != form.references.data or user_descr.rating != form.rating.data: user_descr.common_name = form.common_name.data user_descr.references = form.references.data user_descr.text = form.text.data user_descr.rating = form.rating.data if was_text_changed: user_descr.update_by = current_user.id user_descr.update_date = datetime.now() if has_descr or user_descr.text: db.session.add(user_descr) else: db.session.delete(user_descr) db.session.commit() flash('Deepsky object successfully updated', 'form-success') if form.goback.data != 'true': return redirect( url_for('main_deepskyobject.deepskyobject_edit', dso_id=dso_id)) back = request.args.get('back') back_id = request.args.get('back_id') if back == 'constell': return redirect( url_for('main_constellation.constellation_info', constellation_id=back_id, _anchor='dso' + str(dso.id))) return redirect( url_for('main_deepskyobject.deepskyobject_info', dso_id=dso.name, back=back, back_id=back_id)) authors['dso'] = _create_author_entry(user_descr.update_by, user_descr.update_date) for ad in user_apert_descriptions: authors[ad.aperture_class] = _create_author_entry( ad.update_by, ad.update_date) return render_template('main/catalogue/deepskyobject_edit.html', form=form, dso=dso, authors=authors, is_new=False)
def deepskyobject_info(dso_id): """View a deepsky object info.""" dso, orig_dso = _find_dso(dso_id) if dso is None: abort(404) embed = request.args.get('embed') if embed: session['dso_embed_seltab'] = 'info' lang, editor_user = get_lang_and_editor_user_from_request( for_constell_descr=False) user_descr = None apert_descriptions = [] title_img = None if editor_user: user_descr = UserDsoDescription.query.filter_by( dso_id=dso.id, user_id=editor_user.id, lang_code=lang).first() user_apert_descrs = UserDsoApertureDescription.query.filter_by(dso_id=dso.id, user_id=editor_user.id, lang_code=lang) \ .filter(func.coalesce(UserDsoApertureDescription.text, '') != '') \ .order_by(UserDsoApertureDescription.aperture_class, UserDsoApertureDescription.lang_code) for apdescr in user_apert_descrs: if apdescr.aperture_class not in [ cl[0] for cl in apert_descriptions ] and apdescr.text: if apdescr.aperture_class == '<100': apert_descriptions.insert( 0, (apdescr.aperture_class, apdescr.text)) else: apert_descriptions.append( (apdescr.aperture_class, apdescr.text)) if user_descr and (not user_descr.text or not user_descr.text.startswith('![<]($IMG_DIR/')): image_info = get_dso_image_info_with_imgdir( dso.normalized_name_for_img()) if image_info is not None: title_img = image_info[0] prev_dso, prev_dso_title, next_dso, next_dso_title = _get_prev_next_dso( orig_dso) editable = current_user.is_editor() descr_available = user_descr and user_descr.text or any( [adescr for adescr in apert_descriptions]) dso_image_info = get_dso_image_info(dso.normalized_name_for_img()) other_names = _get_other_names(dso) wish_list = None observed_list = None if current_user.is_authenticated: wish_item = WishListItem.query.filter(WishListItem.dso_id.in_((dso.id, orig_dso.id))) \ .join(WishList) \ .filter(WishList.user_id == current_user.id) \ .first() wish_list = [wish_item.dso_id] if wish_item is not None else [] observed_item = ObservedListItem.query.filter(ObservedListItem.dso_id.in_((dso.id, orig_dso.id))) \ .join(ObservedList) \ .filter(ObservedList.user_id == current_user.id) \ .first() observed_list = [observed_item.dso_id ] if observed_item is not None else [] has_observations = _has_dso_observations(dso, orig_dso) season = request.args.get('season') return render_template( 'main/catalogue/deepskyobject_info.html', type='info', dso=dso, user_descr=user_descr, apert_descriptions=apert_descriptions, prev_dso=prev_dso, next_dso=next_dso, prev_dso_title=prev_dso_title, next_dso_title=next_dso_title, editable=editable, descr_available=descr_available, dso_image_info=dso_image_info, other_names=other_names, wish_list=wish_list, observed_list=observed_list, title_img=title_img, season=season, embed=embed, has_observations=has_observations, )
def userdata_menu(): can_edit_news = current_user.is_editor() return render_template('main/userdata/userdata_menu.html', can_edit_news=can_edit_news)
def _is_git_content_enabled(): if not current_user.is_editor(): return False return current_user.git_content_repository and \ current_user.git_content_ssh_public_key and \ current_user.git_content_ssh_private_key
def _is_editable(location): return location.user_id == current_user.id or current_user.is_admin( ) or current_user.is_editor()