예제 #1
0
    def cancel_talk(self, user, talk_id, post_data):
        """Cancel the negotiation talk
        
        Delete the negociation talk and associated messages, send a mail to 
        the other user.
        
        Canceling a negociation talk must have a reason. So, we use the contact 
        form.
        
        - If the talk doesnt exists, raise a TalkDoesNotExist exception
        
        """
        try:
            talk = Talk.objects.get(pk=talk_id)
        except Talk.DoesNotExist:
            raise TalkDoesNotExist(talk_id)

        if (talk.from_user.id != user.id and talk.trip.user.id != user.id):
            raise InvalidUser(user)

        form = ContactUserForm(post_data)

        if not form.is_valid():
            raise InvalidContactUserForm(form)
        from_user = user
        to_user = (talk.trip.user
                   if from_user.id == talk.from_user.id else talk.from_user)
        subject = talk.trip.get_public_name()
        message_header = (u"%s a annulé la négociation " % from_user.username)

        if from_user.id == talk.from_user.id:
            message_header += u"à propos de votre annonce %s (%s)." % (
                talk.trip.name,
                subject,
            )
        else:
            message_header += (u"à propos de l'annonce %s." % subject)
        send_mail(
            (u"Annonce %s - Annulation de la négociation" % subject),
            u"""Ceci est un message automatique, veuillez ne pas y répondre.

Bonjour %s,

%s
Il semblerait que vous n'ayez pas trouvé de compromis satisfaisant.

Voici la raison de l'annulation donnée par %s:
--------------------------------
%s
--------------------------------

Nous espérons que vous aurez plus de chance lors de votre prochaine négociation.

Cordialement,
L'équipe %s""" % (to_user.username, message_header, from_user.username,
                  form.cleaned_data['message'], settings.PROJECT_NAME),
            settings.FROM_EMAIL, [to_user.email])
        talk.delete()
예제 #2
0
    def delete_trip(self, user, trip_id, post_data):
        """Delete a trip
        
        Delete the announce and associated negociations and alert the contact 
        that the negociation has been deleted because of the related announce 
        deletion.
        
        - If the trip doesnt exists, raise a TripDoesNotExist exception
        """
        try:
            trip = Trip.objects.get(pk=trip_id, user=user)
        except Trip.DoesNotExist:
            raise InvalidUser(user)
            
        form = ContactUserForm(post_data)
        if not form.is_valid():
            raise InvalidContactUserForm(form)
            
        for talk in trip.talk_set.all():
            try:
                from_user = user
                to_user = talk.from_user
                subject = talk.trip.get_public_name()
                message_header = u"%s a supprimé son annonce %s" % (
                    from_user.username,
                    subject,
                )
                send_mail(
                    (u"Annonce %s - Suppression de l'annonce et de la "
                        u"négociation" % subject),
                    u"""Ceci est un message automatique, veuillez ne pas y répondre.

Bonjour %s,

%s,
ce qui entraîne la suppression de votre négociation.

Voici la raison de la suppression donnée par %s:
--------------------------------
%s
--------------------------------

Cordialement,
L'équipe %s""" % (
                        to_user.username,
                        message_header,
                        from_user.username,
                        form.cleaned_data['message'],
                        settings.PROJECT_NAME
                    ),
                    settings.FROM_EMAIL, [to_user.email]
                )
            except:
                pass
        trip.delete()
