Skip to content

Commit

Permalink
Merge pull request #16 from PnX-SI/amine
Browse files Browse the repository at this point in the history
add gallery
  • Loading branch information
HamoudaAmine authored Dec 12, 2018
2 parents 35d8c44 + 6298f81 commit e3a7f0c
Show file tree
Hide file tree
Showing 29 changed files with 742 additions and 366 deletions.
80 changes: 64 additions & 16 deletions backend/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,25 @@
users_schema = user_models.usersViewSchema(many=True)
corThemeStheme_Schema = models.CorThemeSthemeSchema(many=True)
themes_sthemes_schema = models.CorSthemeThemeSchema(many=True)
ville_schema = models.VilleSchema(many=True)


@api.route('/api/sites', methods=['GET'])
def returnAllSites():
get_all_sites = models.TSite.query.all()
get_all_sites = models.TSite.query.order_by('name_site').all()
sites = site_schema.dump(get_all_sites).data
for site in sites:
get_photo = models.TPhoto.query.filter_by(
id_photo=site.get('t_photos')[0])
main_photo = photo_schema.dump(get_photo).data
get_main_photo = models.TPhoto.query.filter_by(
id_photo=site.get('main_photo'))
'''
get_photos = models.TPhoto.query.filter(
models.TPhoto.id_photo.in_(site.get('t_photos')))
dump_photos = photo_schema.dump(get_photos).data
for photo in dump_photos :
photo['sm'] = utils.getThumbnail(photo).get('output_name'),
site['photos'] = dump_photos
'''
main_photo = photo_schema.dump(get_main_photo).data
site['main_photo'] = utils.getThumbnail(
main_photo[0]).get('output_name')
return jsonify(sites), 200
Expand All @@ -39,8 +49,8 @@ def returnAllSites():
def returnSiteById(id_site):
get_site_by_id = models.TSite.query.filter_by(id_site=id_site)
site = site_schema.dump(get_site_by_id).data
get_photos_by_site = models.TPhoto.query.filter_by(id_site=id_site)
photos = photo_schema.dump(get_photos_by_site).data
get_photos_by_site = models.TPhoto.query.order_by('filter_date').filter_by(id_site=id_site).all()
dump_photos = photo_schema.dump(get_photos_by_site).data

cor_sthemes_themes = site[0].get('cor_site_stheme_themes')
cor_list = []
Expand All @@ -61,18 +71,21 @@ def returnSiteById(id_site):
site[0]['themes'] = themes_list
site[0]['subthemes'] = subthemes_list

def getPhoto(photo):
return {
'id': photo.get('id_photo'),
'sm': url_for('static', filename=DATA_IMAGES_PATH + utils.getThumbnail(photo).get('output_name')),
'md': url_for('static', filename=DATA_IMAGES_PATH + utils.getMedium(photo).get('output_name')),
'lg': url_for('static', filename=DATA_IMAGES_PATH + utils.getLarge(photo).get('output_name')),
'date': photo.get('date_photo')
}
for photo in dump_photos :
photo['sm'] = utils.getThumbnail(photo).get('output_name'),

photos = [getPhoto(photo) for photo in photos]
photos = dump_photos
return jsonify(site=site, photos=photos), 200

@api.route('/api/gallery')
def gallery():

get_photos = models.TPhoto.query.order_by('id_site').all()
dump_photos = photo_schema.dump(get_photos).data
for photo in dump_photos :
photo['sm'] = utils.getThumbnail(photo).get('output_name'),
return jsonify(dump_photos), 200

@api.route('/api/themes', methods=['GET'])
def returnAllThemes():
try:
Expand Down Expand Up @@ -153,7 +166,6 @@ def add_site():
@fnauth.check_auth(6, False, None, None)
def update_site():
site = request.get_json()
print('site',site)
models.CorSiteSthemeTheme.query.filter_by(id_site=site.get('id_site')).delete()
models.TSite.query.filter_by(id_site= site.get('id_site')).update(site)
db.session.commit()
Expand Down Expand Up @@ -199,6 +211,30 @@ def upload_file():
return jsonify('photo added successfully'), 200


