def add(): form = TripForm() if form.validate_on_submit(): url = form.url.data description = form.description.data destination = form.destination.data outbound_date = form.outbound_date.data outbound_time = form.outbound_time.data inbound_date = form.inbound_date.data inbound_time = form.inbound_time.data tags = form.tags.data bm = Trip(user=current_user, url=url, description=description, destination=destination, tags=tags, outbound_date=outbound_date, outbound_time=outbound_time, inbound_date=inbound_date, inbound_time=inbound_time) db.session.add(bm) db.session.commit() flash("Stored trip '{}'".format(description)) return redirect(url_for('index')) return render_template('trip_form.html', form=form, title="Add a Trip")
def addtrip(): error = None tripForm = TripForm() tripForm.trip_country.choices = [(a.countryName, a.countryName) for a in Country.query.all()] tripForm.trip_city.choices = [(a.cityName, a.cityName) for a in City.query.all()] if request.method == 'POST': if tripForm.validate_on_submit(): now_loc = img_folder + str(current_user.id) #if directory is not yet created this function will create it if os.path.isdir(now_loc) == False: os.makedirs(now_loc) if tripForm.file.data and allowed_file( tripForm.file.data.filename): filename = secure_filename(tripForm.file.data.filename) tripForm.file.data.save(os.path.join(now_loc + '/', filename)) uploadFolder = now_loc + '/' #the renaming process of the image nameNow = str(int(time.time())) + '.' + str( os.path.splitext(filename)[1][1:]) #saving the changes os.rename(uploadFolder + filename, uploadFolder + nameNow) #this is the compressor part, this will optimize the image #and will decrease its file size but not losing that much quality img = Image.open(open(str(uploadFolder + nameNow), 'rb')) img.save(str(uploadFolder + nameNow), quality=90, optimize=True) tripform = Trips(tripName=tripForm.trip_name.data, tripDateFrom=tripForm.trip_date_from.data, tripDateTo=tripForm.trip_date_to.data, userID=current_user.id, tripCountry=tripForm.trip_country.data, tripCity=tripForm.trip_city.data, status=1, visibility=tripForm.trip_visibility.data, img_thumbnail=nameNow, featuredTrip=0) db.session.add(tripform) db.session.commit() return redirect(url_for('trip_blueprint.trips')) ph = Photos.query.filter_by(id=current_user.profile_pic).first() if ph is None: cas = 'default' else: cas = ph.photoName return render_template('addtrip.html', form=tripForm, error=error, csID=str(current_user.id), csPic=str(cas))
def edit_trip(trip_id): trip = Trip.query.get_or_404(trip_id) if current_user != trip.user: abort(403) form = TripForm(obj=trip) if form.validate_on_submit(): form.populate_obj(trip) db.session.commit() flash("Stored '{}'".format(trip.description)) return redirect(url_for('user', username=current_user.username)) return render_template('trip_form.html', form=form, title="Edit Trip")
def addtrip(): error = None tripForm = TripForm() tripForm.trip_country.choices = [(a.countryName, a.countryName) for a in Country.query] tripForm.trip_city.choices = [(a.cityName, a.cityName) for a in City.query] if request.method == 'POST': if tripForm.validate_on_submit(): tripform = Trips(tripName=tripForm.trip_name.data, tripDateFrom=tripForm.trip_date_from.data, tripDateTo=tripForm.trip_date_to.data, userID=current_user.id, tripCountry=tripForm.trip_country.data, tripCity=tripForm.trip_city.data, status=tripForm.trip_status.data, visibility=tripForm.trip_visibility.data, img_thumbnail=tripForm.file.data.filename) db.session.add(tripform) db.session.commit() if tripForm.file.data and allowed_file( tripForm.file.data.filename): filename = secure_filename(tripForm.file.data.filename) tripForm.file.data.save( os.path.join(img_folder + 'trips/', filename)) #ex = os.path.splitext(filename)[1][1:] #st = img_folder+'trips/'+filename #img = Image.open(open(str(st), 'rb')) #img.save(str(st), format=None, quality=50) return redirect(url_for('trip_blueprint.addtrip')) ph = Photos.query.filter_by(id=current_user.profile_pic).first() if ph is None: cas = 'default' else: cas = ph.photoName return render_template('addtrip.html', form=tripForm, error=error, csID=str(current_user.id), csPic=str(cas))
def edittrips(tripName): trips = Trips.query.filter_by(tripName=tripName).first() result = Trips.query.all() form = TripForm() if form.validate_on_submit(): trips.tripName = form.tripName.data trips.tripDateFrom = form.tripDateFrom.data trips.tripDateTo = form.tripDateTo.data trips.id = form.id.data trips.viewsNumber = form.viewsNumber.data trips.img_thumbnail = form.img_thumbnail.data db.session.add(trips) db.session.commit() flash("Your changes have been saved.") return render_template('admin/trips.html', result=result) else: form.tripName.data = trips.tripName form.tripDateFrom.data = trips.tripDateFrom form.tripDateTo.data = trips.tripDateTo form.id.data = trips.id form.viewsNumber.data = trips.viewsNumber form.img_thumbnail.data = trips.img_thumbnail return render_template('admin/edittrips.html', trips=trips, form=form)
def addtrip(): error = None tripForm = TripForm() if request.method == 'POST': if tripForm.validate_on_submit(): tripform = Trips(tripName=tripForm.trip_name.data, tripDateFrom=tripForm.trip_date_from.data, tripDateTo=tripForm.trip_date_to.data, userID=current_user.id, img_thumbnail=tripForm.file.data.filename) db.session.add(tripform) db.session.commit() if tripForm.file.data and allowed_file(tripForm.file.data.filename): filename = secure_filename(tripForm.file.data.filename) tripForm.file.data.save(os.path.join(img_folder+'trips/', filename)) ex = os.path.splitext(filename)[1][1:] st = img_folder+'trips/'+filename img = Image.open(open(str(st), 'rb')) img.save(str(st), format=None, quality=50) return redirect(url_for('trip_blueprint.addtrip')) return render_template('addtrip.html', form=tripForm, error=error)