Skip to content

Instantly share code, notes, and snippets.

View maryokhin's full-sized avatar

Tymur Maryokhin maryokhin

View GitHub Profile
graph [
{{#each nodes ~}}
node [
id {{id}}
isGroup {{isGroup}}
label "
<html><h2 align=&quot;center&quot;>{{label}}</h2></html>"
]
{{/each ~}}
{{#each edges ~}}
@maryokhin
maryokhin / pytest DB connection error
Created July 13, 2014 21:11
Using the same settings as unittest, but pytest can't connect
/Users/maryokhin/.virtualenvs/backend/bin/python /Applications/PyCharm.app/helpers/pycharm/pytestrunner.py -p pytest_teamcity /Users/maryokhin/Workspace/backend/api/tests/test_comment2.py
Testing started at 23:08 ...
============================= test session starts ==============================
platform darwin -- Python 3.4.1 -- py-1.4.20 -- pytest-2.5.2
plugins: django
collected 1 items
../../../Users/maryokhin/Workspace/backend/api/tests/test_comment2.py E
def setup_module():
> channel = Channel.objects.create(name='test')
class PostSerializer(ModelSerializer):
"""
General post serializer.
"""
url = ReverseField(view_name='post-instance')
channel = ChannelSerializer(read_only=True)
user = UserInstanceSerializer(read_only=True)
latest_comments = serializers.SerializerMethodField('get_latest_comments')
comments_url = ReverseField(view_name='post-comment-list')
channel_id = serializers.PrimaryKeyRelatedField(source='channel', write_only=True)
# Channels
url(r'^channels/$', ChannelViewSet.as_view(**VIEW_LIST), name='channel-list'),
url(r'^channels/(?P<channel_key>[\w_]+)/$', ChannelViewSet.as_view(**VIEW_INSTANCE), name='channel-instance'),
class ChannelViewSet(GetObjectByKeyMixin, ModelViewSet):
"""
View for retrieving and updating channel information.
"""
queryset = Channel.objects.all()
serializer_class = ChannelSerializer
lookup_field = 'channel_key'
permission_classes = (ChannelPermission,)
class ReverseField(Field):
"""
Simple read only field for returning reverse URLs.
This field utilizes information from the object being serialized. It does
not care about what the current URL contains, lookup fields in the URL,
nor the structure of a model. Instead, it takes the field names passed
into lookup_fields and reads the values of those fields from the object.
The values are then passed as arguments to the reverse function, which
constructs the final URL.
@maryokhin
maryokhin / channel.py
Created June 24, 2014 17:59
No way to insert a custom serializer inside a model serializer?
from rest_framework import serializers
from rest_framework.serializers import ModelSerializer, Serializer
from api.models import Subscriber
from api.models.channel import Channel
from api.util.connection import get_redis_connection
from api.util.field import ReverseField
class ChannelStatsSerializer(Serializer):
@maryokhin
maryokhin / social_login.py
Created June 24, 2014 17:06
DRF + python-social-auth
from rest_framework import status
from rest_framework.authtoken.models import Token
from rest_framework.authtoken.views import ObtainAuthToken
from rest_framework.generics import ListAPIView
from rest_framework.response import Response
from rest_framework.reverse import reverse
from rest_framework.settings import api_settings
from social.apps.django_app.utils import strategy
from social.backends.oauth import BaseOAuth1, BaseOAuth2
@maryokhin
maryokhin / AbstractGraph.java
Created May 30, 2014 17:42
Should generate a String that graph does not have a label, not 'null'.
/*
* AbstractGraph.java
*
* Created: 10th of November 2003
*
* File Version: 0.4
*
* History:
* --------
* - The methods are implemented, They were not implemented before, Tomas Saxeggen and Lars Johansson, 20031118
@maryokhin
maryokhin / parameters.py
Created May 18, 2014 21:50
Here it throws an error due to oauth_consumer_key being 'None'
# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals
"""
oauthlib.parameters
~~~~~~~~~~~~~~~~~~~
This module contains methods related to `section 3.5`_ of the OAuth 1.0a spec.
.. _`section 3.5`: http://tools.ietf.org/html/rfc5849#section-3.5