예제 #1
0
파일: views.py 프로젝트: Maxence42/balafon
    def post(self, request, *args, **kwargs):
        """HTTP POST: handle form"""

        form_class = self.get_form_class()

        form = form_class(request.POST, request.FILES, **self.get_form_kwargs())
        if form.is_valid():
            contact = form.save(request)
            try:
                send_verification_email(contact)
                return HttpResponseRedirect(self.get_success_url(contact))
            except EmailSendError:
                except_text = 'send_verification_email'
                logger.exception(except_text)
                
                #create action
                detail = _(u"An error occurred while verifying the email address of this contact.")
                fix_action = ActionType.objects.get_or_create(name=_(u'Balafon'))[0]
                action = Action.objects.create(
                    subject=_(u"Need to verify the email address"), planned_date=now_rounded(),
                    type=fix_action, detail=detail, display_on_board=True,
                )
                action.contacts.add(contact)
                action.save()
                
                return HttpResponseRedirect(self.get_error_url(contact))
        else:
            except_text = u'contact form : {0}'.format(form.errors)
            logger.warning(except_text)
        return self._display_form(form)
예제 #2
0
def post_message(request):
    try:
        profile = request.user.contactprofile
    except ContactProfile.DoesNotExist:
        raise Http404

    if not profile.contact:
        raise Http404
    
    if request.method == "POST":
        
        form = MessageForm(request.POST)
        if form.is_valid():
            
            message = form.cleaned_data['message']
            
            # send message by email
            notification_email = getattr(settings, 'BALAFON_NOTIFICATION_EMAIL', '')
            if notification_email:
                from_email = getattr(settings, 'DEFAULT_FROM_EMAIL')
                
                data = {
                    'contact': profile.contact,
                    'message': message,
                    'site': settings.COOP_CMS_SITE_PREFIX,
                }
                template_ = get_template('Emailing/subscribe_notification_email.txt')
                content = template_.render(data)
                email = EmailMessage(
                    _("Message from web site"), content, from_email,
                    [notification_email], headers = {'Reply-To': profile.contact.email})
                try:
                    email.send()
                    messages.add_message(request, messages.SUCCESS, _("The message have been sent"))
                except Exception:
                    messages.add_message(request, messages.ERROR, _("The message couldn't be send."))
                    
            # add an action
            message_action, _is_new = ActionType.objects.get_or_create(name=_('Message'))
            Action.objects.create(
                subject=_("New message on web site"), planned_date=now_rounded(),
                type=message_action, detail=message, contact=profile.contact, display_on_board=True
            )
        
            return HttpResponseRedirect(reverse('homepage'))
    else:
        form = MessageForm()
        
    return render(
        request,
        'Profile/post_message.html',
        {
            'contact': profile.contact,
            'form': form,
        }
    )
예제 #3
0
파일: views.py 프로젝트: ljean/balafon
    def post(self, request, *args, **kwargs):
        """HTTP POST: handle form"""

        form_class = self.get_form_class()

        form = form_class(request.POST, request.FILES,
                          **self.get_form_kwargs())
        if form.is_valid():
            contact = form.save(request)
            if self.require_confirmation():
                contact.confirmed = False
                contact.save()
                entity = contact.entity
                entity.confirmed = False
                entity.save()
            try:
                subscription_queryset = contact.subscription_set.filter(
                    accept_subscription=True)

                subscription_types = []
                for subscription in subscription_queryset:
                    new_subscription.send(sender=Subscription,
                                          instance=subscription,
                                          contact=contact)
                    subscription_types.append(subscription.subscription_type)

                send_verification_email(contact, subscription_types)
                return HttpResponseRedirect(self.get_success_url(contact))
            except EmailSendError:
                except_text = 'send_verification_email'
                logger.exception(except_text)

                # create action
                detail = _(
                    "An error occurred while verifying the email address of this contact."
                )
                fix_action = ActionType.objects.get_or_create(
                    name=_('Balafon'))[0]
                action = Action.objects.create(
                    subject=_("Need to verify the email address"),
                    planned_date=now_rounded(),
                    type=fix_action,
                    detail=detail,
                    display_on_board=True,
                )
                action.contacts.add(contact)
                action.save()

                return HttpResponseRedirect(self.get_error_url(contact))
        else:
            except_text = 'contact form : {0}'.format(form.errors)
            logger.warning(except_text)
        return self._display_form(form)
