Example #1
0
 def get_context_data(self, **kwargs):
     context = super(PublicProfile, self).get_context_data(**kwargs)
     
     if self.request.user.is_authenticated():
         context["form"] = ContactUserForm(initial={"name":self.request.user.username, "email":self.request.user.email})
     else:
         context["form"] = ContactUserForm()
         
     if not self.request.user.is_authenticated() or self.request.user != self.object: #.user:
         msg_sent_stat = UserStatModel(stat_type = "view",
                                       related_user = self.object)
         msg_sent_stat.save()
         
     context["active_social_profiles"]=[x for x in UserSocialAuth.objects.filter(user=self.object.user).all()]
         
     return context
Example #2
0
    def post(self, request, *args, **kwargs):
        response = {"success":False, "message": _("Error sending message")}
        
        send_to = kwargs.get("slug", None)
        send_from_name = request.POST.get("from",None)
        send_from_email = request.POST.get("email", None)
        send_message = request.POST.get("message", None)
        
        if not send_from_name:
            response["message"] = _("You must enter your name")
            return response
        if not send_from_email:
            response["message"] = _("You must enter your email")
            return response
        if not send_message:
            response["message"] = _("You must enter a message")
            return response
        
        try:
            send_to_user = UserProfile.objects.get(user_slug__iexact=send_to).user
        except UserProfile.DoesNotExist:
            response["message"] = "%s %s" % (_("Couldn't find user"), send_to)
            return response
        
        try:
            plaintext = get_template('email/new_message.txt')
            htmly     = get_template('email/new_message.html')
            
        # send alerts to followes
            d = Context({'from_name': send_from_name,
                         'from_email': send_from_email,
                         'to_user': send_to_user, 
                         'message': send_message,
                         "sender_profile_link":None
                        })
            
            if request.user.is_authenticated():
                d["sender_profile_link"]="http://pairup.org.il%s" % reverse("user_profile",kwargs={"slug":request.user.get_profile().user_slug})
        
            text_content = plaintext.render(d)
            html_content = htmly.render(d)
            
            subject = _('Message from Pairup Israel') 

            msg = EmailMultiAlternatives(subject, text_content, send_from_email, [send_to_user.email])
            msg.attach_alternative(html_content, "text/html")
            msg.send(fail_silently=False)
            
            new_stat, created = StatModel.objects.get_or_create(stat_type='mail')
            StatModel.objects.filter(pk=new_stat.id).update(stat_counter=F('stat_counter')+1)
            msg_sent_stat = UserStatModel(stat_type = "msg",
                                          related_user = send_to_user.get_profile())
            msg_sent_stat.save()
                
        except SMTPException:
            response["message"] = _("Failed sending email message")
            return response
        
        response["message"] = _("Message sent successfully")
        response["success"] = True
        return response