Skip to content

Instantly share code, notes, and snippets.

View samirbr's full-sized avatar

Sam Aouar samirbr

View GitHub Profile
@samirbr
samirbr / cf.py
Created July 30, 2015 17:16
COALESCE
from django.db.models.expressions import F
def __floordiv__(self, other):
return self._combine(other, '||', False)
F.__floordiv__ = __floordiv__
class CF(F):
"""
A coalesced expression representing the value of the given field.
"""
@samirbr
samirbr / coalesce.py
Last active March 21, 2022 15:52
django coalesce
"""
Classes that represent database functions.
"""
from django.db.models import IntegerField
from django.db.models.expressions import Func, Value
class Coalesce(Func):
"""
Chooses, from left to right, the first non-null expression and returns it.
"""
def copy(o, replacements, exclude):
d = model_to_dict(o)
for key in exclude:
d.pop(key)
for key in replacements.keys():
d[key] = replacements[key]
obj = o.__class__.objects.create(**d)
<div ng-controller="UploadOptionController">
<!-- $scope must have a method fromFile that receives the content -->
<!-- $scope.fromFile = function(content) { ... }
<div>
<label>Separator</label>
<input type="text" ng-model="upload.needle" name="needle">
<div>
<input class="hide" type="file" ng-model="upload.file" name="file"
onchange="angular.element(this).scope().upload.load().then(angular.element(this).scope().fromFile)">
def remove_keys(d, keys):
for key in keys:
d.pop(key)
return d
for consistency in Consistency.objects.filter(question__parent_id=form_to_copy.id):
c_dict = generic_as_dict(consistency)
c_dict = remove_keys(c_dict, ['id', 'condition', 'question'])
cond_dict = generic_as_dict(consistency.condition)
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
def update_plans(apps, schema_editor):
# free to Free
for user in User.objects.filter(plan='free'):
user.plan = 'Free'
user.save()
def do_update(self, obj, request):
obj.user = request.user.get_root()
serializer = DeviceSerializer(obj, data=request.data, partial=True)
is_create = obj.pk is None
if serializer.is_valid():
serializer.save()
if is_create:
@samirbr
samirbr / Client.js
Last active December 28, 2015 19:58
app.factory('Client', function() {
function Client(obj) {
obj.created = new Date(obj.created);
angular.extend(this, obj);
}
return Client;
});
$scope.questionnaires = [];
$scope.selectAll = function() {
$scope.selected = $scope.questionnaires.map(function(q) {
return q.id;
});
};
$scope.clear = function() {
$scope.selected = [];
moment.locale('pt-BR');
$scope.months = [];
$scope.series = [];
$scope.data = [];
$http.get('/nodes')
.then(function(response) {
var months = {};