<s> from django . db import models <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from django . utils . text import truncate_words <EOL> from userena . contrib . umessages . managers import ( MessageManager , MessageContactManager , <EOL> MessageRecipientManager ) <EOL> from userena . utils import user_model_label <EOL> class MessageContact ( models . Model ) : <EOL> """<STR_LIT>""" <EOL> from_user = models . ForeignKey ( user_model_label , verbose_name = _ ( "<STR_LIT>" ) , <EOL> related_name = ( '<STR_LIT>' ) ) <EOL> to_user = models . ForeignKey ( user_model_label , verbose_name = _ ( "<STR_LIT>" ) , <EOL> related_name = ( '<STR_LIT>' ) ) <EOL> latest_message = models . ForeignKey ( '<STR_LIT>' , <EOL> verbose_name = _ ( "<STR_LIT>" ) ) <EOL> objects = MessageContactManager ( ) <EOL> class Meta : <EOL> unique_together = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> ordering = [ '<STR_LIT>' ] <EOL> verbose_name = _ ( "<STR_LIT>" ) <EOL> verbose_name_plural = _ ( "<STR_LIT>" ) <EOL> def __unicode__ ( self ) : <EOL> return ( _ ( "<STR_LIT>" ) <EOL> % { '<STR_LIT>' : self . from_user . username , <EOL> '<STR_LIT>' : self . to_user . username } ) <EOL> def opposite_user ( self , user ) : <EOL> """<STR_LIT>""" <EOL> if self . from_user == user : <EOL> return self . to_user <EOL> else : return self . from_user <EOL> class MessageRecipient ( models . Model ) : <EOL> """<STR_LIT>""" <EOL> user = models . ForeignKey ( user_model_label , <EOL> verbose_name = _ ( "<STR_LIT>" ) ) <EOL> message = models . ForeignKey ( '<STR_LIT>' , <EOL> verbose_name = _ ( "<STR_LIT:message>" ) ) <EOL> read_at = models . DateTimeField ( _ ( "<STR_LIT>" ) , <EOL> null = True , <EOL> blank = True ) <EOL> deleted_at = models . DateTimeField ( _ ( "<STR_LIT>" ) , <EOL> null = True , <EOL> blank = True ) <EOL> objects = MessageRecipientManager ( ) <EOL> class Meta : <EOL> verbose_name = _ ( "<STR_LIT>" ) <EOL> verbose_name_plural = _ ( "<STR_LIT>" ) <EOL> def __unicode__ ( self ) : <EOL> return ( _ ( "<STR_LIT>" ) <EOL> % { '<STR_LIT:message>' : self . message } ) <EOL> def is_read ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . read_at is None <EOL> class Message ( models . Model ) : <EOL> """<STR_LIT>""" <EOL> body = models . TextField ( _ ( "<STR_LIT:body>" ) ) <EOL> sender = models . ForeignKey ( user_model_label , <EOL> related_name = '<STR_LIT>' , <EOL> verbose_name = _ ( "<STR_LIT>" ) ) <EOL> recipients = models . ManyToManyField ( user_model_label , <EOL> through = '<STR_LIT>' , <EOL> related_name = "<STR_LIT>" , <EOL> verbose_name = _ ( "<STR_LIT>" ) ) <EOL> sent_at = models . DateTimeField ( _ ( "<STR_LIT>" ) , <EOL> auto_now_add = True ) <EOL> sender_deleted_at = models . DateTimeField ( _ ( "<STR_LIT>" ) , <EOL> null = True , <EOL> blank = True ) <EOL> objects = MessageManager ( ) <EOL> class Meta : <EOL> ordering = [ '<STR_LIT>' ] <EOL> verbose_name = _ ( "<STR_LIT:message>" ) <EOL> verbose_name_plural = _ ( "<STR_LIT>" ) <EOL> def __unicode__ ( self ) : <EOL> """<STR_LIT>""" <EOL> truncated_body = truncate_words ( self . body , <NUM_LIT:10> ) <EOL> return "<STR_LIT>" % { '<STR_LIT>' : truncated_body } <EOL> def save_recipients ( self , to_user_list ) : <EOL> """<STR_LIT>""" <EOL> created = False <EOL> for user in to_user_list : <EOL> MessageRecipient . objects . create ( user = user , <EOL> message = self ) <EOL> created = True <EOL> return created <EOL> def update_contacts ( self , to_user_list ) : <EOL> """<STR_LIT>""" <EOL> updated = False <EOL> for user in to_user_list : <EOL> MessageContact . objects . update_contact ( self . sender , <EOL> user , <EOL> self ) <EOL> updated = True <EOL> return updated </s>
