def newsletter_unsubscribe(request): """ user unsubscription from the newsletter. """ user_email = request.GET.get("user_email") if not Newsletter.objects.filter(email=user_email).exists(): response = { "success": False, "message": _("Sorry, you haven't any subscription in our newsletter!"), } else: subscription = Newsletter.objects.get(email=user_email) if subscription.is_active: Newsletter.objects.filter(email=user_email).update(is_active=False) context = { "environment": settings.ENVIRONMENT, "first_name": subscription.first_name, "last_name": subscription.last_name, "logo_url": SettingsDb.get_emails_image(), "resubscribe_url": "{site_url_root}/#/newsletter/resubscribe/{user_email}".format( site_url_root=settings.SITE_URL_ROOT, user_email=subscription.email), "site_name": SettingsDb.get_site_name(), "site_url_root": settings.SITE_URL_ROOT, "show_unsubscribe_url": True, "social_links": get_list_social_links(), "social_links_images": get_list_social_links_images() } html_content = get_template( 'newsletter/unsubscription_template.html').render(context) text_content = get_template( 'newsletter/unsubscription_template.txt').render(context) send_email(_("Unsubscription from the newsletter"), text_content, settings.EMAIL_HOST_USER, subscription.email, html_content) response = { "success": True, "message": _("Your subscription is deactivated!"), } else: response = { "success": False, "message": _("Your subscription is already deactivated!"), } return Response({'data': response})
def contact_sell_create(request): """ Create a contact entry for selling a property. """ if request.method == 'POST': data = request.data if data.get("email") and ( data.get("first_name") or data.get("last_name") ) and not Newsletter.objects.filter(email=data["email"]).exists(): Newsletter.objects.create(email=data["email"], first_name=data["first_name"] or data["last_name"], last_name=data["last_name"] or data["first_name"]) serializer = ContactSellSerializer(data=data) if serializer.is_valid(): serializer.save() context = { "email": data['email'], "environment": settings.ENVIRONMENT, "first_name": data['first_name'], "last_name": data['last_name'], "logo_url": SettingsDb.get_emails_image(), "message": data['message'], "site_name": SettingsDb.get_site_name(), "site_url_root": settings.SITE_URL_ROOT, "show_unsubscribe_url": True, "social_links": get_list_social_links(), "social_links_images": get_list_social_links_images(), "phone": data.get('phone', "") } html_content = get_template( 'contact/contact_template.html').render(context) text_content = get_template('contact/contact_template.txt').render( context) send_email(data["object"], text_content, data["email"], AdminData.get_admin_email(), html_content) return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def contact(request): """ send mail from client to administrator. """ if request.method == 'POST': data = request.data if data.get("email") and ( data.get("first_name") or data.get("last_name") ) and not Newsletter.objects.filter(email=data["email"]).exists(): Newsletter.objects.create(email=data["email"], first_name=data["first_name"] or data["last_name"], last_name=data["last_name"] or data["first_name"]) context = { "email": data['email'], "environment": settings.ENVIRONMENT, "first_name": data['first_name'], "last_name": data['last_name'], "logo_url": SettingsDb.get_emails_image(), "message": data['message'], "site_name": SettingsDb.get_site_name(), "site_url_root": settings.SITE_URL_ROOT, "show_unsubscribe_url": True, "social_links": get_list_social_links(), "social_links_images": get_list_social_links_images(), "phone": data.get('phone', ""), "property_url": data.get('property_url', '') } html_content = get_template('contact/contact_template.html').render( context) text_content = get_template('contact/contact_template.txt').render( context) send_email(data["object"], text_content, data["email"], AdminData.get_admin_email(), html_content) return Response({"success": True})
def newsletter_create(request): """ user subscription to the newsletter. """ data = request.data already_exists = False subscription = Newsletter.objects.filter(email=data["email"]) if subscription.exists(): subscription = subscription[0] if Newsletter.objects.filter(email=data["email"], is_active=True, first_name=data["first_name"], last_name=data["last_name"]).exists(): pass else: if not subscription.is_active: response_type = "reactivated" else: response_type = "updated" already_exists = True Newsletter.objects.filter(email=data["email"]).update( is_active=True, first_name=data["first_name"], last_name=data["last_name"]) if not already_exists: serializer = NewsletterSerializer(data=data) if serializer.is_valid(): serializer.save() response_type = "created" else: response_type = "conflict" if response_type != "conflict": footer_text_content = get_template( 'newsletter/footer_email.txt').render({ "unsubscribe_url": "{site_url_root}/#/newsletter/unsubscribe/{user_email}".format( site_url_root=settings.SITE_URL_ROOT, user_email=data["email"]) }) context = { "environment": settings.ENVIRONMENT, "first_name": data['first_name'], "footer_text_content": footer_text_content, "last_name": data['last_name'], "logo_url": SettingsDb.get_emails_image(), "site_name": SettingsDb.get_site_name(), "site_url_root": settings.SITE_URL_ROOT, "social_links": get_list_social_links(), "social_links_images": get_list_social_links_images(), "unsubscribe_url": "{site_url_root}/#/newsletter/unsubscribe/{user_email}".format( site_url_root=settings.SITE_URL_ROOT, user_email=data["email"]), "show_unsubscribe_url": True } html_content = get_template( 'newsletter/subscription_template.html').render(context) text_content = get_template( 'newsletter/subscription_template.txt').render(context) send_email(_("Subscription to the newsletter"), text_content, settings.EMAIL_HOST_USER, data["email"], html_content) if response_type in ["updated", "reactivated"]: return Response({"response_type": response_type}, status=status.HTTP_201_CREATED) else: return Response( { "data": serializer.data, "response_type": response_type }, status=status.HTTP_201_CREATED) else: return Response(serializer.errors, status=status.HTTP_409_CONFLICT)
def contact_buy_create(request): """ Create a contact entry for buying a property. """ data = request.data.copy() if data.get("email") and ( data.get("first_name") or data.get("last_name") ) and not Newsletter.objects.filter(email=data["email"]).exists(): Newsletter.objects.create(email=data["email"], first_name=data["first_name"] or data["last_name"], last_name=data["last_name"] or data["first_name"]) if not data.get("lot_size_max", ""): data["lot_size_max"] = 0 data["lot_size_min"] = 0 if not data.get("lot_size_min", ""): data["lot_size_min"] = 0 try: data["occupation_date"] = datetime.datetime.strptime( data.get("occupation_date"), "%d-%m-%Y").date() except: data["occupation_date"] = None serializer = ContactBuySerializer(data=data) if serializer.is_valid(): serializer.save() context = { "email": data['email'], "environment": settings.ENVIRONMENT, "first_name": data['first_name'], "last_name": data['last_name'], "logo_url": SettingsDb.get_emails_image(), "phone": data.get('phone', ""), "property_information": [(_("Bathrooms number"), data.get('bathrooms_number_text', "") if data.get('bathrooms_number', "") else ""), (_("Bedrooms number"), data.get('bedrooms_number_text', "") if data.get('bedrooms_number', "") else ""), (_("Building type"), data.get('building_type_text', "") if data.get('building_type', "") else ""), (_("City"), data.get('city_text', "") if data.get('city', "") else ""), (_("Construction age"), data.get('construction_age_text', "") if data.get('construction_age', "") else ""), (_("Dining room"), data.get('has_dining_room', False)), (_("Fireplace"), data.get('has_fireplace', False)), (_("Garage"), data.get('has_garage', False)), (_("Garden"), data.get('has_garden', False)), (_("Property size max"), str(data['lot_size_max']) if int(data.get('lot_size_max', 0)) > 0 else ""), (_("Property size min"), str(data['lot_size_min']) if int(data.get('lot_size_min', 0)) > 0 else ""), (_("Price range"), data.get('price_range_text', "") if data.get( 'price_range', "") else ""), (_("Property type"), data.get('property_type_text', "") if data.get('property_type', "") else ""), (_("Occupation date"), data.get('occupation_date', "")), (_("Other characteristics"), data.get('other_characteristics', "")), (_("Status"), data.get('status_text', "")), (_("Swimming pool"), data.get('has_swimming_pool', False))], "site_name": SettingsDb.get_site_name(), "site_url_root": settings.SITE_URL_ROOT, "show_unsubscribe_url": True, "social_links": get_list_social_links(), "social_links_images": get_list_social_links_images() } html_content = get_template( 'contact/contact_buying_template.html').render(context) text_content = get_template( 'contact/contact_buying_template.txt').render(context) send_email(_("Buying property"), text_content, data["email"], AdminData.get_admin_email(), html_content) return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def newsletter_resubscribe(request): """ user resubscription from the newsletter. """ user_email = request.GET.get("user_email") if not Newsletter.objects.filter(email=user_email).exists(): response = { "success": False, "message": _("Désolé, vous n'êtes pas inscrit à notre newsletter!"), } else: subscription = Newsletter.objects.get(email=user_email) if not subscription.is_active: Newsletter.objects.filter(email=user_email).update(is_active=True) footer_text_content = get_template( 'newsletter/footer_email.txt').render({ "unsubscribe_url": "{site_url_root}/#/newsletter/unsubscribe/{user_email}". format(site_url_root=settings.SITE_URL_ROOT, user_email=subscription.email) }) context = { "environment": settings.ENVIRONMENT, "first_name": subscription.first_name, "footer_text_content": footer_text_content, "last_name": subscription.last_name, "logo_url": SettingsDb.get_emails_image(), "site_name": SettingsDb.get_site_name(), "site_url_root": settings.SITE_URL_ROOT, "show_unsubscribe_url": True, "social_links": get_list_social_links(), "social_links_images": get_list_social_links_images(), "unsubscribe_url": "{site_url_root}/#/newsletter/unsubscribe/{user_email}".format( site_url_root=settings.SITE_URL_ROOT, user_email=subscription.email) } html_content = get_template( 'newsletter/resubscription_template.html').render(context) text_content = get_template( 'newsletter/resubscription_template.txt').render(context) send_email(_("Réinscription à la newsletter"), text_content, settings.EMAIL_HOST_USER, subscription.email, html_content) response = { "success": True, "message": _("Votre abonnement est réactivé!"), } else: response = { "success": False, "message": _("Votre abonnement est déjà activé!"), } return Response({'data': response})
def contact_rent_create(request): """ Create a contact entry for rent a property. """ data = request.data.copy() if data.get("email") and ( data.get("first_name") or data.get("last_name") ) and not Newsletter.objects.filter(email=data["email"]).exists(): Newsletter.objects.create(email=data["email"], first_name=data["first_name"] or data["last_name"], last_name=data["last_name"] or data["first_name"]) if not data.get("lot_size_max", ""): data["lot_size_max"] = 0 data["lot_size_min"] = 0 if not data.get("lot_size_min", ""): data["lot_size_min"] = 0 try: data["occupation_date"] = datetime.datetime.strptime( data.get("occupation_date"), "%d-%m-%Y").date() except: data["occupation_date"] = None serializer = ContactRentSerializer(data=data) if serializer.is_valid(): serializer.save() context = { "email": data['email'], "environment": settings.ENVIRONMENT, "first_name": data['first_name'], "last_name": data['last_name'], "logo_url": SettingsDb.get_emails_image(), "phone": data.get('phone', ""), "property_information": [(_("Nombre de salles de bain"), data.get('bathrooms_number_text', "") if data.get( 'bathrooms_number', "") else ""), (_("Nombre de chambres"), data.get('bedrooms_number_text', "") if data.get('bedrooms_number', "") else ""), (_("Type de bâtiment"), data.get('building_type_text', "") if data.get('building_type', "") else ""), (_("Ville"), data.get('city_text', "") if data.get('city', "") else ""), (_("Âge de construction"), data.get('construction_age_text', "") if data.get('construction_age', "") else ""), (_("Salle à manger"), data.get('has_dining_room', False)), (_("Cheminée"), data.get('has_fireplace', False)), (_("Garage"), data.get('has_garage', False)), (_("Jardin"), data.get('has_garden', False)), (_("Taille de la propriété maximale"), str(data['lot_size_max']) if int(data.get('lot_size_max', 0)) > 0 else ""), (_("Taille de la propriété minimale"), str(data['lot_size_min']) if int(data.get('lot_size_min', 0)) > 0 else ""), (_("Intervalle de prix"), data.get('price_range_text', "") if data.get('price_range', "") else ""), (_("Type de la Propriété"), data.get('property_type_text', "") if data.get('property_type', "") else ""), (_("Date d'occupation"), data.get('occupation_date', "")), (_("Autres caractéristiques"), data.get('other_characteristics', "")), (_("Statut"), data.get('status_text', "")), (_("Piscine"), data.get('has_swimming_pool', False))], "site_name": SettingsDb.get_site_name(), "site_url_root": settings.SITE_URL_ROOT, "show_unsubscribe_url": True, "social_links": get_list_social_links(), "social_links_images": get_list_social_links_images() } html_content = get_template( 'contact/contact_renting_template.html').render(context) text_content = get_template( 'contact/contact_renting_template.txt').render(context) send_email(_("location d'une propriété"), text_content, data["email"], AdminData.get_admin_email(), html_content) return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)