@api.route('/api/updatePhoto', methods=['PATCH'])
@fnauth.check_auth(6, False, None, None)
def update_photo():
base_path = './static/' + DATA_IMAGES_PATH
data = request.form.get('data')
image = request.files.get('image')
data_serialized = json.loads(data)
photos_query = models.TPhoto.query.filter_by(id_photo=data_serialized.get('id_photo')).all()
photo_name = photo_schema.dump(photos_query).data[0].get('path_file_photo')
print('data_serialized', data_serialized)
if (data_serialized.get('main_photo') == True):
models.TSite.query.filter_by(id_site= data_serialized.get('id_site')).update({models.TSite.main_photo: data_serialized.get('id_photo')})
if (data_serialized.get('main_photo')):
del data_serialized['main_photo']
models.TPhoto.query.filter_by(id_photo = data_serialized.get('id_photo')).update(data_serialized)
db.session.commit()
if (image):
for fileName in os.listdir(base_path):
if fileName.endswith(photo_name):
os.remove(base_path + fileName)
image.save(os.path.join(base_path + image.filename))
return jsonify('photo added successfully'), 200


@api.route('/api/deletePhotos', methods=['POST'])
@fnauth.check_auth(6, False, None, None)
def deletePhotos():
Expand All @@ -215,3 +251,15 @@ def deletePhotos():
if fileName.endswith(photo_name):
os.remove(base_path + fileName)
return jsonify('site has been deleted'), 200




@api.route('/api/villes', methods=['GET'])
def returnAllville():
try:
get_all_ville = models.Ville.query.all()
ville= ville_schema.dump(get_all_ville).data
return jsonify(ville), 200
except Exception as exception:
return jsonify(error=exception), 400
144 changes: 109 additions & 35 deletions backend/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
db = SQLAlchemy()
ma = Marshmallow()


class TSite(db.Model):
__tablename__ = 't_site'
__table_args__ = {'schema': 'geopaysages'}

id_site = db.Column(db.Integer, primary_key=True, server_default=db.FetchedValue())
id_site = db.Column(db.Integer, primary_key=True,
server_default=db.FetchedValue())
name_site = db.Column(db.String)
desc_site = db.Column(db.String)
legend_site = db.Column(db.String)
Expand All @@ -25,38 +27,49 @@ class TSite(db.Model):
path_file_guide_site = db.Column(db.String(1))
publish_site = db.Column(db.Boolean)
geom = db.Column(Geometry(geometry_type='POINT', srid=4326))

main_photo = db.Column(db.Integer)


class CorSiteSthemeTheme(db.Model):
__tablename__ = 'cor_site_stheme_theme'
__table_args__ = {'schema': 'geopaysages'}

id_site_stheme_theme = db.Column(db.Integer, nullable=False, server_default=db.FetchedValue())
id_site = db.Column(db.ForeignKey('geopaysages.t_site.id_site'), primary_key=True, nullable=False)
id_stheme_theme = db.Column(db.ForeignKey('geopaysages.cor_stheme_theme.id_stheme_theme'), primary_key=True, nullable=False)
id_site_stheme_theme = db.Column(
db.Integer, nullable=False, server_default=db.FetchedValue())
id_site = db.Column(db.ForeignKey(
'geopaysages.t_site.id_site'), primary_key=True, nullable=False)
id_stheme_theme = db.Column(db.ForeignKey(
'geopaysages.cor_stheme_theme.id_stheme_theme'), primary_key=True, nullable=False)

t_site = db.relationship('TSite', primaryjoin='CorSiteSthemeTheme.id_site == TSite.id_site', backref='cor_site_stheme_themes')
cor_stheme_theme = db.relationship('CorSthemeTheme', primaryjoin='CorSiteSthemeTheme.id_stheme_theme == CorSthemeTheme.id_stheme_theme', backref='cor_site_stheme_themes')
t_site = db.relationship(
'TSite', primaryjoin='CorSiteSthemeTheme.id_site == TSite.id_site', backref='cor_site_stheme_themes')
cor_stheme_theme = db.relationship(
'CorSthemeTheme', primaryjoin='CorSiteSthemeTheme.id_stheme_theme == CorSthemeTheme.id_stheme_theme', backref='cor_site_stheme_themes')


class CorSthemeTheme(db.Model):
__tablename__ = 'cor_stheme_theme'
__table_args__ = {'schema': 'geopaysages'}

id_stheme_theme = db.Column(db.Integer, nullable=False, unique=True, server_default=db.FetchedValue())
id_stheme = db.Column(db.ForeignKey('geopaysages.dico_stheme.id_stheme'), primary_key=True, nullable=False)
id_theme = db.Column(db.ForeignKey('geopaysages.dico_theme.id_theme'), primary_key=True, nullable=False)
id_stheme_theme = db.Column(
db.Integer, nullable=False, unique=True, server_default=db.FetchedValue())
id_stheme = db.Column(db.ForeignKey(
'geopaysages.dico_stheme.id_stheme'), primary_key=True, nullable=False)
id_theme = db.Column(db.ForeignKey(
'geopaysages.dico_theme.id_theme'), primary_key=True, nullable=False)