예제 #4
0
파일: views.py 프로젝트: Maxence42/balafon
def view_link(request, link_uuid, contact_uuid):
    """view magic link"""
    link = get_object_or_404(models.MagicLink, uuid=link_uuid)

    try:
        contact = Contact.objects.get(uuid=contact_uuid)
        link.visitors.add(contact)
        
        #create action
        link_action = ActionType.objects.get_or_create(name=_(u'Link'))[0]
        action = Action.objects.create(
            subject=link.url, planned_date=now_rounded(),
            type=link_action, detail='', done=True, display_on_board=False,
            done_date=now_rounded()
        )
        action.contacts.add(contact)
        action.save()
        
    except Contact.DoesNotExist:
        pass

    return HttpResponseRedirect(link.url)
예제 #5
0
파일: views.py 프로젝트: ljean/balafon
def view_link(request, link_uuid, contact_uuid):
    """view magic link"""
    link = get_object_or_404(models.MagicLink, uuid=link_uuid)

    try:
        contact = Contact.objects.get(uuid=contact_uuid)
        link.visitors.add(contact)

        # create action
        link_action = ActionType.objects.get_or_create(name=_('Link'))[0]
        action = Action.objects.create(subject=link.url[:200],
                                       planned_date=now_rounded(),
                                       type=link_action,
                                       detail='',
                                       done=True,
                                       display_on_board=False,
                                       done_date=now_rounded())
        action.contacts.add(contact)
        action.save()

    except Contact.DoesNotExist:
        pass

    return HttpResponseRedirect(link.url)
예제 #6
0
파일: utils.py 프로젝트: ljean/balafon
def create_profile_contact(user):
    profile = user.contactprofile
    if not profile:
        profile = ContactProfile(user=user)
    rename_entity = False

    if profile.contact:
        contact = profile.contact
    else:
        warn_duplicates = False
        contact = None
        try:
            contact = Contact.objects.get(email=user.email)
        except Contact.DoesNotExist:
            pass
        except Contact.MultipleObjectsReturned:
            warn_duplicates = True

        if not contact:
            if profile and profile.entity_type:
                entity_type = profile.entity_type
            else:
                if crm_settings.ALLOW_SINGLE_CONTACT:
                    entity_type = None
                else:
                    et_id = getattr(settings, 'BALAFON_INDIVIDUAL_ENTITY_ID',
                                    1)
                    entity_type, x = EntityType.objects.get_or_create(id=et_id)
                    rename_entity = True

            entity = Entity(
                name=profile.entity_name if profile else user.username,
                is_single_contact=(entity_type is None)
                and crm_settings.ALLOW_SINGLE_CONTACT,
                type=entity_type)

            entity.save()
            # This create a default contact
            contact = entity.default_contact

        if warn_duplicates:
            action_type = ActionType.objects.get_or_create(
                name=_("Balafon admin"))[0]
            action = Action.objects.create(
                subject=_(
                    "A user have registred with email {0} used by several other contacts"
                    .format(user.email)),
                type=action_type,
                planned_date=now_rounded(),
                detail=_(
                    'You should check that this contact is not duplicated'),
                display_on_board=True)
            action.contacts.add(contact)
            action.save()

    fields = (
        'gender',
        'firstname',
        'lastname',
        'phone',
        'mobile',
        'address',
        'address2',
        'address3',
        'zip_code',
        'city',
        'cedex',
        'country',
    )
    if profile:
        for field in fields:
            value = getattr(profile, field, None)
            if value:
                setattr(contact, field, value)

    contact.lastname = contact.lastname or user.last_name
    contact.firstname = contact.firstname or user.first_name
    contact.email = user.email
    contact.email_verified = True
    if not contact.lastname:
        contact.lastname = user.email.split("@")[0]

    contact.save()

    try:
        ids = [int(s) for s in profile.subscriptions_ids.split(",")]
    except ValueError:
        ids = []

    subscription_types = []
    for subscription_type_id in ids:
        try:
            subscription_types.append(
                SubscriptionType.objects.get(id=subscription_type_id))
        except SubscriptionType.DoesNotExist:
            pass
    save_subscriptions(contact, subscription_types)

    try:
        groups_ids = [int(s) for s in profile.groups_ids.split(",")]
    except ValueError:
        groups_ids = []

    for group_id in groups_ids:
        try:
            group = Group.objects.get(id=group_id)
            group.contacts.add(contact)
            group.save()
        except Group.DoesNotExist:
            pass

    action_type = ActionType.objects.get_or_create(
        name=_("Account creation"))[0]
    action = Action.objects.create(subject=_("Create an account on web site"),
                                   type=action_type,
                                   planned_date=now_rounded(),
                                   display_on_board=False,
                                   done=True)
    action.contacts.add(contact)
    action.save()

    for custom_field in profile.custom_fields.all():
        if custom_field.name:
            if custom_field.entity_field:
                if not contact.entity.is_single_contact:
                    contact.entity.set_custom_field(custom_field.name,
                                                    custom_field.value)
            else:
                contact.set_custom_field(custom_field.name, custom_field.value)

    if rename_entity:
        contact.entity.name = "{0.lastname} {0.firstname}".format(
            contact).strip().upper()
        contact.entity.save()

    profile.contact = contact
    profile.save()
    return profile
