Example #1
0
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})
Example #2
0
def send_property_to_newsletters(sender, **kwargs):
    instance = kwargs['instance']
    if instance.is_active:
        instance_data = ItemSerializer(instance).data
        images_items = instance.images.all()
        is_updated = not SendNewsletterAfterActivating.objects.filter(item_id=instance.item_id).exists() and \
                     instance.is_updated
        context = {
            "backend_url": settings.BACKEND_URL_ROOT,
            "environment": settings.ENVIRONMENT,
            "logo_url": SettingsDb.get_emails_image(),
            "property_description": instance_data['description'],
            "property_id": instance_data['pk'],
            "property_images": images_items,
            "property_label": instance_data['label'],
            "property_short_description": instance_data['short_description'],
            "is_updated": is_updated,
            "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('item/new_item_template.html').render(
            context)
        text_content = get_template('item/new_item_template.txt').render(
            context)
        newsletters_emails = [
            newsletter_email['email']
            for newsletter_email in Newsletter.objects.filter(
                is_active=True).values('email')
        ]
        msg = EmailMultiAlternatives(_('Une propriété a été mise à jour'
                                       if is_updated else 'New property'),
                                     text_content,
                                     settings.EMAIL_HOST_USER,
                                     newsletters_emails[0:1],
                                     bcc=newsletters_emails[1:])
        msg.attach_alternative(html_content, "text/html")
        msg.content_subtype = 'html'
        msg.mixed_subtype = 'related'
        for item_image in images_items:
            # Create an inline attachment
            ext = '.' + item_image.image.url.split('.')[-1]
            image = MIMEImage(item_image.image.read(), _subtype=ext)
            image.add_header('Content-ID',
                             '<{}>'.format(item_image.image_filename))
            msg.attach(image)

        # try:
        msg.send()
        if not is_updated:
            SendNewsletterAfterActivating.objects.filter(
                item_id=instance.item_id).delete()
Example #3
0
def email_de_rappel_de_paiement(is_test=False,
                                to_email_test="*****@*****.**"):
    site_name = SettingsDb.get_site_name()
    social_links = get_list_social_links()
    social_links_images = get_list_social_links_images()
    for group_client in GroupClient.objects.filter(is_active=True):
        reminder_email_data = group_client.to_reminder_email_data()
        context = {
            "backend_url": settings.BACKEND_URL_ROOT,
            "environment": settings.ENVIRONMENT,
            "logo_url": SettingsDb.get_emails_image(),
            "message_1": reminder_email_data['message_1'],
            "message_2": reminder_email_data['message_2'],
            "message_3": reminder_email_data['message_3'],
            "footer_1": reminder_email_data['footer_1'],
            "footer_2": reminder_email_data['footer_2'],
            "site_name": site_name,
            "site_url_root": settings.SITE_URL_ROOT,
            "social_links": social_links,
            "social_links_images": social_links_images,
        }
        html_content = get_template(
            'client/payment_reminder_email.html').render(context)
        text_content = get_template(
            'client/payment_reminder_email.txt').render(context)
        if is_test:
            # f = open("/home/ubuntu/log_immobilier_backend/test_log.txt", "w+")
            # f.write("hfgfhfhfh \r\n")
            # f.write("This is line {}\r\n".format(str(is_test)))
            # f.write("This is line {}\r\n".format(to_email_test))
            clients_emails = [
                to_email_test if to_email_test else "*****@*****.**"
            ]
            # f.close()
        else:
            clients_emails = [
                client['email'] for client in group_client.clients.filter(
                    is_active=True, type="locataire").values('email')
            ]
        msg = EmailMultiAlternatives(
            reminder_email_data['object'],
            text_content,
            settings.EMAIL_HOST_USER,
            clients_emails[0:1],
            bcc=clients_emails[1:],
        )
        msg.attach_alternative(html_content, "text/html")
        msg.content_subtype = 'html'
        msg.mixed_subtype = 'related'
        msg.send()