dico_stheme = db.relationship('DicoStheme', primaryjoin='CorSthemeTheme.id_stheme == DicoStheme.id_stheme', backref='cor_stheme_themes')
dico_theme = db.relationship('DicoTheme', primaryjoin='CorSthemeTheme.id_theme == DicoTheme.id_theme', backref='cor_stheme_themes')
dico_stheme = db.relationship(
'DicoStheme', primaryjoin='CorSthemeTheme.id_stheme == DicoStheme.id_stheme', backref='cor_stheme_themes')
dico_theme = db.relationship(
'DicoTheme', primaryjoin='CorSthemeTheme.id_theme == DicoTheme.id_theme', backref='cor_stheme_themes')


class DicoLicencePhoto(db.Model):
__tablename__ = 'dico_licence_photo'
__table_args__ = {'schema': 'geopaysages'}

id_licence_photo = db.Column(db.Integer, primary_key=True, server_default=db.FetchedValue())
id_licence_photo = db.Column(
db.Integer, primary_key=True, server_default=db.FetchedValue())
name_licence_photo = db.Column(db.String)
description_licence_photo = db.Column(db.String)

Expand All @@ -65,34 +78,39 @@ class DicoStheme(db.Model):
__tablename__ = 'dico_stheme'
__table_args__ = {'schema': 'geopaysages'}

id_stheme = db.Column(db.Integer, primary_key=True, server_default=db.FetchedValue())
id_stheme = db.Column(db.Integer, primary_key=True,
server_default=db.FetchedValue())
name_stheme = db.Column(db.String)


class DicoTheme(db.Model):
__tablename__ = 'dico_theme'
__table_args__ = {'schema': 'geopaysages'}

id_theme = db.Column(db.Integer, primary_key=True, server_default=db.FetchedValue())
id_theme = db.Column(db.Integer, primary_key=True,
server_default=db.FetchedValue())
name_theme = db.Column(db.String)



class TRole(db.Model):
__tablename__ = 't_roles'
__table_args__ = {'schema': 'utilisateurs'}

groupe = db.Column(db.Boolean, nullable=False, server_default=db.FetchedValue())
id_role = db.Column(db.Integer, primary_key=True, server_default=db.FetchedValue())
groupe = db.Column(db.Boolean, nullable=False,
server_default=db.FetchedValue())
id_role = db.Column(db.Integer, primary_key=True,
server_default=db.FetchedValue())
identifiant = db.Column(db.String(100))
nom_role = db.Column(db.String(50))
prenom_role = db.Column(db.String(50))
desc_role = db.Column(db.Text)
_pass = db.Column('pass', db.String(100))
email = db.Column(db.String(250))
id_organisme = db.Column(db.ForeignKey('utilisateurs.bib_organismes.id_organisme', onupdate='CASCADE'))
id_organisme = db.Column(db.ForeignKey(
'utilisateurs.bib_organismes.id_organisme', onupdate='CASCADE'))
organisme = db.Column(db.String(32))
id_unite = db.Column(db.ForeignKey('utilisateurs.bib_unites.id_unite', onupdate='CASCADE'))
id_unite = db.Column(db.ForeignKey(
'utilisateurs.bib_unites.id_unite', onupdate='CASCADE'))
remarques = db.Column(db.Text)
pn = db.Column(db.Boolean)
session_appli = db.Column(db.String(50))
Expand All @@ -104,26 +122,65 @@ class VTest(db.Model):
__tablename__ = 'VTest'
__table_args__ = {'schema': 'geopaysages'}

id = db.Column(db.Integer,primary_key=True)
id = db.Column(db.Integer, primary_key=True)


class TPhoto(db.Model):
__tablename__ = 't_photo'
__table_args__ = {'schema': 'geopaysages'}

id_photo = db.Column(db.Integer, primary_key=True, server_default=db.FetchedValue())
id_photo = db.Column(db.Integer, primary_key=True,
server_default=db.FetchedValue())
id_site = db.Column(db.ForeignKey('geopaysages.t_site.id_site'))
path_file_photo = db.Column(db.String)
id_role = db.Column(db.ForeignKey('utilisateurs.t_roles.id_role'))
date_photo = db.Column(db.String)
filter_date = db.Column(db.Date)
legende_photo = db.Column(db.String)
display_gal_photo = db.Column(db.Boolean)
id_licence_photo = db.Column(db.ForeignKey('geopaysages.dico_licence_photo.id_licence_photo'))
id_licence_photo = db.Column(db.ForeignKey(
'geopaysages.dico_licence_photo.id_licence_photo'))

