-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp_database.js
More file actions
226 lines (214 loc) · 8.76 KB
/
Copy pathapp_database.js
File metadata and controls
226 lines (214 loc) · 8.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
databaseInit = function (app, i18n, upload, im, fs, cloudinary) {
//Function to create JSON from the schema
Array.prototype.contains = function (a) { for (i in this) { if (this[i] == a) return true; } return false; }
var objectPath = require("object-path");
//Mongoose setup
var mongoose = require('mongoose');
console.log("Connecting to " + process.env.MONGODB_URI)
mongoose.connect(process.env.MONGODB_URI);
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function () {
console.log("Connected to mongodb database")
});
//Mongoose Internationalization
var mongooseIntl = require('mongoose-intl');
var i18nPlugin = require('mongoose-i18n');
mongoose.plugin(i18nPlugin, { languages: ["en", "cr", "cs", "da", "nl", "et", "fi", "fr", "bg", "de", "el", "hu", "ga", "it", "lv", "lt", "mt", "pl", "pt", "ro", "sk", "sl", "es", "sv"], defaultLanguage: 'en' });
var Intlfields = ["bio_short", "bio_long", "title", "description", "role", "quote", "subtitle"]; //This is to keep track of translateable fields
//Mongoose schema definitions
//WARNING!!!!! DO NOT CALL ANY PROPERTY "TYPE"! IT WILL MESS UP WITH THE FORM CREATION FUNCTION!
var Schema = mongoose.Schema;
var personSchema = new Schema({
visibility: { type: Number, required: true }, //0=visible, 1= hidden, 2= non-referenced
name: { type: String, required: true },
fullname: { type: String },
gender: { type: Number, required: true }, //0=Male, 1 = Female, 2= Nonbinary
bio_short: { type: String, i18n: true },
bio_long: { type: String, i18n: true },
dateofbirth: { type: Date },
locationofbirth: { type: String },
dateofdeath: { type: Date },
locationofdeath: { type:String },
nationalities: [{ type: String }],
jobs:
[{
title: { type: String, i18n: true },
description: { type: String, i18n: true },
from: { type: Date },
to: { type: Date },
political: { type: Boolean }, //This mainly serves to calculate the political career length.
elected: { type: Boolean },
parlement: { type: String },
parlementplace: { type: Number }
}],
politicalmemberships:
[{
party: { type: String },
from: { type: Date },
to: { type: Date },
role: [{ type: String, i18n: true }]
}],
socialmedia_urls: [{ type: String }],
quotes:
[{
quote: { type: String, i18n: true },
location: { type: String },
date: { type: String },
source_urls: [{ type: String }],
context: { type: String }
}],
criminalrecord: [{
gravity: { type: Number }, //0=not too important, 5= really fucking bad
title: { type: String, i18n: true },
subtitle: { type: String, i18n: true },
timeline: {
title: { type: String, i18n: true },
date: { type: Date }
}
}],
positioning: {
soc: { type: Number },
eco: { type: Number },
eu: { type: Number },
auth: { type: Number }
}
});
var Person = mongoose.model('Person', personSchema);
var tagSchema = new Schema({
text: { type: String, intl: true },
description: { type: String, intl: true }
})
// var Tag = mongoose.model('Tag', tagSchema);
var partySchema = new Schema({
name: { type: String, intl: true },
abreviation: String,
color1: String,
color2: String,
bio_short: { type: String, intl: true },
bio_long: { type: String, intl: true },
dateofbirth: Date,
dateofdeath: Date,
nationality: String,
major: Boolean,
socialmedia_urls: [String],
founders: [Schema.Types.ObjectId],
predecessor: Schema.Types.ObjectId,
successor: Schema.Types.ObjectId,
tags: Schema.Types.ObjectId,
official_positioning: {
eco: Number,
soc: Number,
eu: Number,
auth: Number
},
calculated_positioning: {
eco: Number,
soc: Number,
eu: Number,
auth: Number
}
});
// var Party = mongoose.model('Party', partySchema);
var parlementSchema = new Schema({
name: { type: String, intl: true },
type: Number,
bio_short: { type: String, intl: true },
bio_long: { type: String, intl: true },
country: String,
location: String,
img_inside: String,
img_outside: String,
svg: String //The seat numbers should be embeded in the SVG
});
// var Parlement = mongoose.model('Person', parlementSchema);
var electionSchema = new Schema({
name: { type: String, intl: true },
candidates: [Schema.Types.ObjectId],
country: String,
from: Date,
to: Date,
published: Date,
type: Number,
polls: [{
institute_name: { type: String, intl: true },
institute_url: String,
summary: { type: String, intl: true },
method_url: String,
values: [{ candidate: [Schema.Types.ObjectId], value: Number }]
}]
});
// var Election = mongoose.model('Person', personSchema);
var countrySchema = new Schema({
iso_3166_2: String,
name: { type: String, intl: true },
bio_short: { type: String, intl: true },
bio_long: { type: String, intl: true },
governments: [{ //Array for the unlikely event in which a european country changes constitution
established: Date,
socialmedia_urls: [String],
members: [{
name: String,
id: String, //if ever the person exists in the database
role: { type: String, intl: true },
type: Number,
hierarchy: String, //the direct hierarchy of the person
from: Date,
to: Date
}]
}]
//The rest of the data we've got about countries, like flags or statistics don't need to be linked to the database.
//as they can just exist as plain files.
});
// var Country = mongoose.model('Person', personSchema);
///////////////////////////
// Rendering functions //
///////////////////////////
//Database page
app.get("/data", function (req, res, next) {
res.render("Pages/Database/DataHome.ejs", { lang: i18n.getLocale(req), headerIndex: 2 });
})
//Person page
app.get("/person/:id", function (req, res, next) {
Person.findById(req.params.id, function (err, selPerson) {
if(selPerson == undefined){
res.sendStatus(404); return;
}
console.log(Date.toCivilizedString(selPerson._doc.birthdate))
res.render('Pages/Database/Person.ejs', { lang: i18n.getLocale(req), headerIndex: 2, p: selPerson._doc });
});
})
//Country page
app.get("/country/:name", function (req, res, next) {
res.render("Pages/Database/CountryPage.ejs", { lang: i18n.getLocale(req), headerIndex: 2 });
})
//Political party page
app.get("/party/:country/:name", function (req, res, next) {
res.render("Pages/Database/PoliticalParty.ejs", { lang: i18n.getLocale(req), headerIndex: 2 });
})
//Editing page
app.get("/data/new/person", function (req, res, next) {
res.render("Pages/Database/EditDatabaseEntry.ejs", { lang: i18n.getLocale(req), headerIndex: 2, partial: "../../Partials/Database/Edit/Person.html"});
})
app.get("/data/new/party", function (req, res, next) {
res.render("Pages/Database/EditDatabaseEntry.ejs", { lang: i18n.getLocale(req), headerIndex: 2, partial: "../../Partials/Database/Edit/Party.html" });
})
//Upload person data
app.post("/data/person/edit/upload", upload.single('profileimg'), function (req, res, next) {
// res.sendStatus(403);return;
var so = JSON.parse(req.body.json);
var Person = mongoose.model("Person", personSchema);
var p = new Person;
Object.assign(p, so);
var id = mongoose.Types.ObjectId();
p._id = id;
p.save(function (err, mobj, numAffected) {
if (err) { res.status(400).send(JSON.stringify(err)); /*fs.unlinkSync(req.file.path);*/ return; }
else { res.status(200).send("/person/" + id.toHexString()) }
});
cloudinary.uploader.upload(req.file.path, function (result) { /* fs.unlinkSync(req.file.path);*/ },
{
public_id: id.toHexString()
});
})
}