Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
paolini committed Oct 27, 2023
2 parents 60667d5 + 718ab25 commit 650b6da
Show file tree
Hide file tree
Showing 11 changed files with 308 additions and 21 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dm-manager",
"version": "1.3.5",
"version": "1.3.6",
"private": true,
"dependencies": {
"@fortawesome/fontawesome-svg-core": "^6.1.2",
Expand Down
8 changes: 8 additions & 0 deletions server/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const visitsQuery = require('./controllers/public/visits')
const seminarsQuery = require('./controllers/public/seminars')
const seminarQuery = require('./controllers/public/seminar')
const coursesQuery = require('./controllers/public/courses')
const lessonsQuery = require('./controllers/public/lessons')
const processes = require('./processes.js')

const router = express.Router()

Expand Down Expand Up @@ -82,5 +84,11 @@ router.get('/public/courses', async (req, res) => {
res.send(await coursesQuery(req))
})

router.get('/public/lessons', async (req, res) => {
res.send(await lessonsQuery(req))
})

router.use('/process', processes)

module.exports = router
module.exports.ModelSchemas = ModelSchemas
33 changes: 26 additions & 7 deletions server/controllers/public/courses.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,35 @@ const EventPhdCourse = require('../../models/EventPhdCourse')

async function coursesQuery(req) {
const pipeline = [
{ $unwind: '$lessons'},
{ $lookup: {
from: 'conferencerooms',
localField: 'lessons.conferenceRoom',
as: 'lessons.conferenceRoom',
foreignField: '_id'
}},
{ $lookup: {
from: 'people',
localField: 'lecturers',
foreignField: '_id',
as: 'lecturers',
}},
{ $project: {
_id: 1,
speaker: 1,
lecturer: 1,
lecturers: {
firstName: 1,
lastName: 1,
},
title: 1,
lessons: 1,
}},
]

// console.log(JSON.stringify({pipeline}))
lessons: {
date: 1,
duration: 1,
conferenceRoom: {
name: 1
}
}
}}
];

const courses = await EventPhdCourse.aggregate(pipeline)

Expand Down
58 changes: 58 additions & 0 deletions server/controllers/public/lessons.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const EventPhdCourse = require('../../models/EventPhdCourse')

async function lessonsQuery(req) {
const pipeline = [
{$unwind: '$lessons'},
{$project: {
date: '$lessons.date',
duration: '$lessons.duration',
lecturers: '$lessons.lecturers',
conferenceRoom: '$lessons.conferenceRoom',
course: {
_id: '$_id',
title: '$title',
description: '$description',
lecturers: '$lecturers',
}
}},
{$project: {
_id: 0,
}},
{$lookup: {
from: 'conferencerooms',
localField: 'conferenceRoom',
as: 'conferenceRoom',
foreignField: '_id',
pipeline: [
{$project: {
_id: 0,
name: 1,
}}
]
}},
{$unwind: {
path: '$conferenceRoom',
preserveNullAndEmptyArrays: true
}
},
{$lookup: {
from: 'people',
localField: 'course.lecturers',
as: 'course.lecturers',
foreignField: '_id',
pipeline: [
{$project: {
_id: 0,
firstName: 1,
lastName: 1,
}}
]
}}
];

const courses = await EventPhdCourse.aggregate(pipeline)

return courses
}

module.exports = lessonsQuery
127 changes: 116 additions & 11 deletions server/migrations.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
const { default: axios } = require('axios')
const he = require('he')
const assert = require('assert')
const jsdom = require('jsdom')

async function findPerson(people, firstName, lastName, affiliazione) {
let p = await people.findOne({ firstName, lastName })
Expand Down Expand Up @@ -561,7 +562,7 @@ const migrations = {
return true
},

D20231013_copy_events_15: async function(db) {
D20231027_update_events_1: async function(db) {
const people = db.collection('people')
const conferences = db.collection('eventconferences')
const seminars = db.collection('eventseminars')
Expand Down Expand Up @@ -602,34 +603,52 @@ const migrations = {
}

// await people.deleteMany({created_by_migration: true})
await conferences.deleteMany({})
await seminars.deleteMany({})
// await conferences.deleteMany({})
// await seminars.deleteMany({})

var offset = 0;
const batch_size = 97;

const alreadyLoaded = {}
var res = await axios.get(`https://www.dm.unipi.it/wp-json/wp/v2/unipievents?per_page=${batch_size}&offset=0`)
while (res.data.length > 0) {
const events = res.data
for (const event of events) {
const taxonomy = event.unipievents_taxonomy
const title = he.decode(event.title.rendered)
const conferenceRoom = room_mapping[event.unipievents_place]
let conferenceRoom = room_mapping[event.unipievents_place]
const startDatetime = toUTCDate(event.unipievents_startdate)
const duration = (event.unipievents_enddate - event.unipievents_startdate) / 60
const notes = event.content.rendered
const abstract = event.content.rendered
const oldUrl = event.link

let found = await conferences.findOne({ oldUrl })

if (found) {
console.log(`Already loaded ${oldUrl} as conference ${found._id}. Skipping!`)
continue
}

found = await seminars.findOne({ oldUrl })

if (found) {
console.log(`Already loaded ${oldUrl} as seminar ${found._id}. Skipping!`)
continue
}


if (event.unipievents_place && !conferenceRoom) {
console.log("**************** Cannot find room: ${event.unipievents_place}")
console.log("conferenceRoom", JSON.stringify({conferenceRoom, unipi_place: event.unipievents_place}))
const res = await conferencerooms.insertOne({
name: event.unipievents_place,
})
conferenceRoom = res.insertedId
console.log(`**** creatd new room: ${event.unipievents_place}`)
}

if (taxonomy.includes(90)) {
console.log("> Conference", event.link)


const object = {
title,
startDate: toUTCDate(event.unipievents_startdate),
Expand Down Expand Up @@ -693,10 +712,8 @@ const migrations = {
abstract: notes,
}
// console.log(object)
if (alreadyLoaded[object.title] === undefined) {
alreadyLoaded[object.title] = true
await seminars.insertOne(object)
}
const res = await seminars.insertOne(object)
console.log(`inserted seminar ${res.insertedId}`)
}

}
Expand All @@ -707,6 +724,94 @@ const migrations = {

return true
},

D20231027_save_old_abstract_1: async function(db) {
const seminars = db.collection('eventseminars')
for(const seminar of await seminars.find({}).toArray()) {
await seminars.updateOne({ _id: seminar._id },
{ $set: { oldAbstract: seminar.abstract } })
}
return true
},

D20231027_save_conference_old_abstract_1: async function(db) {
const conferences = db.collection('eventconferences')
for(const conference of await conferences.find({}).toArray()) {
await conferences.updateOne({ _id: conference._id },
{ $set: { oldNotes: conference.notes } })
}
return true
},

D20231028_convert_html_to_markdown_1: async function(db) {
const seminars = db.collection('eventseminars')
const conferences = db.collection('eventconferences')
const invalid_tags = []

function parseHTML(html) {
const dom = new jsdom.JSDOM(html, 'text/html')
const doc = dom.window.document
let out = parseElement(doc.body).trim()
out = out.replace('\n\n\n', '\n\n')
out = out.replace('\n\n\n', '\n\n')
out = out.replace('\n\n\n', '\n\n')
return out
}

function parseElement(el) {
const nodeName = el.nodeName.toLowerCase()
if (nodeName === '#text') return el.textContent
const children = [...el.childNodes].map((child, i) =>
parseElement(child)).join('')
switch (nodeName) {
case 'body':
return children
case 'h4':
return `#### ${children}`
case 'h3':
return `### ${children}`
case 'h2':
return `## ${children}`
case 'h1':
return `# ${children}`
case 'p':
return `${children}\n`
case 'br':
return `\n\n`
case 'em':
case 'i':
return `*${children}*`
case 'a':
return `[${children}](${el.href})`
case 'strong':
case 'b':
return `**${children}**`
case 'ul':
case 'ol':
return `${children}\n`
case 'li':
return `* ${children}\n`
case 'pre':
return `\`\`\`\n${children}\n\`\`\`\n`
case 'img':
return `![${el.alt}](${el.src})`
default:
// console.log(`unexpected node ${nodeName}`)
if (!invalid_tags.includes(nodeName)) invalid_tags.push(nodeName)
return children
}
}

for(const seminar of await seminars.find({}).toArray()) {
const abstract = seminar.oldAbstract || ''
const parsed = parseHTML(abstract)
await seminars.updateOne({ _id: seminar._id },
{ $set: { abstract: parsed } })
}

console.log(`invalid tags:`, invalid_tags.join(', '))
return true
},
}

async function migrate(db, options) {
Expand Down
1 change: 1 addition & 0 deletions server/models/EventConference.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const eventConferenceSchema = new Schema({
conferenceRoom: { type: ObjectId, label: 'Aula', ref: 'ConferenceRoom', required: true },
grants: [{type: ObjectId, label: 'grants', ref: 'Grant'}],
notes,
oldNotes: { type: String, label: 'note vecchie', widget: 'text'},

createdBy,
updatedBy,
Expand Down
3 changes: 2 additions & 1 deletion server/models/EventSeminar.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ const eventSeminarSchema = new Schema({
duration: { type: Number, label: 'Durata (in minuti)', default: 60 },
category: { type: ObjectId, label: 'Ciclo di Seminari', ref: 'SeminarCategory', required: true },
grants: [{type: ObjectId, label: 'grants', ref: 'Grant'}],
abstract: { type: String, label: 'Abstract', widget: 'text' },
abstract: { type: String, label: 'Abstract (non modificare!)', widget: 'text' },
oldUrl: { type: String, label: 'URL vecchio', widget: 'url' },
oldAbstract: { type: String, label: 'Abstract vecchio', widget: 'text' },

createdBy,
updatedBy,
Expand Down
10 changes: 10 additions & 0 deletions server/processes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Custom processes to insert data from end-users.
const express = require('express')

const router = express.Router()

router.get('/', function (req, res) {
res.send('ok')
})

module.exports = router
2 changes: 1 addition & 1 deletion server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ if (config.OAUTH2_CLIENT_ID) {
function setup_routes(app) {
app.use(cors(
{
origin: config.CORS_ORIGIN.split(","),
origin: config.CORS_ORIGIN,
optionsSuccessStatus: 200,
credentials: true // Needed for the client to handle session
}))
Expand Down
2 changes: 2 additions & 0 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import Models from './models/Models'
import { Container } from 'react-bootstrap'
import {QueryClient, QueryClientProvider } from 'react-query'
import FormFillPage from './pages/FormFillPage'
import AddSeminar from './processes/AddSeminar';


console.log("dm-manager (app starting)")
Expand Down Expand Up @@ -55,6 +56,7 @@ function Internal() {
<Routes>
<Route path="/" element={<Home />} />
<Route path="/profile" element={<Profile />} />
<Route path="/process/seminar/add" element={<AddSeminar/>}></Route>
{ Object.values(Models).map(x => x.routers()) }
<Route path="/map" element={<Map />} />
<Route path="*" element={<NotFound />} />
Expand Down
Loading

0 comments on commit 650b6da

Please sign in to comment.