Skip to main content

Posts

Showing posts with the label request

SQLAlchemy (Flask) count model instances by unique values

One comes to a task that has to do with counting items in a database. We will describe the right approach here. Despite being so obvious I did not find much of the docs for junior developers to watch and learn. Here is a sample task and solution: Let's assume we have a model like so: class Cycle (db.Model): id = db.Column(db.Integer, primary_key= True ) object_id = db.Column(db.String, nullable= False ) Sample date populated into it will be: { id : 1 , object_id: 'unique1' }, { id : 2 , object_id: 'unique1' }, { id : 3 , object_id: 'unique2' }, { id : 4 , object_id: 'unique2' }, { id : 5 , object_id: 'unique2' }, { id : 6 , object_id: 'unique3' } We need to count unique model instances with same  object_id . To achieve this relatively simple task one would go straightforward. E.g. Fetch all the Cycle instances with a simple query and then...

AJAX form in Django with jQuery.form plugin

Here I will describe an example form using jQuery Form  plugin. It will provide a good frontend, while Django serving a simple backend. I made this AJAX form using unobtrusive javascript. With human language it means the form will function with JavaScript disabled. And so jQuery.form plugin will work as an AJAX speedup addition and not as a major requirement. Features: * Works both with JavaScript  and without. * Uses standard Django ideology/hooks. * Uses the most known jQuery plugin for forms AJAX handling. Theory: Main idea that Django supports both normal and AJAX request in a standard view. And has a handy request.is_ajax() request method. It returns True/False depending on if request search has HTTP_X_REQUESTED_WITH header for the string 'XMLHttpRequest' . jQuery.form plugin sure does have that. Backend Django. We will create an app called 'contact' for our task and place it in the example django project. Urls from the project will redirect to ...

CouchDB restoring deleted/updated documents and their data

We are using CouchDB for production and happy with it. It is much more lightweight rather then MongoDB yet powerful. (For our needs at least). But sometimes you have situations that some code deleted/spoiled your Couch Database data. We had some bugs leading to deleting indexes. However compaction have not been run and here is the decision. There are several ways for different situations. I'll try to cover them all. So for deleted CouchDB documents you need to: 1. Make sure your document with this id is Deleted. To do it you need to request CouchDB for this document. E.g. with this string: $db/$id Where  $db  is your CouchDB database name and  $id  is your deleted document id it should return something like this: { "error" : "not_found" , "reason" : "deleted" } 2. Get all the revisions of the deleted document. With this request: $db/$id?revs= true &open_revs=all Where $db is your CouchDB database name and $id is ...

Meteor. Request to host is not allowed by Access-Control-Allow-Origin.

As a novice I'd like to describe a situation when you may need to make calls to a different domain. In my case it was a requirement to access external API. Anyway I'd like to describe here my perspective of usage this in meteor. You would need to get those data and parse them. First thought is to use client. Wrong. You would need to add CORS support to your application. It means you need to have header  Access-Control-Allow-Origin: * added to all of your response objects. This will enable Meteor usage of external domain responses. And requires you to hack Meteor code. But there is more "proper" way to work with server. Code on a server is executed synchronously and so you can be sure it will be executed and result returned. So. First we need to make sure we have a Meteor.http package installed. You can install it by executing "meteor add http" in your project root directory. Meteor.http can work in both synchronous and asynchronous modes. It is d...

Users, Groups and their Permissions in Django + Recipes

Django supports security models and methods out of the box. They are Group and Permission objects. Permission is m2m related to internal Django User. I helps you relay on request.user later in your code. You often come to situations where you may need a view to be accessed only by certain group of users.  For example you have the app that has two groups of users. One can search and another one can Index files. Simplest approach is to use Groups here.  In fact you may use permissions in case your app will have several unique users that might do some stuff. In general best approach is to use Group to specify type of users and Permission to specify the role of users in this group. So if you will have Group called 'search' and it will have permission with name, say 'search stuff'. So when you will call: def my_view (request): # ... my view actions ... user_permissions = request.user.user_permissions.all() for p in user...