Skip to content

Instantly share code, notes, and snippets.

@Marco-Rod
Created September 16, 2019 20:17
Show Gist options
  • Save Marco-Rod/92206bc957ce2a133f52490bf5a215ad to your computer and use it in GitHub Desktop.
Save Marco-Rod/92206bc957ce2a133f52490bf5a215ad to your computer and use it in GitHub Desktop.
class DetailPropertyPDF(PDFTemplateResponseMixin, DetailView):
model = Property
template_name = 'reports/detail_property.html'
context_object_name = 'property'
URL_STATIC = "{}{}".format(APPS_DIR, STATIC_URL)
URL_MEDIA = "{}{}".format(APPS_DIR, MEDIA_URL)
MAIN_SERVICES = [
{
'category': 'airport',
'name': 'Aeropuerto Internacional de Guadalajara Miguel Hidalgo y Costilla',
'address': 'Carr. Guadalajara Chapala Km 17.5',
'city': 'Tlajomulco de Zuñiga',
'latitude': 20.525708,
'longitude': -103.308048
},
{
'category': 'bus',
'name': 'Central de autobuses Guadalajara ',
'address': '',
'city': 'San Pedro Tlaquepaque',
'latitude': 20.621560,
'longitude': -103.285844
}
]
def get_context_data(self,**kwargs):
context = super().get_context_data(**kwargs)
if len(self.object.forsquare) == 0:
Forsquare.get_foursquare_property(self.object)
CodeQR.create_code(url='www.inkuvi.com', name='{}.png'.format(self.object.id))
if self.object.video_show:
CodeQR.create_code(url=self.object.video_show, name='video_{}.png'.format(self.object.id))
images = Image.objects.filter(property=self.object, type_image=TYPE_HOUSE, status=IMAGE_UPLOAD).order_by('is_front')
if images.count() >= 6:
images = images[:6]
blueprints = Image.objects.filter(property=self.object, type_image=BLUEPRINTS, status=IMAGE_UPLOAD)
if len(images) > 0:
url = images.last().url
img_front = base64.b64encode(requests.get(url).content)
img_front = img_front.decode("utf-8")
context['img_fachada'] = 'data:image/png;base64, {}'.format(img_front)
if self.object.location:
json_position = json.loads(self.object.location.geojson)
url_map = "https://maps.googleapis.com/maps/api/staticmap?size=370x300&markers=color:red%7Clabel:C%7C{},{}&key=AIzaSyBQwIOZMg6LfJPiVTULgqUDZX8O7dPDchU".format(json_position['coordinates'][0], json_position['coordinates'][1])
img_map = base64.b64encode(requests.get(url_map).content)
img_map = img_map.decode("utf-8")
context['img_map'] = 'data:image/png;base64, {}'.format(img_map)
list_images_first = []
list_images_second = []
list_images_third = []
for index, image in enumerate(images, 1):
url = image.url
img = base64.b64encode(requests.get(url).content)
img = img.decode("utf-8")
if index in [1, 2]:
list_images_first.append({
"image": 'data:image/{};base64, {}'.format(image.name.split(".")[::-1][0].lower(), img),
"description": image.description
})
elif index in [3, 4]:
list_images_second.append({
"image": 'data:image/{};base64, {}'.format(image.name.split(".")[::-1][0].lower(), img),
"description": image.description
})
elif index in [5, 6]:
list_images_third.append({
"image": 'data:image/{};base64, {}'.format(image.name.split(".")[::-1][0].lower(), img),
"description": image.description
})
list_blueprints = []
for image in blueprints:
url = image.url
img = base64.b64encode(requests.get(url).content)
img = img.decode("utf-8")
list_blueprints.append({
"image": 'data:image/{};base64, {}'.format(image.name.split(".")[::-1][0], img),
"description": image.description
})
for category in CATEGORIES:
if category in self.object.forsquare:
list_category = self.object.forsquare.get(category)
if len(list_category) >= 3:
context['list_{}'.format(category)] = list_category[:3]
else:
context['list_{}'.format(category)] = list_category
for place in self.MAIN_SERVICES:
json_position = json.loads(self.object.location.geojson)
dict_distance = {'distance': OperationPoint.calculate_distance(
{'latitude': json_position['coordinates'][0], 'longitude': json_position['coordinates'][1]},
{'latitude': place.get('latitude'), 'longitude': place.get('longitude')}
)}
dict_place = place
context[place.get('category')] = {**dict_distance, **dict_place}
context['professional_personalities'] = ', '.join(
[personality.name for personality in self.object.personality.all() if
personality.created == INICIALMENTE and personality.group == PROFESIONAL])
context['family_personalities'] = ', '.join(
[personality.name for personality in self.object.personality.all() if
personality.created == INICIALMENTE and personality.group == CIVIL_STATUS])
context['personal_personalities'] = ', '.join(
[personality.name for personality in self.object.personality.all() if
personality.created == INICIALMENTE and personality.group == PERSONALITY])
context['other_personalities'] = ', '.join(
[personality.name for personality in self.object.personality.all() if
personality.created == WEB])
with open('{}{}'.format(APPS_DIR, static('dist/img/user_160x160.png')), 'rb') as file:
encoded_string = base64.b64encode(file.read())
context['coach_photo'] = 'data:image/png;base64, {}'.format(encoded_string.decode("utf-8"))
photos = FileCoach.objects.filter(user=self.request.user, type=FileCoach.TYPE_FILE.photo)
if photos.count() > 0:
photo = photos.last()
if 'url' in photo.response:
img = base64.b64encode(requests.get(photo.response['url']).content)
img = img.decode("utf-8")
context['coach_photo'] = 'data:image/png;base64, {}'.format(img)
context['coach_name'] = self.request.user.full_name
context['coach_phone'] = ''
if self.request.user.phone_number:
context['coach_phone'] = self.request.user.phone_number[3:]
context['coach_email'] = self.request.user.username
context['list_blueprints'] = list_blueprints
context['images_property_first'] = list_images_first
context['images_property_second'] = list_images_second
context['images_property_third'] = list_images_third
context['price'] = "{:,.2f}".format(self.object.price)
context['static'] = self.URL_STATIC
context['media'] = self.URL_MEDIA
context['date'] = datetime.datetime.now()
context['user'] = self.request.user
return context
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment