add place_id on add/edit event, fix media update

This commit is contained in:
lesion 2022-05-06 23:02:43 +02:00
parent 82ae4b1be9
commit 5bce9cf289
No known key found for this signature in database
GPG key ID: 352918250B012177
3 changed files with 33 additions and 17 deletions

View file

@ -75,6 +75,7 @@ export default {
if (typeof p === 'object' && !p.create) { if (typeof p === 'object' && !p.create) {
this.place.name = p.name.trim() this.place.name = p.name.trim()
this.place.address = p.address this.place.address = p.address
this.place.id = p.id
this.disableAddress = true this.disableAddress = true
} else { // this is a new place } else { // this is a new place
this.place.name = p.name || p this.place.name = p.name || p
@ -83,9 +84,11 @@ export default {
const place = this.places.find(p => p.name.toLowerCase() === tmpPlace) const place = this.places.find(p => p.name.toLowerCase() === tmpPlace)
if (place) { if (place) {
this.place.name = place.name this.place.name = place.name
this.place.id = place.id
this.place.address = place.address this.place.address = place.address
this.disableAddress = true this.disableAddress = true
} else { } else {
delete this.place.id
this.place.address = '' this.place.address = ''
this.disableAddress = false this.disableAddress = false
this.$refs.place.blur() this.$refs.place.blur()

View file

@ -187,12 +187,15 @@ export default {
if (this.event.media.length) { if (this.event.media.length) {
formData.append('image', this.event.media[0].image) formData.append('image', this.event.media[0].image)
formData.append('image_url', this.event.media[0].url) // formData.append('image_url', this.event.media[0].url)
formData.append('image_name', this.event.media[0].name) formData.append('image_name', this.event.media[0].name)
formData.append('image_focalpoint', this.event.media[0].focalpoint) formData.append('image_focalpoint', this.event.media[0].focalpoint)
} }
formData.append('title', this.event.title) formData.append('title', this.event.title)
if (this.event.place.id) {
formData.append('place_id', this.event.place.id)
}
formData.append('place_name', this.event.place.name) formData.append('place_name', this.event.place.name)
formData.append('place_address', this.event.place.address) formData.append('place_address', this.event.place.address)
formData.append('description', this.event.description) formData.append('description', this.event.description)

View file

@ -290,7 +290,7 @@ const eventController = {
}, },
async isAnonEventAllowed (_req, res, next) { async isAnonEventAllowed (_req, res, next) {
if (!res.locals.settings.allow_anon_event) { if (!res.locals.settings.allow_anon_event && !res.locals.user) {
return res.sendStatus(403) return res.sendStatus(403)
} }
next() next()
@ -307,11 +307,28 @@ const eventController = {
const body = req.body const body = req.body
const recurrent = body.recurrent ? JSON.parse(body.recurrent) : null const recurrent = body.recurrent ? JSON.parse(body.recurrent) : null
const required_fields = [ 'title', 'place_name', 'start_datetime'] const required_fields = [ 'title', 'start_datetime']
const missing_field = required_fields.find(required_field => !body[required_field]) let missing_field = required_fields.find(required_field => !body[required_field])
if (missing_field) { if (missing_field) {
log.warn(`${missing_field} is required`) log.warn(`${missing_field} required`)
return res.status(400).send(`${missing_field} is required`) return res.status(400).send(`${missing_field} required`)
}
// find or create the place
let place
if (body.place_id) {
place = await Place.findByPk(body.place_id)
} else {
place = await Place.findOne({ where: { name: body.place_name.trim() }})
if (!place) {
if (!body.place_address || !body.place_name) {
return res.status(400).send(`place_id or place_name and place_address required`)
}
place = await Place.create({
name: body.place_name,
address: body.place_address
})
}
} }
const eventDetails = { const eventDetails = {
@ -347,13 +364,6 @@ const eventController = {
let event = await Event.create(eventDetails) let event = await Event.create(eventDetails)
const [place] = await Place.findOrCreate({
where: { name: body.place_name },
defaults: {
address: body.place_address
}
})
await event.setPlace(place) await event.setPlace(place)
// create/assign tags // create/assign tags
@ -427,8 +437,9 @@ const eventController = {
} }
} }
if (req.file || body.image_url) { // modify associated media only if a new file is uploaded or remote image_url is used
if (body.image_url && /^https?:\/\//.test(body.image_url)) { if (req.file || (body.image_url && /^https?:\/\//.test(body.image_url))) {
if (body.image_url) {
req.file = await helpers.getImageFromURL(body.image_url) req.file = await helpers.getImageFromURL(body.image_url)
} }
@ -441,10 +452,9 @@ const eventController = {
name: body.image_name || body.title || '', name: body.image_name || body.title || '',
focalpoint: [parseFloat(focalpoint[0].slice(0, 6)), parseFloat(focalpoint[1].slice(0, 6))] focalpoint: [parseFloat(focalpoint[0].slice(0, 6)), parseFloat(focalpoint[1].slice(0, 6))]
}] }]
} else { } else if (!body.image) {
eventDetails.media = [] eventDetails.media = []
} }
await event.update(eventDetails) await event.update(eventDetails)
const [place] = await Place.findOrCreate({ const [place] = await Place.findOrCreate({
where: { name: body.place_name }, where: { name: body.place_name },