Skip to content

Instantly share code, notes, and snippets.

@Yogendra0Sharma
Forked from ragowthaman/app.js
Created February 21, 2017 06:57
Show Gist options
  • Save Yogendra0Sharma/a7f82e568d7847685f4eea5fe155c4a6 to your computer and use it in GitHub Desktop.
Save Yogendra0Sharma/a7f82e568d7847685f4eea5fe155c4a6 to your computer and use it in GitHub Desktop.
Django Webservice Angular Ionic App setup
.state('tab.referral-registration-form', {
url: '/register/referral/form',
views: {
'tab-dash': {
templateUrl: 'templates/dash-referral-registration/dash-referral_form.html',
controller: 'RegisterReferralFormCtrl'
}
}
})
.state('tab.referral-registration-ack', {
url: '/register/referral/ack',
views: {
'tab-dash': {
templateUrl: 'templates/dash-referral-registration/dash-referral_reg_ack.html',
controller: 'RegisterReferralAckCtrl'
}
}
})
// That is end of Django server side setup for publishing a webservice.
// This serves the first instance via GET
// This allows a remote http request to CREATE a new entry in Referral model
http://localhost:8000/instance/webservice/register/referral/
Example JSON dataset is:
{
"farmer": 6,
"name": "Malathi Gowthaman",
"phone": 9500989012
}
.controller('RegisterReferralFormCtrl', function($scope, $localStorage, $stateParams, $state, $http) {
$scope.registerReferral = function(form) {
if(form.$valid) {
var data = {"name": form.name, "farmer": $localStorage.farmerid, "phone": form.phone};
$http({
method: 'PUT',
url: 'http://127.0.0.1:8000/instance/webservice/register/referral/',
data: data,
headers: {'Authorization': 'Basic '+ window.btoa( "*********:*********")}
}
).success(function(data, status, headers, config) {
// this callback will be called asynchronously
// when the response is available
console.log(data);
console.log(status);
}).error(function(data, status, headers, config) {
// called asynchronously if an error occurs
// or server returns response with an error status.
console.log(data);
console.log(status);
});
$state.go('tab.sowing-registration-ack');
}
};
})
from instance.serializer import ReferralSerializer
@api_view(['GET', 'PUT'])
def register_farmer_referral_via_webservice(request):
"""
Record the referrals coming from mobile applications.
:param request:
:return: success
"""
# do the operations
if request.method == "PUT":
serializer = ReferralSerializer(data=request.data)
print serializer
if serializer.is_valid():
print "serializer is valid"
serializer.save()
return Response(status=status.HTTP_201_CREATED)
else:
print "serializer is NOT valid"
print serializer.error_messages
print serializer.error
print serializer.data
return Response(serializer.errors, status=status.HTTP_204_NO_CONTENT)
elif request.method == 'GET':
serializer = ReferralSerializer(Referral.objects.all()[0])
return Response(serializer.data)
url(r'^webservice/register/referral/$', 'instance.views.register_farmer_referral_via_webservice'),
class Referal(models.Model):
farmer = models.ForeignKey(Farmer)
name = models.CharField(max_length=100)
phone = models.BigIntegerField()
<ion-view view-title="Referral">
<ion-content class="padding">
<h4>Referral Registration is successful! Thanks!</h4>
<span>Your Friend will be contacted soon. If he/she fails to receive a call from us in 72 hours, or if the matter is urgent please don't hesitate to call us right away.</span>
<span>Our Phone Number:</span><span>{{ universalphone }}</span>
</ion-content>
</ion-view>
<ion-view view-title="Referral">
<ion-content class="padding">
<form name="referralRegistrationForm" novalidate="" ng-submit="registerReferral(referralRegistrationForm)">
<div class="list list-section">
<label class="item item-input item-stacked-label">
<span class="input-label">Name:</span>
<input type="text" name="name" ng-model="referralRegistrationForm.name" placeholder="eg., Ramasamy Kaliannan">
</label>
<label class="item item-input item-stacked-label">
<span class="input-label">Phone:</span>
<input type="text" name="phone" ng-model="referralRegistrationForm.phone" placeholder="eg., 1234567890">
</label>
<button class="button button-full button-positive" type="submit">
Register
</button>
</div>
</form>
</ion-content>
</ion-view>
class ReferralSerializer(serializers.ModelSerializer):
class Meta:
model = Referral
fields = ('farmer', 'name', 'phone')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment