mirror of
https://github.com/sudoxnym/habitica-self-host.git
synced 2026-05-19 20:28:43 +00:00
support for xml and json export of all user data
This commit is contained in:
parent
b4d910c9cb
commit
c0a02eeef8
4 changed files with 67 additions and 2 deletions
|
|
@ -42,7 +42,9 @@
|
|||
"nodemailer": "~0.5.2",
|
||||
"grunt-cli": "~0.1.9",
|
||||
"paypal-ipn": "~1.0.1",
|
||||
"express-csv": "~0.6.0"
|
||||
"express-csv": "~0.6.0",
|
||||
"pretty-data": "git://github.com/vkiryukhin/pretty-data#master",
|
||||
"js2xmlparser": "~0.1.2"
|
||||
},
|
||||
"private": true,
|
||||
"subdomain": "habitrpg",
|
||||
|
|
|
|||
|
|
@ -1,8 +1,12 @@
|
|||
var _ = require('lodash');
|
||||
var csv = require('express-csv');
|
||||
var express = require('express');
|
||||
var nconf = require('nconf');
|
||||
var moment = require('moment');
|
||||
var dataexport = module.exports;
|
||||
var js2xmlparser = require("js2xmlparser");
|
||||
var pd = require('pretty-data').pd;
|
||||
var User = require('../models/user').model;
|
||||
|
||||
|
||||
/*
|
||||
|
|
@ -25,3 +29,55 @@ dataexport.history = function(req, res) {
|
|||
});
|
||||
return res.csv(output);
|
||||
}
|
||||
|
||||
var userdata = function(user) {
|
||||
//may eventually need to do some parsing here to eliminate/add from/to the object, just return user for now
|
||||
return user;
|
||||
}
|
||||
|
||||
dataexport.leanuser = function(req, res, next) {
|
||||
var user = res.locals.user;
|
||||
User.findOne({_id: user._id,}).lean().exec(function(err, user) {
|
||||
if (err) return res.json(500, {err: err});
|
||||
if (_.isEmpty(user)) return res.json(401, NO_USER_FOUND);
|
||||
res.locals.user = user;
|
||||
return next();
|
||||
});
|
||||
};
|
||||
|
||||
dataexport.userdata = {
|
||||
xml: function(req, res) {
|
||||
var user = userdata(res.locals.user);
|
||||
return res.xml({data: JSON.stringify(user), rootname: 'user'});
|
||||
},
|
||||
json: function(req, res) {
|
||||
var user = userdata(res.locals.user);
|
||||
return res.jsonstring(user);
|
||||
},
|
||||
}
|
||||
|
||||
/*
|
||||
------------------------------------------------------------------------
|
||||
Express Extensions (should be refactored into a module)
|
||||
------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
var expressres = express.response || http.ServerResponse.prototype;
|
||||
|
||||
expressres.xml = function(obj, headers, status) {
|
||||
var body = '';
|
||||
this.charset = this.charset || 'utf-8';
|
||||
this.header('Content-Type', 'text/xml');
|
||||
this.header('Content-Disposition', 'attachment');
|
||||
body = pd.xml(js2xmlparser(obj.rootname,obj.data));
|
||||
return this.send(body, headers, status);
|
||||
};
|
||||
|
||||
expressres.jsonstring = function(obj, headers, status) {
|
||||
var body = '';
|
||||
this.charset = this.charset || 'utf-8';
|
||||
this.header('Content-Type', 'application/json');
|
||||
this.header('Content-Disposition', 'attachment');
|
||||
body = pd.json(JSON.stringify(obj));
|
||||
return this.send(body, headers, status);
|
||||
};
|
||||
|
|
|
|||
|
|
@ -6,5 +6,7 @@ var nconf = require('nconf');
|
|||
|
||||
/* Data export */
|
||||
router.get('/history.csv',auth.authWithSession,dataexport.history); //[todo] encode data output options in the data controller and use these to build routes
|
||||
router.get('/userdata.xml',auth.authWithSession,dataexport.leanuser,dataexport.userdata.xml);
|
||||
router.get('/userdata.json',auth.authWithSession,dataexport.leanuser,dataexport.userdata.json);
|
||||
|
||||
module.exports = router;
|
||||
|
|
|
|||
|
|
@ -71,4 +71,9 @@ script(id='partials/options.settings.export.html', type="text/ng-template")
|
|||
h2 Data Export
|
||||
small Here are a few options for saving your Habit data.
|
||||
h4 Habit History
|
||||
a(href="/export/history.csv") Export History (CSV)
|
||||
Export History:
|
||||
a(href="/export/history.csv") (CSV)
|
||||
h4 User Data
|
||||
Export User Data:
|
||||
a(href="/export/userdata.xml") (XML)
|
||||
a(href="/export/userdata.json") (JSON)
|
||||
|
|
|
|||
Loading…
Reference in a new issue