예제 #3
0
    def delete_trip(self, user, trip_id, post_data):
        """Delete a trip
        
        Delete the announce and associated negociations and alert the contact 
        that the negociation has been deleted because of the related announce 
        deletion.
        
        - If the trip doesnt exists, raise a TripDoesNotExist exception
        """
        try:
            trip = Trip.objects.get(pk=trip_id, user=user)
        except Trip.DoesNotExist:
            raise InvalidUser(user)

        form = ContactUserForm(post_data)
        if not form.is_valid():
            raise InvalidContactUserForm(form)

        for talk in trip.talk_set.all():
            try:
                from_user = user
                to_user = talk.from_user
                subject = talk.trip.get_public_name()
                message_header = u"%s a supprimé son annonce %s" % (
                    from_user.username,
                    subject,
                )
                send_mail((
                    u"Annonce %s - Suppression de l'annonce et de la "
                    u"négociation" % subject
                ), u"""Ceci est un message automatique, veuillez ne pas y répondre.

Bonjour %s,

%s,
ce qui entraîne la suppression de votre négociation.

Voici la raison de la suppression donnée par %s:
--------------------------------
%s
--------------------------------

Cordialement,
L'équipe %s""" % (to_user.username, message_header, from_user.username,
                  form.cleaned_data['message'], settings.PROJECT_NAME),
                          settings.FROM_EMAIL, [to_user.email])
            except:
                pass
        trip.delete()
예제 #4
0
    def contact_user(self, user, trip_id, post_data):
        """Create a new negociation about an announce
        
        Create the negotiation, the message, and send a mail to the user trip.
        
        If the trip doesnt exists, raise a TripDoesNotExist exception.
        If a negociation already exists for this announce and this user, raise 
        an TalkAlreadyExist exception.
        
        If one of the email field is empty, raise a InvalidMail exception.

        """
        try:
            trip = Trip.objects.get(pk=trip_id)
        except Trip.DoesNotExist:
            raise TripDoesNotExist(trip_id)
        try:
            talk = Talk.objects.get(trip=trip, from_user=user)
            raise TalkAlreadyExists(trip_id)
        except Talk.DoesNotExist:
            pass

        if user.email == "" or trip.user.email == "":
            raise InvalidMail()

        from_user = user
        to_user = trip.user
        subject = _("Trip %(trip_public_name)s") % {
            'trip_public_name': trip.get_public_name()
        }
       
        form = ContactUserForm(post_data)
        if not form.is_valid():
            raise InvalidContactUserForm(form)
        try:
            talk = Talk(
                trip=trip,
                from_user=from_user
            )
            talk.save()
            self._send_message(
                talk,
                from_user,
                to_user,
                form.cleaned_data['message']
            )
        except Exception, err:
            transaction.rollback()
            raise err
예제 #5
0
    def contact_user(self, user, trip_id, post_data):
        """Create a new negociation about an announce
        
        Create the negotiation, the message, and send a mail to the user trip.
        
        If the trip doesnt exists, raise a TripDoesNotExist exception.
        If a negociation already exists for this announce and this user, raise 
        an TalkAlreadyExist exception.
        
        If one of the email field is empty, raise a InvalidMail exception.

        """
        try:
            trip = Trip.objects.get(pk=trip_id)
        except Trip.DoesNotExist:
            raise TripDoesNotExist(trip_id)
        try:
            talk = Talk.objects.get(trip=trip, from_user=user)
            raise TalkAlreadyExists(trip_id)
        except Talk.DoesNotExist:
            pass

        if user.email == "" or trip.user.email == "":
            raise InvalidMail()

        from_user = user
        to_user = trip.user
        subject = _("Trip %(trip_public_name)s") % {
            'trip_public_name': trip.get_public_name()
        }

        form = ContactUserForm(post_data)
        if not form.is_valid():
            raise InvalidContactUserForm(form)
        try:
            talk = Talk(trip=trip, from_user=from_user)
            talk.save()
            self._send_message(talk, from_user, to_user,
                               form.cleaned_data['message'])
        except Exception, err:
            transaction.rollback()
            raise err