Example #4
0
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)
Example #5
0
def send_property_to_newsletters(sender, **kwargs):
    instance = kwargs['instance']
    if instance.is_active:
        instance_data = ItemSerializer(instance).data
        images_items = instance.images.all()
        is_updated = not SendNewsletterAfterActivating.objects.filter(
            item_id=instance.item_id).exists()
        context = {
            "backend_url": settings.BACKEND_URL_ROOT,
            "environment": settings.ENVIRONMENT,
            "logo_url": SettingsDb.get_emails_image(),
            "property_description": instance_data['description'],
            "property_id": instance_data['pk'],
            "property_images": images_items,
            "property_label": instance_data['label'],
            "property_short_description": instance_data['short_description'],
            "is_updated": is_updated,
            "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('item/new_item_template.html').render(
            context)
        text_content = get_template('item/new_item_template.txt').render(
            context)
        newsletters_emails = [
            newsletter_email['email']
            for newsletter_email in Newsletter.objects.filter(
                is_active=True).values('email')
        ]
        args_tuple = (is_updated, instance.item_id)
        EmailThread(
            _('Une propriété a été mise à jour'
              if is_updated else 'Nouvelle propriété'), text_content,
            settings.EMAIL_HOST_USER, newsletters_emails, False, html_content,
            images_items, args_tuple).start()
Example #6
0
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})
Example #7
0
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)
Example #8
0
def global_params(request):
    return JsonResponse({
        "home_page_data": SettingsDb.get_home_page_data(),
        "realtor_data": AdminData.get_admin_data(),
        "selects_choices": {
            "CITIES": [[canadian_city[0],
                        _(canadian_city[1])]
                       for canadian_city in static_variables.CANADIAN_CITIES],
            "PROPERTIES_TYPES":
            [[property_type[0], _(property_type[1])]
             for property_type in static_variables.PROPERTIES_TYPES],
            "BUILDINGS_TYPES":
            [[building_type[0], _(building_type[1])]
             for building_type in static_variables.BUILDINGS_TYPES],
            "BEDROOMS_NUMBER":
            [[bedrooms_number[0], _(bedrooms_number[1])]
             for bedrooms_number in static_variables.BEDROOMS_NUMBER],
            "BATHROOMS_NUMBER":
            [[bathrooms_number[0], _(bathrooms_number[1])]
             for bathrooms_number in static_variables.BATHROOMS_NUMBER],
            "CONSTRUCTION_AGE":
            [[construction_age[0], _(construction_age[1])]
             for construction_age in static_variables.CONSTRUCTION_AGE],
            "ITEMS_STATUS": [[item_status[0],
                              _(item_status[1])]
                             for item_status in static_variables.ITEMS_STATUS],
            "PRICES_RANGES": [[price_range[0],
                               _(price_range[1])]
                              for price_range in static_variables.PRICES_RANGES
                              ]
        },
        "selects_choices_dict": {
            "CITIES":
            choices_format_to_dict(static_variables.CANADIAN_CITIES),
            "PROPERTIES_TYPES":
            choices_format_to_dict(static_variables.PROPERTIES_TYPES),
            "BUILDINGS_TYPES":
            choices_format_to_dict(static_variables.BUILDINGS_TYPES),
            "BEDROOMS_NUMBER":
            choices_format_to_dict(static_variables.BEDROOMS_NUMBER),
            "BATHROOMS_NUMBER":
            choices_format_to_dict(static_variables.BATHROOMS_NUMBER),
            "CONSTRUCTION_AGE":
            choices_format_to_dict(static_variables.CONSTRUCTION_AGE),
            "ITEMS_STATUS":
            choices_format_to_dict(static_variables.ITEMS_STATUS),
            "PRICES_RANGES":
            choices_format_to_dict(static_variables.PRICES_RANGES)
        },
        "is_maps_active": added_settings.IS_MAPS_ACTIVE,
        "footer_params": {
            "socialLinks": get_list_social_links(),
            "site_name": SettingsDb.get_site_name(),
            "site_url_root": settings.SITE_URL_ROOT
        },
        "header_params": {
            "i18n": added_settings.I18N_ACTIVE,
            "langue_label": _("French"),
            "langue_url": settings.SITE_URL_ROOT,
            "realtor_data": AdminData.get_admin_data(),
            "header_settings": SettingsDb.get_header_settings()
        }
    })
Example #9
0
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)
Example #10
0
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})
Example #11
0
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)