Install Django on Linux

1. Create virtual environment and Install django pip install virtualenv virtualenv -p python3 venv source venv/bin/activate pip install Django==1.9.3 2. Create a django project django-admin startproject projectName 3. Migration : python migrate 4. Create superuser : python createsuperuser 5. run the server: python runserver 6. To change “Django Administration” header. Add following […]

Export mongodb query result to csv

1. Create a file “exportwords.js” and add following code: var today = new Date(); var year = today.getFullYear(); var month = today.getMonth(); var day = today.getDate(); var date = year + “-” + month + “-” + day; print(“Query”); db.searchwords.find({date: date}).forEach(function(sw){ print (sw.query); }) 2. Run following command: mongo databasename exportwords.js > exportwords.csv

Loopback : How to get the current user on loopback models

To get the current user add following code on server.js : app.use(function (req, res, next) { if (!req.accessToken) return next(); var userId = req.accessToken.userId; app.models.User.findById(userId, function(err, user) { if (err) return next(err); if (!user) return next(‘No user found!’); var loopbackContext = loopback.getCurrentContext(); if (loopbackContext) loopbackContext.set(‘currentUser’, user); next(); }); }); To get current user from model […]