Django

Save django model with dictionaries

29 Mar , 2015  

Beware when queryset filter update() because it will pass model validation. Here is some example save model with dictionaries :

1
2
3
4
5
6
7
8
9
10
for k, v in account.items():
    setattr(instance, k, v)

try:
    instance.save()
except Exception, e:
    raise ValidationError(e)
else:
    updated += 1
    # print account

Django

Convert boolean True and False field value in Django Rest Framework

29 Mar , 2015  

Here is a quick snippet to convert boolean True / False in DRF Serializer:

1
2
3
4
5
6
is_finish = serializers.SerializerMethodField()

def get_is_finish(self, obj):
    if obj.is_finish:
        return "Selesai"
    return "Progress"

Django

Modify object data by write Custom Renderer in Django Rest Framework

29 Mar , 2015  

When we need to modify object or results from DRF / Django Rest Framework, all we need just write a custom JSON Renderer like below :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class JGridJSONRenderer(renderers.JSONRenderer):
    def render(self, data, accepted_media_type=None, renderer_context=None):
        new_rows = []

        # Name the object list
        for obj in data['rows']:
            obj = dict(obj)
            new_rows.append({'id': obj['id'], 'cell': obj})

        data['rows'] = new_rows
        response_data = data
        response = super(JGridJSONRenderer, self).render(response_data, accepted_media_type, renderer_context)

        return response

Python

Python remove dictionary key if value is empty

11 Mar , 2015  

Here is a quick snippet if we want to remove dictionary key if value is empty:

1
new_dict = {k: v for k, v in _data.items() if v if v is not ''}

Javascript

Solve issue Fullcalendar count two day as one day

9 Mar , 2015  

This is not bug. When we try to add day in March with start 10-03-2015T10:00:00 and end 11-03-2015T02:00:00, then it will showing one day instead of two day.

To solve this issue, we need to put nextDayThreshold:

1
nextDayThreshold: "00:00"

To make the next day threshold every 00:00

Algorithm

Counting number contain 14 on n-digit number in less than 1 second

8 Mar , 2015  

My fellow Adiyat in our company POLATIC bug me with this question “How to counting number contain 14 in 10 millions numbers less than 1 second?”. Then I thought, it’s easy, let give python a shot!

Famous-characters-Troll-face-Challenge-accepted-140949

1
print len([x for x in range(0, 10000000) if str(x).find('14') > -1])

With result “590040″ it’s takes 9.6s based on SublimeText (yes, i’m kindda hipster guy!). This is not good, no one want to waiting around 10 seconds to get the answer!

Lets break it down by printing sample data for 0-100 and 0-1000

1
2
print [x for x in range(0, 100) if str(x).find('14') > -1]
print [x for x in range(0, 1000) if str(x).find('14') > -1]

And we got :

1
2
3
[14]
[114, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 214, 314, 414, 514, 614, 714, 814, 914]
[Finished in 0.2s]

Wait, i want more, this is not enough to see pattern

1
2
3
4
[14]
[114, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 214, 314, 414, 514, 614, 714, 814, 914]
[1014, 1114, 1140, 1141, 1142, 1143, 1144, 1145, 1146, 1147, 1148, 1149, 1214, 1314, 1400, 1401, 1402, 1403, 1404, 1405, 1406, 1407, 1408, 1409, 1410, 1411, 1412, 1413, 1414, 1415, 1416, 1417, 1418, 1419, 1420, 1421, 1422, 1423, 1424, 1425, 1426, 1427, 1428, 1429, 1430, 1431, 1432, 1433, 1434, 1435, 1436, 1437, 1438, 1439, 1440, 1441, 1442, 1443, 1444, 1445, 1446, 1447, 1448, 1449, 1450, 1451, 1452, 1453, 1454, 1455, 1456, 1457, 1458, 1459, 1460, 1461, 1462, 1463, 1464, 1465, 1466, 1467, 1468, 1469, 1470, 1471, 1472, 1473, 1474, 1475, 1476, 1477, 1478, 1479, 1480, 1481, 1482, 1483, 1484, 1485, 1486, 1487, 1488, 1489, 1490, 1491, 1492, 1493, 1494, 1495, 1496, 1497, 1498, 1499, 1514, 1614, 1714, 1814, 1914, 2014, 2114, 2140, 2141, 2142, 2143, 2144, 2145, 2146, 2147, 2148, 2149, 2214, 2314, 2414, 2514, 2614, 2714, 2814, 2914, 3014, 3114, 3140, 3141, 3142, 3143, 3144, 3145, 3146, 3147, 3148, 3149, 3214, 3314, 3414, 3514, 3614, 3714, 3814, 3914, 4014, 4114, 4140, 4141, 4142, 4143, 4144, 4145, 4146, 4147, 4148, 4149, 4214, 4314, 4414, 4514, 4614, 4714, 4814, 4914, 5014, 5114, 5140, 5141, 5142, 5143, 5144, 5145, 5146, 5147, 5148, 5149, 5214, 5314, 5414, 5514, 5614, 5714, 5814, 5914, 6014, 6114, 6140, 6141, 6142, 6143, 6144, 6145, 6146, 6147, 6148, 6149, 6214, 6314, 6414, 6514, 6614, 6714, 6814, 6914, 7014, 7114, 7140, 7141, 7142, 7143, 7144, 7145, 7146, 7147, 7148, 7149, 7214, 7314, 7414, 7514, 7614, 7714, 7814, 7914, 8014, 8114, 8140, 8141, 8142, 8143, 8144, 8145, 8146, 8147, 8148, 8149, 8214, 8314, 8414, 8514, 8614, 8714, 8814, 8914, 9014, 9114, 9140, 9141, 9142, 9143, 9144, 9145, 9146, 9147, 9148, 9149, 9214, 9314, 9414, 9514, 9614, 9714, 9814, 9914]
[Finished in 0.2s]

Did you see the patterns?

1
2
3
[{1 number}]
[{1 number}, 140..{10 number}, {1 number}, {1 number}, {1 number}, {1 number}, {1 number}, {1 number}, {1 number}, {1 number}]
[{1 number}, {1 number},{10 number}, {3 number} {100 number}, {5 number}, 2014, 2114, {10 number}, 2214, 2314, 2414, 2514, 2614, 2714, 2814, 2914, 3014, 3114, {10 number}, 3214, 3314, 3414, 3514, 3614, 3714, 3814, 3914, 4014, 4114, {10 number},  ...]

Which i can convert into simple list like :

1
2
3
1
9 + 10, ...  => total (19) + 1 = 20
9 + 10, 100, 10 + 10,  10 + 10,  10 + 10,  10 + 10 ... => total (279) + 20 = 299

My assumption, we need (10^n-2) here to capture total number in combination 14, 14x, 14xx, 14xxx … so on.

Second, we see need for recursive function to add result from previous base.

1
f(x) = 10 * f(n-1) + 10^n-2

Let do test with base 2, 3 and 4 on paper :

1
2
3
1
10 + 10
100 + 200

Well, it’s failed on base 3. Let’s think that this formula 10 * f(n-1) missing reduce duplicate number. Let’s reduce it with f(n-2) and recalculate again :

1
2
3
1
10 + 10
100 + 199

Yeay! Let’s write into code :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
"""
Calculate contain number of whatever base 2 number
in given base number
"""
def counting(n):
    if n == 2:
        return 1
    elif n < 2:
        return 0

    return 10 * counting(n-1) - counting(n-2) + 10**(n-2)

if __name__ == "__main__":
    n = 7
    print "The results counting {} base is {}".format(n, counting(n))

And the results is :

1
2
The results counting 7 base is 590040
[Finished in 0.2s]

In Polatic, we always learn and solve enterprise problems with best approach. See pattern and use algorithm to provide quick solution is one of our daily challenge.

Btw, you can check my github in http://github.com/yodiaditya or our company github http://github.com/polatic

Follow this discussion in HackerNews:
https://news.ycombinator.com/item?id=9165357

Django

Django unit test formset

1 Mar , 2015  

When testing page that contains formset in Django using unit test, we may encounter :

1
ValidationError: [u'ManagementForm data is missing or has been tampered with']

This is happend because we POST data without required FORMSET hidden value in form. To solve this issue, inspect element on your formset and insert hidden value in payload data.

Example :

1
2
<input id="id_firstformaset-MIN_NUM_FORMS" name="firstformaset-MIN_NUM_FORMS" type="hidden" value="0">
...

And put this in our payload:

1
2
3
4
5
6
7
8
        self.correct = {
            'name': 'TESTDATA',
            'gender': self.model.FEMALE_STATUS,
            'firstformaset-TOTAL_FORMS': 1,
            'firstformaset-INITIAL_FORMS': 0,
            'firstformaset-MIN_NUM_FORMS': 0,
            'firstformaset-MAX_NUM_FORMS': 1,
        }