0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2025-12-28 09:12:45 +00:00

Merge branch 'admin'

This commit is contained in:
Scott Tolksdorf
2016-04-20 01:30:15 -04:00
11 changed files with 289 additions and 226 deletions

View File

@@ -1,5 +1,12 @@
# changelog
### Wednesday, 20/04/2016
- A lot of admin improvements. Pagninated brew table
- Added a searching and removing abandoned brew api endpoints (turns out about 40% of brews are shorter that a tweet!).
- Fixed the require cache being cleared. Pages should render a bit faster now.
### Wednesday, 06/04/2016 - v1.4
* Pages will now partially render. This should greatly speed up *very* large homebrews. The Homebreery will figure out which page you should be looking at and render that page, the page before, and the page after.
* Zooming should be fixed. I've changed the font size units to be cm, which match the units of the page. Zooming in and out now look much better.

View File

@@ -5,49 +5,99 @@ var request = require('superagent');
var Moment = require('moment');
//TODO: Add incremental React scrolling
var VIEW_LIMIT = 30;
var COLUMN_HEIGHT = 52;
var HomebrewAdmin = React.createClass({
getDefaultProps: function() {
return {
homebrews : [],
admin_key : ''
};
},
getInitialState: function() {
return {
viewStartIndex: 0
page: 0,
count : 20,
brewCache : {},
total : 0,
processingOldBrews : false
};
},
clearOldBrews : function(){
if(!confirm("Are you sure you want to clear out old brews?")) return;
request.get('/homebrew/clear_old/?admin_key=' + this.props.admin_key)
.send()
.end(function(err, res){
window.location.reload();
fetchBrews : function(page){
request.get('/homebrew/api/search')
.query({
admin_key : this.props.admin_key,
count : this.state.count,
page : page
})
.end((err, res)=>{
this.state.brewCache[page] = res.body.brews;
this.setState({
brewCache : this.state.brewCache,
total : res.body.total,
count : res.body.count
})
})
},
componentDidMount: function() {
this.fetchBrews(this.state.page);
},
changePageTo : function(page){
if(!this.state.brewCache[page]){
this.fetchBrews(page);
}
this.setState({
page : page
})
},
clearInvalidBrews : function(){
request.get('/homebrew/api/invalid')
.query({admin_key : this.props.admin_key})
.end((err, res)=>{
if(!confirm("This will remove " + res.body.count + " brews. Are you sure?")) return;
request.get('/homebrew/api/invalid')
.query({admin_key : this.props.admin_key, do_it : true})
.end((err, res)=>{
alert("Done!")
});
});
},
deleteBrew : function(brewId){
request.get('/homebrew/remove/' + brewId +'?admin_key=' + this.props.admin_key)
.send()
if(!confirm("Are you sure you want to delete '" + brewId + "'?")) return;
request.get('/homebrew/api/remove/' + brewId)
.query({admin_key : this.props.admin_key})
.end(function(err, res){
window.location.reload();
})
},
renderBrews : function(){
// return _.times(VIEW_LIMIT, (i)=>{
// var brew = this.props.homebrews[i + this.state.viewStartIndex];
// if(!brew) return null;
handlePageChange : function(dir){
this.changePageTo(this.state.page + dir);
},
return _.map(this.props.homebrews, (brew)=>{
renderPagnination : function(){
var outOf;
if(this.state.total){
outOf = this.state.page + ' / ' + Math.round(this.state.total/this.state.count);
}
return <div className='pagnination'>
<i className='fa fa-chevron-left' onClick={this.handlePageChange.bind(this, -1)}/>
{outOf}
<i className='fa fa-chevron-right' onClick={this.handlePageChange.bind(this, 1)}/>
</div>
},
renderBrews : function(){
var brews = this.state.brewCache[this.state.page] || _.times(this.state.count);
return _.map(brews, (brew)=>{
return <tr className={cx('brewRow', {'isEmpty' : brew.text == "false"})} key={brew.sharedId}>
<td><a href={'/homebrew/edit/' + brew.editId} target='_blank'>{brew.editId}</a></td>
<td><a href={'/homebrew/share/' + brew.shareId} target='_blank'>{brew.shareId}</a></td>
@@ -74,7 +124,7 @@ var HomebrewAdmin = React.createClass({
<th>Created At</th>
<th>Last Updated</th>
<th>Last Viewed</th>
<th>Number of Views</th>
<th>Views</th>
</tr>
</thead>
<tbody>
@@ -88,12 +138,14 @@ var HomebrewAdmin = React.createClass({
var self = this;
return <div className='homebrewAdmin'>
<h2>
Homebrews - {this.props.homebrews.length}
<button className='clearOldButton' onClick={this.clearOldBrews}>
Clear Old
</button>
Homebrews - {this.state.total}
</h2>
{this.renderPagnination()}
{this.renderBrewTable()}
<button className='clearOldButton' onClick={this.clearInvalidBrews}>
Clear Old
</button>
</div>
}
});

View File

@@ -1,8 +1,7 @@
.homebrewAdmin{
margin-bottom: 80px;
.brewTable{
overflow-y : scroll;
max-height : 500px;
table{
th{

View File

@@ -43,7 +43,7 @@ var Statusbar = React.createClass({
if(!confirm("are you sure you want to delete this brew?")) return;
if(!confirm("are you REALLY sure? You will not be able to recover it")) return;
request.get('/homebrew/remove/' + this.props.editId)
request.get('/homebrew/api/remove/' + this.props.editId)
.send()
.end(function(err, res){
window.location.href = '/homebrew';

View File

@@ -8,9 +8,6 @@ var app = express();
app.use(express.static(__dirname + '/build'));
app.use(bodyParser.json());
//Mongoose
var mongoose = require('mongoose');
var mongoUri = process.env.MONGOLAB_URI || process.env.MONGOHQ_URL || 'mongodb://localhost/naturalcrit';
@@ -19,65 +16,46 @@ mongoose.connection.on('error', function(){
console.log(">>>ERROR: Run Mongodb.exe ya goof!");
});
//Admin route
process.env.ADMIN_USER = process.env.ADMIN_USER || 'admin';
process.env.ADMIN_PASS = process.env.ADMIN_PASS || 'password';
process.env.ADMIN_KEY = process.env.ADMIN_KEY || 'admin_key';
var auth = require('basic-auth');
var HomebrewModel = require('./server/homebrew.model.js').model;
app.get('/admin', function(req, res){
var credentials = auth(req)
if (!credentials || credentials.name !== process.env.ADMIN_USER || credentials.pass !== process.env.ADMIN_PASS) {
res.setHeader('WWW-Authenticate', 'Basic realm="example"')
return res.status(401).send('Access denied')
}
HomebrewModel.find({}, function(err, homebrews){
//Remove the text to reduce the response payload
homebrews = _.map(homebrews, (brew)=>{
brew.text = brew.text != '';
return brew;
});
vitreumRender({
page: './build/admin/bundle.dot',
prerenderWith : './client/admin/admin.jsx',
clearRequireCache : true,
initialProps: {
url: req.originalUrl,
admin_key : process.env.ADMIN_KEY,
homebrews : homebrews,
},
}, function (err, page) {
return res.send(page)
});
vitreumRender({
page: './build/admin/bundle.dot',
prerenderWith : './client/admin/admin.jsx',
clearRequireCache : !process.env.PRODUCTION,
initialProps: {
url: req.originalUrl,
admin_key : process.env.ADMIN_KEY,
},
}, function (err, page) {
return res.send(page)
});
});
//Populate homebrew routes
app = require('./server/homebrew.api.js')(app);
app = require('./server/homebrew.server.js')(app);
app.get('*', function (req, res) {
vitreumRender({
page: './build/naturalCrit/bundle.dot',
globals:{
},
globals:{},
prerenderWith : './client/naturalCrit/naturalCrit.jsx',
initialProps: {
url: req.originalUrl
},
clearRequireCache : true,
clearRequireCache : !process.env.PRODUCTION,
}, function (err, page) {
return res.send(page)
});

View File

@@ -1,9 +1,24 @@
var _ = require('lodash');
var Moment = require('moment');
var vitreumRender = require('vitreum/render');
var HomebrewModel = require('./homebrew.model.js').model;
var changelogText = require('fs').readFileSync('./changelog.md', 'utf8');
var homebrewTotal = 0;
var refreshCount = function(){
HomebrewModel.count({}, function(err, total){
homebrewTotal = total;
});
};
refreshCount()
var mw = {
adminOnly : function(req, res, next){
if(req.query && req.query.admin_key == process.env.ADMIN_KEY){
next();
}else{
return res.status(401).send('Access denied');
}
}
};
var getTopBrews = function(cb){
@@ -13,7 +28,6 @@ var getTopBrews = function(cb){
}
module.exports = function(app){
app.get('/homebrew/top', function(req, res){
@@ -22,16 +36,6 @@ module.exports = function(app){
});
})
app.get('/homebrew/new', function(req, res){
var newHomebrew = new HomebrewModel();
newHomebrew.save(function(err, obj){
return res.redirect('/homebrew/edit/' + obj.editId);
})
})
//Updating
app.put('/homebrew/update/:id', function(req, res){
HomebrewModel.find({editId : req.params.id}, function(err, objs){
if(!objs.length || err) return res.status(404).send("Can not find homebrew with that id");
@@ -45,137 +49,65 @@ module.exports = function(app){
});
});
app.get('/homebrew/remove/:id', function(req, res){
//if(req.query && req.query.admin_key == process.env.ADMIN_KEY){
HomebrewModel.find({editId : req.params.id}, function(err, objs){
console.log(err);
if(!objs.length || err) return res.status(404).send("Can not find homebrew with that id");
var resEntry = objs[0];
resEntry.remove(function(err){
if(err) return res.status(500).send("Error while removing");
return res.status(200).send();
})
});
//}else{
// return res.status(401).send('Access denied');
//}
app.get('/homebrew/api/remove/:id', function(req, res){
HomebrewModel.find({editId : req.params.id}, function(err, objs){
if(!objs.length || err) return res.status(404).send("Can not find homebrew with that id");
var resEntry = objs[0];
resEntry.remove(function(err){
if(err) return res.status(500).send("Error while removing");
return res.status(200).send();
})
});
});
//Removes all empty brews that are older than 3 days
app.get('/homebrew/clear_old', function(req, res){
if(req.query && req.query.admin_key == process.env.ADMIN_KEY){
HomebrewModel.remove({
text : '',
createdAt: {
$lt: Moment().subtract(3, 'days').toDate()
}
}, function(err, objs){
return res.status(200).send();
});
//Removes all empty brews that are older than 3 days and that are shorter than a tweet
app.get('/homebrew/api/invalid', mw.adminOnly, function(req, res){
var invalidBrewQuery = HomebrewModel.find({
'$where' : "this.text.length < 140",
createdAt: {
$lt: Moment().subtract(3, 'days').toDate()
}
});
if(req.query.do_it){
invalidBrewQuery.remove().exec((err, objs)=>{
refreshCount();
return res.send(200);
})
}else{
return res.status(401).send('Access denied');
invalidBrewQuery.exec((err, objs)=>{
if(err) console.log(err);
return res.json({
count : objs.length
})
})
}
});
//Edit Page
app.get('/homebrew/edit/:id', function(req, res){
HomebrewModel.find({editId : req.params.id}, function(err, objs){
if(err || !objs.length) return res.status(404).send('Could not find Homebrew with that id');
app.get('/homebrew/api/search', mw.adminOnly, function(req, res){
var page = req.query.page || 0;
var count = req.query.count || 20;
var resObj = null;
var errObj = {text: "# oops\nCould not find the homebrew."}
if(objs.length){
resObj = objs[0];
}
vitreumRender({
page: './build/homebrew/bundle.dot',
globals:{},
prerenderWith : './client/homebrew/homebrew.jsx',
initialProps: {
url: req.originalUrl,
brew : resObj || errObj
},
clearRequireCache : true,
}, function (err, page) {
return res.send(page)
HomebrewModel.find({}, {
text : 0 //omit the text
}, {
skip: page*count,
limit: count*1
}, function(err, objs){
if(err) console.log(err);
return res.json({
page : page,
count : count,
total : homebrewTotal,
brews : objs
});
})
});
//Share Page
app.get('/homebrew/share/:id', function(req, res){
HomebrewModel.find({shareId : req.params.id}, function(err, objs){
if(err || !objs.length) return res.status(404).send('Could not find Homebrew with that id');
var resObj = null;
var errObj = {text: "# oops\nCould not find the homebrew."}
if(objs.length){
resObj = objs[0];
resObj.lastViewed = new Date();
resObj.views = resObj.views + 1;
resObj.save();
}
vitreumRender({
page: './build/homebrew/bundle.dot',
globals:{},
prerenderWith : './client/homebrew/homebrew.jsx',
initialProps: {
url: req.originalUrl,
brew : resObj || errObj
},
clearRequireCache : true,
}, function (err, page) {
return res.send(page)
});
})
});
//Print Page
var Markdown = require('marked');
var PHBStyle = '<style>' + require('fs').readFileSync('./phb.standalone.css', 'utf8') + '</style>'
app.get('/homebrew/print/:id', function(req, res){
HomebrewModel.find({shareId : req.params.id}, function(err, objs){
if(err || !objs.length) return res.status(404).send('Could not find Homebrew with that id');
var resObj = null;
if(objs.length){
resObj = objs[0];
}
var content = _.map(resObj.text.split('\\page'), function(pageText){
return '<div class="phb">' + Markdown(pageText) + '</div>';
}).join('\n');
var title = '<title>' + resObj.text.split('\n')[0] + '</title>';
var page = '<html><head>' + title + PHBStyle + '</head><body>' + content +'</body></html>'
return res.send(page)
});
});
})
//Home and 404, etc.
var welcomeText = require('fs').readFileSync('./client/homebrew/homePage/welcome_msg.txt', 'utf8');
app.get('/homebrew*', function (req, res) {
vitreumRender({
page: './build/homebrew/bundle.dot',
globals:{},
prerenderWith : './client/homebrew/homebrew.jsx',
initialProps: {
url: req.originalUrl,
welcomeText : welcomeText,
changelog : changelogText
},
clearRequireCache : true,
}, function (err, page) {
return res.send(page)
});
});
return app;
}

116
server/homebrew.server.js Normal file
View File

@@ -0,0 +1,116 @@
var _ = require('lodash');
var vitreumRender = require('vitreum/render');
var HomebrewModel = require('./homebrew.model.js').model;
var changelogText = require('fs').readFileSync('./changelog.md', 'utf8');
module.exports = function(app){
app.get('/homebrew/new', function(req, res){
var newHomebrew = new HomebrewModel();
newHomebrew.save(function(err, obj){
return res.redirect('/homebrew/edit/' + obj.editId);
})
})
//Edit Page
app.get('/homebrew/edit/:id', function(req, res){
HomebrewModel.find({editId : req.params.id}, function(err, objs){
if(err || !objs.length) return res.status(404).send('Could not find Homebrew with that id');
var resObj = null;
var errObj = {text: "# oops\nCould not find the homebrew."}
if(objs.length){
resObj = objs[0];
}
vitreumRender({
page: './build/homebrew/bundle.dot',
globals:{},
prerenderWith : './client/homebrew/homebrew.jsx',
initialProps: {
url: req.originalUrl,
brew : resObj || errObj
},
clearRequireCache : !process.env.PRODUCTION,
}, function (err, page) {
return res.send(page)
});
})
});
//Share Page
app.get('/homebrew/share/:id', function(req, res){
HomebrewModel.find({shareId : req.params.id}, function(err, objs){
if(err || !objs.length) return res.status(404).send('Could not find Homebrew with that id');
var resObj = null;
var errObj = {text: "# oops\nCould not find the homebrew."}
if(objs.length){
resObj = objs[0];
resObj.lastViewed = new Date();
resObj.views = resObj.views + 1;
resObj.save();
}
vitreumRender({
page: './build/homebrew/bundle.dot',
globals:{},
prerenderWith : './client/homebrew/homebrew.jsx',
initialProps: {
url: req.originalUrl,
brew : resObj || errObj
},
clearRequireCache : !process.env.PRODUCTION,
}, function (err, page) {
return res.send(page)
});
})
});
//Print Page
var Markdown = require('marked');
var PHBStyle = '<style>' + require('fs').readFileSync('./phb.standalone.css', 'utf8') + '</style>'
app.get('/homebrew/print/:id', function(req, res){
HomebrewModel.find({shareId : req.params.id}, function(err, objs){
if(err || !objs.length) return res.status(404).send('Could not find Homebrew with that id');
var resObj = null;
if(objs.length){
resObj = objs[0];
}
var content = _.map(resObj.text.split('\\page'), function(pageText){
return '<div class="phb">' + Markdown(pageText) + '</div>';
}).join('\n');
var title = '<title>' + resObj.text.split('\n')[0] + '</title>';
var page = '<html><head>' + title + PHBStyle + '</head><body>' + content +'</body></html>'
return res.send(page)
});
});
//Home and 404, etc.
var welcomeText = require('fs').readFileSync('./client/homebrew/homePage/welcome_msg.txt', 'utf8');
app.get('/homebrew*', function (req, res) {
vitreumRender({
page: './build/homebrew/bundle.dot',
globals:{},
prerenderWith : './client/homebrew/homebrew.jsx',
initialProps: {
url: req.originalUrl,
welcomeText : welcomeText,
changelog : changelogText
},
clearRequireCache : !process.env.PRODUCTION,
}, function (err, page) {
return res.send(page)
});
});
return app;
}

View File

@@ -1,31 +0,0 @@
var pdf = require('html-pdf');
var Markdown = require('marked');
var PHBStyle = '<style>' + require('fs').readFileSync('../phb.standalone.css', 'utf8') + '</style>'
var content = Markdown('# oh hey \n welcome! isnt this neat \n \\page ##### test');
var html = "<html><head>" + PHBStyle + "</head><body><div class='phb'>"+ content +"</div></body></html>"
//var h = 279.4 - 20*2.8;
var h = 279.4 - 56;
//var w = 215.9 - 56*1.7
var w = 215.9 - 43;
var config = {
"height": (279.4 - 56) + "mm",
"width": (215.9 - 43) + "mm",
"border": "0",
}
pdf.create(html, config).toFile('./temp.pdf', function(err, res){
console.log(err);
console.log(res.filename);
});

Binary file not shown.

Binary file not shown.

10
todo.txt Normal file
View File

@@ -0,0 +1,10 @@
## v1.5 todo
- Make statusbar un-fixed
- Simplify the panel css to remove the current issues
- Add bleed snippet
- Add the '/new' page and force save to reduce database size
- Add pagniation and query to the homebrew api
- Update the admin page with pagnition and a query box
- Test the old/small brew filtering for deleteion
- Make the status bar take children? Or just give it a string of booleans. Just simplify it
- Partial rendering kills style tags on unrendered pages. Detect if pages have style tags and render them.