dico_licence_photo = db.relationship(
'DicoLicencePhoto', primaryjoin='TPhoto.id_licence_photo == DicoLicencePhoto.id_licence_photo', backref='t_photos')
t_role = db.relationship(
'TRole', primaryjoin='TPhoto.id_role == TRole.id_role', backref='t_photos')
t_site = db.relationship(
'TSite', primaryjoin='TPhoto.id_site == TSite.id_site', backref='t_photos')


class Ville(db.Model):
__tablename__ = 'villes_france'
__table_args__ = {'schema': 'geopaysages'}

ville_id = db.Column(db.Integer, primary_key=True,
server_default=db.FetchedValue())
ville_departement = db.Column(db.String)
ville_slug = db.Column(db.String)
ville_nom = db.Column(db.String)
ville_nom_simple = db.Column(db.String)
ville_nom_reel = db.Column(db.String)
ville_nom_soundex = db.Column(db.String)
ville_nom_metaphone = db.Column(db.String)
ville_code_postal = db.Column(db.String)
ville_commune = db.Column(db.String)
ville_code_commune = db.Column(db.String)
ville_arrondissement = db.Column(db.Integer)
ville_canton = db.Column(db.String)
ville_amdi = db.Column(db.Integer)
ville_population_2010 = db.Column(db.Integer)
ville_population_1999 = db.Column(db.Integer)
ville_population_2012 = db.Column(db.Integer)
ville_densite_2010 = db.Column(db.Integer)
ville_surface = db.Column(db.Integer)
ville_longitude_deg = db.Column(db.Integer)
ville_latitude_deg = db.Column(db.Integer)
ville_longitude_grd = db.Column(db.String)
ville_latitude_grd = db.Column(db.String)
ville_latitude_dms = db.Column(db.String)
ville_zmin = db.Column(db.Integer)
ville_zmax = db.Column(db.Integer)

dico_licence_photo = db.relationship('DicoLicencePhoto', primaryjoin='TPhoto.id_licence_photo == DicoLicencePhoto.id_licence_photo', backref='t_photos')
t_role = db.relationship('TRole', primaryjoin='TPhoto.id_role == TRole.id_role', backref='t_photos')
t_site = db.relationship('TSite', primaryjoin='TPhoto.id_site == TSite.id_site', backref='t_photos')

class GeographySerializationField(fields.String):
def _serialize(self, value, attr, obj):
Expand All @@ -145,37 +202,54 @@ def _deserialize(self, value, attr, data):
return None

#schemas#


class DicoThemeSchema(ma.ModelSchema):
class Meta:
fields = ('id_theme','name_theme')
fields = ('id_theme', 'name_theme')


class DicoSthemeSchema(ma.ModelSchema):
class Meta:
model = DicoStheme


class CorThemeSthemeSchema(ma.ModelSchema):
class Meta:
fields = ('id_stheme_theme',)


class LicencePhotoSchema(ma.ModelSchema):
class Meta:
fields =('id_licence_photo','name_licence_photo','description_licence_photo')

fields = ('id_licence_photo', 'name_licence_photo',
'description_licence_photo')


class TPhotoSchema(ma.ModelSchema):
dico_licence_photo = ma.Nested(LicencePhotoSchema)
dico_licence_photo = ma.Nested(LicencePhotoSchema)

class Meta:
model = TPhoto



class CorSthemeThemeSchema(ma.ModelSchema):
dico_theme = ma.Nested(DicoThemeSchema,only=["id_theme","name_theme"] )
dico_stheme = ma.Nested(DicoSthemeSchema,only=["id_stheme","name_stheme"] )
dico_theme = ma.Nested(DicoThemeSchema, only=["id_theme", "name_theme"])
dico_stheme = ma.Nested(DicoSthemeSchema, only=[
"id_stheme", "name_stheme"])

class Meta:
fields = ('dico_theme', 'dico_stheme')
#model = CorSthemeTheme



class TSiteSchema(ma.ModelSchema):
geom = GeographySerializationField(attribute='geom')

class Meta:
model = TSite



class VilleSchema(ma.ModelSchema):
class Meta:
fields = ('ville_nom',)

Loading

0 comments on commit e3a7f0c

Please sign in to comment.