Example #1
0
def Offer(establishment_id=None):
    form=AddOfferForm()
    from models import Establishment

    if(establishment_id==None):
        all_my_establishments = Establishment.query.filter_by(user_id=current_user.id).all()

        if(not all_my_establishments):
            flash('Please add establishment detail before its offers')
            return redirect(url_for('service_provider.Establishment'))
        form.establsihment.choices = [(establishment.id,establishment.establishment_name) for establishment in all_my_establishments]
    else:

        my_establishment = Establishment.query.filter_by(id=establishment_id).first()
        if (not my_establishment):
            flash('Please add establishment detail before its offers')
            return redirect(url_for('service_provider.Establishment'))
        form.establsihment.choices = [(establishment_id,my_establishment.establishment_name)]

    if form.validate_on_submit():
        from models import Offer, sqlalchemy_obj
        from werkzeug import secure_filename
        import os
        from manage import my_app_obj

        print "Your establishment code is " + str(form.establsihment.data)

        new_offer_obj = Offer(offer_description=form.offer_description.data, validity=form.validity.data,
                              establishments_id=form.establsihment.data, conditions=form.conditions.data, user_id=current_user.id)

        sqlalchemy_obj.session.add(new_offer_obj)
        sqlalchemy_obj.session.commit()

        file_path_string_comma_seperated = ""
        #http://stackoverflow.com/questions/23706370/wtforms-multi-selection-file-upload
        picture_count = 0
        for a_picture in request.files.getlist("offer_picture"):
            #uploaded_files = request.files.getlist("offer_picture[]")

            ###############################################Uploading pictures
            print "Uploading " + a_picture.filename
            file_name = secure_filename(a_picture.filename)

            picture_storage_path = os.path.join(os.getcwd(),'static','gallery' , str(form.establsihment.data), str(new_offer_obj.id))

            file_storage_path = "/".join(['gallery' , str(form.establsihment.data), str(new_offer_obj.id)])
            print "Path of file storage is " + file_storage_path

            if not os.path.exists(picture_storage_path):
                os.makedirs(picture_storage_path)

            file_path_with_name =  os.path.join(os.getcwd(),'static','gallery' , str(form.establsihment.data), str(new_offer_obj.id), file_name)

            if (picture_count==0):
                file_path_string_comma_seperated = str(file_storage_path+"/" + file_name)
                picture_count = picture_count + 1
            else:
                file_path_string_comma_seperated = file_path_string_comma_seperated + "," + str(file_storage_path+"/" + file_name)


            print "Complete path is " + file_path_with_name
            a_picture.save(file_path_with_name)
            image_size(file_path_with_name)
            ###############################################Uploading pictures####################
        new_offer_obj.offer_pictures_location = file_path_string_comma_seperated
        sqlalchemy_obj.session.commit()

        flash("Your offer is added, congratulations - Let's see customers now")
        return redirect(url_for('service_provider.index'))

    return render_template('service_provider/edit_offer.html', form=form)