Created
June 19, 2017 15:36
-
-
Save SanskarSans/84c2ba3a5087f271a8273c8fdd5ed8f9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db import models | |
from django.conf import settings | |
from django.core.urlresolvers import reverse | |
from django.utils.translation import ugettext_lazy as _ | |
from smart_selects.db_fields import ChainedForeignKey | |
class PropertyType(models.Model): | |
"""Class that implements properties types, as House or Apartment.""" | |
name = models.CharField(_('Type of Property'), max_length=140) | |
class Meta: | |
verbose_name = _("Type of Property") | |
verbose_name_plural = _('Type of Properties') | |
def __str__(self): | |
return self.name | |
class Category(models.Model): | |
"""Model for category of properties.""" | |
title = models.CharField(_('Category'), max_length=140) | |
property_type = models.ForeignKey(PropertyType) | |
class Meta: | |
verbose_name = _("Category") | |
verbose_name_plural = _('Categories') | |
def __str__(self): | |
return self.title | |
class Property(models.Model): | |
"""Model for all properties.""" | |
owner = models.ForeignKey(settings.AUTH_USER_MODEL) | |
address = models.CharField(_('Address'), max_length=140) | |
state = models.CharField(_('State'), max_length=80) | |
property_type = models.ForeignKey(PropertyType) | |
category = ChainedForeignKey( | |
Category, | |
chained_field='property_type', | |
chained_model_field='property_type' | |
) | |
rooms = models.PositiveSmallIntegerField(_('Rooms')) | |
title = models.CharField(_('Title'), max_length=140) | |
image = models.ImageField(_('Image'), upload_to='img/property/') | |
rent_price = models.DecimalField( | |
_('Property Value'), | |
max_digits=19, | |
decimal_places=10 | |
) | |
timestamp = models.DateTimeField(auto_now=True) | |
class Meta: | |
verbose_name = _("Property") | |
verbose_name_plural = _('Properties') | |
def __str__(self): | |
return self.title | |
def get_absolute_url(self): | |
"""Return the absolute url for the model instance.""" | |
return reverse('properties:show_property', args=[self.id]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PropertySerializer(serializers.ModelSerializer): | |
owner = serializers.ReadOnlyField(source='owner.username') | |
class Meta: | |
model = Property | |
fields = ('id', 'owner', 'address', 'state', 'city', 'district', 'rooms', 'util_area', 'total_area', | |
'title', 'image', 'description', 'rent_price', 'timestamp', ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
url(r'^property/$', views.PropertyView.as_view(), name='property'), | |
url(r'^property/(?P<property_id>[0-9]*)/$', views.PropertyView.as_view(), name='property-detail'), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PropertyView(APIView): | |
permission_classes = (IsAuthenticated,) # explicit | |
serializer_class = PropertySerializer | |
def get_object(self, pk): | |
try: | |
return Property.objects.get(pk=pk) | |
except Property.DoesNotExist: | |
raise Http404 | |
def get(self, request, property_id=None, format=None): | |
""" Get all property or a single property """ | |
if property_id: | |
property_instance = self.get_object(property_id) | |
serializer = self.serializer_class(property_instance) | |
return Response(serializer.data) | |
else: | |
properties = Property.objects.filter(owner=request.user.id) | |
serializer = self.serializer_class(properties, many=True) | |
return Response(serializer.data) | |
def post(self, request, format=None): | |
""" Adding a new property""" | |
serializer = self.serializer_class(data=request.data) | |
if not serializer.is_valid(): | |
return Response(serializer.errors, status= status.HTTP_400_BAD_REQUEST) | |
else: | |
data = serializer.data | |
owner = request.user | |
property_instance = Property(owner=owner, address=data['address']) # verified has to be added | |
property_instance.save() | |
request.data['id'] = property_instance.pk # return id | |
return Response(request.data, status=status.HTTP_201_CREATED) | |
def put(self, request, property_id, format=None): | |
""" Updating a property """ | |
serializer = self.serializer_class(data=request.data) | |
if not serializer.is_valid(): | |
return Response(serializer.errors, status= status.HTTP_400_BAD_REQUEST) | |
else: | |
data = serializer.data | |
address = data['address'] | |
property_instance = Property(id=property_id, owner=request.user, address=address) | |
property_instance.save() | |
return Response(status=status.HTTP_200_OK) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment