def follow(): Follower.follow(g.current_user, g.user) create_follower_notification(g.user, g.current_user) db.session.flush() unlock_user_achievements(g.current_user, FOLLOW_ACHIEVEMENTS) unlock_user_achievements(g.user, FOLLOWER_ACHIEVEMENTS) db.session.commit() return redirect(request.referrer or url_for('.index'))
def add_comment(): if not g.current_user: flash(_('You have to be logged in to post comments!'), 'warning') return redirect(url_for('.index')) text = request.form['text'].strip() if not text: return redirect(url_for('.index')) comment = FlightComment() comment.user = g.current_user comment.flight = g.flight comment.text = text create_flight_comment_notifications(comment) db.session.flush() unlock_user_achievements(g.current_user, COMMENT_ACHIEVEMENTS) db.session.commit() return redirect(url_for('.index'))
def index_post(form): user = g.current_user pilot_id = form.pilot.data if form.pilot.data != 0 else None pilot = pilot_id and User.get(int(pilot_id)) pilot_id = pilot and pilot.id club_id = (pilot and pilot.club_id) or user.club_id flights = [] success = False for name, f in IterateUploadFiles(form.file.raw_data): filename = files.sanitise_filename(name) filename = files.add_file(filename, f) # check if the file already exists with files.open_file(filename) as f: md5 = file_md5(f) other = Flight.by_md5(md5) if other: files.delete_file(filename) flights.append((name, other, _('Duplicate file'))) continue igc_file = IGCFile() igc_file.owner = user igc_file.filename = filename igc_file.md5 = md5 igc_file.update_igc_headers() if igc_file.date_utc is None: files.delete_file(filename) flights.append((name, None, _('Date missing in IGC file'))) continue flight = Flight() flight.pilot_id = pilot_id flight.pilot_name = form.pilot_name.data if form.pilot_name.data else None flight.club_id = club_id flight.igc_file = igc_file flight.model_id = igc_file.guess_model() if igc_file.registration: flight.registration = igc_file.registration else: flight.registration = igc_file.guess_registration() flight.competition_id = igc_file.competition_id if not analyse_flight(flight): files.delete_file(filename) flights.append((name, None, _('Failed to parse file'))) continue if not flight.takeoff_time or not flight.landing_time: files.delete_file(filename) flights.append((name, None, _('No flight found in file'))) continue if not flight.update_flight_path(): files.delete_file(filename) flights.append((name, None, _('No flight found in file'))) continue flights.append((name, flight, None)) db.session.add(igc_file) db.session.add(flight) create_flight_notifications(flight) # flush data to make sure we don't get duplicate files from ZIP files db.session.flush() success = True db.session.flush() unlock_user_achievements(user, UPLOAD_ACHIEVEMENTS) db.session.commit() try: for flight in flights: if flight[2] is None: tasks.analyse_flight.delay(flight[1].id) except ConnectionError: current_app.logger.info('Cannot connect to Redis server') def ModelSelectField(*args, **kwargs): return AircraftModelSelectField().bind(None, 'model', *args, **kwargs)() return render_template('upload/result.jinja', flights=flights, success=success, ModelSelectField=ModelSelectField)