Skip to content

Commit

Permalink
add rest-framework app
Browse files Browse the repository at this point in the history
  • Loading branch information
zhijiasun committed May 29, 2014
1 parent 4a6c6f6 commit 44d6c1a
Show file tree
Hide file tree
Showing 17 changed files with 155 additions and 0 deletions.
Binary file modified db.sqlite3
Binary file not shown.
Empty file added epm/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions epm/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
42 changes: 42 additions & 0 deletions epm/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from django.db import models

# Create your models here.

class enterprise(models.Model):

STATE_OWNED_ENTER = 1
COOPERATIVE_ENTER = 2
JOIN_VENTURE = 3
NATURE_CHOICES = (
(STATE_OWNED_ENTER,u'国有企业'),
(COOPERATIVE_ENTER,'合作企业'),
(JOIN_VENTURE,u'合资企业'),
)
enter_id = models.AutoField(primary_key=True,auto_created=True)
enter_name = models.CharField(u'企业名称',max_length=50)
register_time = models.DateField(u'注册时间')
enter_address = models.CharField(u'注册地址',max_length=50)
enter_nature = models.IntegerField(u'企业性质',default=STATE_OWNED_ENTER,choices=NATURE_CHOICES)

class Meta:
verbose_name = u'企业信息'
verbose_name_plural = u'企业信息'


def __unicode__(self):
return self.enter_name


class party(models.Model):
party_id = models.AutoField(primary_key=True,auto_created=True)
party_name = models.CharField(u'党支部名称',max_length=100)
member_number = models.IntegerField(u'党员人数')
contact_info = models.CharField(u'联系方式',max_length=300)

class Meta:
verbose_name = u'党支部信息'
verbose_name_plural = u'党支部信息'


def __unicode__(self):
return self.party_name
3 changes: 3 additions & 0 deletions epm/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
3 changes: 3 additions & 0 deletions epm/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.shortcuts import render

# Create your views here.
2 changes: 2 additions & 0 deletions polls/admin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from django.contrib import admin
from models import *

# Register your models here.
admin.site.register(Poll)

7 changes: 7 additions & 0 deletions post.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import requests
import httplib
import json
data = {"username":"afadfa","password":"123456","email":"[email protected]"}
headers = {"Content-Type":"application/json","Accept":"text/plain"}
conn = httplib.HTTPConnection('127.0.0.1:8000')
conn.request("POST","/rest/register/user/",json.dumps(data),headers)
Empty file added rest/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions rest/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
3 changes: 3 additions & 0 deletions rest/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.db import models

# Create your models here.
13 changes: 13 additions & 0 deletions rest/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from rest_framework import serializers
from django.contrib.auth.models import User
from polls.models import Poll

class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ('id','username','email')


class PollSerializer(serializers.ModelSerializer):
class Meta:
model = Poll
3 changes: 3 additions & 0 deletions rest/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
7 changes: 7 additions & 0 deletions rest/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.conf.urls import url,patterns
from rest import views

urlpatterns = patterns('',
url(r'^register/user/$',views.register_user),
url(r'^list/polls/$',views.operate_poll)
)
47 changes: 47 additions & 0 deletions rest/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from django.shortcuts import render
from rest_framework.decorators import api_view
from rest_framework import status
from rest_framework.response import Response
from rest_framework.parsers import JSONParser
from serializers import *
from django.views.decorators.csrf import csrf_exempt
from polls.models import Poll
import pdb

# Create your views here.

@csrf_exempt
@api_view(['POST'])
def register_user(request):
print 'aaaa'
print request.method
print request.DATA
print 'ddd'
user = UserSerializer(data=request.DATA)
if user.is_valid():
print 'valid'
pdb.set_trace()
u = User.objects.create_user(username=user.init_data['username'],email=user.init_data['email'],password=user.init_data['password'])
u.save()
return Response(user.data,status = status.HTTP_201_CREATED)
else:
pdb.set_trace()
return Response(uesr.errors,status = status.HTTP_400_BAD_REQUEST)

@api_view(['GET','POST'])
def operate_poll(request):
print 'aaaabbb'
if request.method == 'GET':
polls = Poll.objects.all()
pserializered = PollSerializer(polls,many=True)
return Response(pserializered.data)
if request.method == 'POST':
print 'method is:%s'%request.method
print request.DATA
p = PollSerializer(data=request.DATA)
if p.is_valid():
p.save()
return Response(p.data,status=status.HTTP_201_CREATED)
else:
return Response(p.errors,status = status.HTTP_400_BAD_REQUEST)

16 changes: 16 additions & 0 deletions tutorial/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,17 @@
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'django.contrib.comments',
'rest_framework',
'snippets',
'testuser',
'formtest',
'polls',
'tagging',
'mptt',
'zinnia',
'rest',
)

MIDDLEWARE_CLASSES = (
Expand Down Expand Up @@ -85,3 +91,13 @@
# https://docs.djangoproject.com/en/1.6/howto/static-files/

STATIC_URL = '/static/'

TEMPLATE_CONTEXT_PROCESSORS = (
'django.contrib.auth.context_processors.auth',
'django.core.context_processors.i18n',
'django.core.context_processors.request',
'zinnia.context_processors.version', # Optional
)


SITE_ID=1
3 changes: 3 additions & 0 deletions tutorial/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@
url(r'^',include('testuser.urls')),
url(r'^',include('formtest.urls')),
url(r'^polls/',include('polls.urls')),
url(r'^',include('rest.urls')),
url(r'^weblog/',include('zinnia.urls')),
url(r'^comments/',include('django.contrib.comments.urls')),
)

0 comments on commit 44d6c1a

Please sign in to comment.