예제 #7
0
파일: views.py 프로젝트: Maxence42/balafon
def unregister_contact(request, emailing_id, contact_uuid):
    """contact unregistrer from emailing list"""

    contact = get_object_or_404(Contact, uuid=contact_uuid)
    try:
        emailing = models.Emailing.objects.get(id=emailing_id)
    except models.Emailing.DoesNotExist:
        emailing = None
    my_company = settings.BALAFON_MY_COMPANY
    
    if request.method == "POST":
        if 'unregister' in request.POST:
            form = forms.UnregisterForm(request.POST)
            if form.is_valid():
                if emailing and emailing.subscription_type:
                    subscription = Subscription.objects.get_or_create(
                        contact=contact, subscription_type=emailing.subscription_type
                    )[0]
                    subscription.accept_subscription = False
                    subscription.unsubscription_date = datetime.datetime.now()
                    subscription.save()

                    emailing_action = ActionType.objects.get_or_create(name=_(u'Unregister'))[0]
                    action = Action.objects.create(
                        subject=_(u'{0} has unregister').format(contact),
                        planned_date=now_rounded(), type=emailing_action, detail=form.cleaned_data['reason'],
                        done=True, display_on_board=False, done_date=now_rounded()
                    )
                    action.contacts.add(contact)
                    action.save()

                    emailing.unsub.add(contact)
                    emailing.save()

                return render_to_response(
                    'Emailing/public/unregister_done.html',
                    {
                        'contact': contact,
                        'emailing': emailing,
                        'form': form,
                        'my_company': my_company,
                        'unregister': True,
                    },
                    context_instance=RequestContext(request)
                )
            else:
                pass #not valid : display with errors
        
        else:
            return render_to_response(
                'Emailing/public/unregister_done.html',
                {
                    'contact': contact,
                    'emailing': emailing,
                    'my_company': my_company
                },
                context_instance=RequestContext(request)
            )
    else:
        form = forms.UnregisterForm()

    return render_to_response(
        'Emailing/public/unregister_confirm.html',
        {
            'contact': contact,
            'emailing': emailing,
            'form': form,
            'my_company': my_company
        },
        context_instance=RequestContext(request)
    )
예제 #8
0
파일: views.py 프로젝트: ljean/balafon
def unregister_contact(request, emailing_id, contact_uuid):
    """contact unregistrer from emailing list"""

    contact = get_object_or_404(Contact, uuid=contact_uuid)
    try:
        emailing = models.Emailing.objects.get(id=emailing_id)
        my_company = emailing.subscription_type.name
    except models.Emailing.DoesNotExist:
        emailing = None
        my_company = getattr(settings, 'BALAFON_MY_COMPANY', '')

    if request.method == "POST":
        if 'unregister' in request.POST:
            form = forms.UnregisterForm(request.POST)
            if form.is_valid():
                if emailing and emailing.subscription_type:
                    subscription = Subscription.objects.get_or_create(
                        contact=contact,
                        subscription_type=emailing.subscription_type)[0]
                    subscription.accept_subscription = False
                    subscription.unsubscription_date = datetime.datetime.now()
                    subscription.save()

                    emailing_action = ActionType.objects.get_or_create(
                        name=_('Unregister'))[0]
                    action = Action.objects.create(
                        subject=_('{0} has unregister').format(contact),
                        planned_date=now_rounded(),
                        type=emailing_action,
                        detail=form.cleaned_data['reason'],
                        done=True,
                        display_on_board=False,
                        done_date=now_rounded())
                    action.contacts.add(contact)
                    action.save()

                    emailing.unsub.add(contact)
                    emailing.save()

                return render(
                    request, 'Emailing/public/unregister_done.html', {
                        'contact': contact,
                        'emailing': emailing,
                        'form': form,
                        'my_company': my_company,
                        'unregister': True,
                    })
            else:
                pass  # not valid : display with errors

        else:
            return render(request, 'Emailing/public/unregister_done.html', {
                'contact': contact,
                'emailing': emailing,
                'my_company': my_company
            })
    else:
        form = forms.UnregisterForm()

    return render(
        request, 'Emailing/public/unregister_confirm.html', {
            'contact': contact,
            'emailing': emailing,
            'form': form,
            'my_company': my_company
        })