예제 #6
0
    def add_message(self, user, talk_id, post_data):
        """Add a message to an already existing negociation talk, and send a 
        mail to the user.
        
        - If the talk doesnt exists, raise a TalkDoesNotExist exception
        - If the provided post_data is not valid, raise a InvalidContactUserForm
        - If the user is not allowed to retreive this data, raise a InvalidUser
          exception.

        """
        try:
            talk = Talk.objects.get(id=talk_id)
        except Talk.DoesNotExist:
            raise TalkDoesNotExist(talk_id)
            
        if (talk.from_user.id != user.id and talk.trip.user.id != user.id):
            raise InvalidUser(user)

        from_user = user
        to_user = (talk.trip.user if from_user.id == talk.from_user.id
                else talk.from_user)
                
        subject = _("Trip %(trip_public_name)s") % {
            'trip_public_name': talk.trip.get_public_name()
        }
        
        form = ContactUserForm(post_data)
        if form.is_valid():
            try:
                self._send_message(
                    talk,
                    from_user,
                    to_user,
                    form.cleaned_data['message']
                )
            except Exception, err:
                transaction.rollback()
                raise err
            else:
                transaction.commit()
                return talk
예제 #7
0
    def add_message(self, user, talk_id, post_data):
        """Add a message to an already existing negociation talk, and send a 
        mail to the user.
        
        - If the talk doesnt exists, raise a TalkDoesNotExist exception
        - If the provided post_data is not valid, raise a InvalidContactUserForm
        - If the user is not allowed to retreive this data, raise a InvalidUser
          exception.

        """
        try:
            talk = Talk.objects.get(id=talk_id)
        except Talk.DoesNotExist:
            raise TalkDoesNotExist(talk_id)

        if (talk.from_user.id != user.id and talk.trip.user.id != user.id):
            raise InvalidUser(user)

        from_user = user
        to_user = (talk.trip.user
                   if from_user.id == talk.from_user.id else talk.from_user)

        subject = _("Trip %(trip_public_name)s") % {
            'trip_public_name': talk.trip.get_public_name()
        }

        form = ContactUserForm(post_data)
        if form.is_valid():
            try:
                self._send_message(talk, from_user, to_user,
                                   form.cleaned_data['message'])
            except Exception, err:
                transaction.rollback()
                raise err
            else:
                transaction.commit()
                return talk
예제 #8
0
    def cancel_talk(self, user, talk_id, post_data):
        """Cancel the negotiation talk
        
        Delete the negociation talk and associated messages, send a mail to 
        the other user.
        
        Canceling a negociation talk must have a reason. So, we use the contact 
        form.
        
        - If the talk doesnt exists, raise a TalkDoesNotExist exception
        
        """
        try:
            talk = Talk.objects.get(pk=talk_id)
        except Talk.DoesNotExist:
            raise TalkDoesNotExist(talk_id)
            
        if (talk.from_user.id != user.id and talk.trip.user.id != user.id):
            raise InvalidUser(user)

        form = ContactUserForm(post_data)
        
        if not form.is_valid():
            raise InvalidContactUserForm(form)
        from_user = user
        to_user = (talk.trip.user if from_user.id == talk.from_user.id
                else talk.from_user)
        subject = talk.trip.get_public_name()
        message_header = (u"%s a annulé la négociation " %
            from_user.username)
            
        if from_user.id == talk.from_user.id:
            message_header += u"à propos de votre annonce %s (%s)." % (
                talk.trip.name,
                subject,
            )
        else:
            message_header += (u"à propos de l'annonce %s." %
                subject)
        send_mail(
            (u"Annonce %s - Annulation de la négociation" %
                subject),
            u"""Ceci est un message automatique, veuillez ne pas y répondre.

Bonjour %s,

%s
Il semblerait que vous n'ayez pas trouvé de compromis satisfaisant.

Voici la raison de l'annulation donnée par %s:
--------------------------------
%s
--------------------------------

Nous espérons que vous aurez plus de chance lors de votre prochaine négociation.

Cordialement,
L'équipe %s""" % (
                to_user.username,
                message_header,
                from_user.username,
                form.cleaned_data['message'],
                settings.PROJECT_NAME
            ),
            settings.FROM_EMAIL, [to_user.email]
        )
        talk.delete()