Personal Messages for muzx

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# models.py
class Thread(models.Model):
id = models.IntegerField(max_length=16, primary_key=True)
read = models.ForeignKey(User, blank=True, null=True)
date = models.DateTimeField()
class Meta:
ordering = ['-date']
class Message(models.Model):
sender = models.ForeignKey(User, related_name='sender')
recipient = models.ForeignKey(User, related_name='recipient')
content = models.TextField()
date = models.DateTimeField()
read = models.BooleanField()
thread = models.ForeignKey(Thread)
class Admin:
list_display = ['id', 'sender', 'recipient', 'read']
class Meta:
ordering = ['id']
verbose_name = 'Message'
verbose_name_plural = 'Messages'
def __unicode__(self):
return str(self.date)
# views.py
@login_required
@activation_required
@render_to('my_profile.html')
def my_profile(request):
try:
picture = request.user.get_profile().pictures.all()[0]
except:
picture = False
friends = request.user.get_profile().friends.all()
communities = Community.objects.filter(user=request.user)
correspondence_users = []
for message in Message.objects.filter(Q(sender=request.user) | Q(recipient=request.user)):
correspondence_users.append(message.sender)
correspondence_users.append(message.recipient)
correspondence_users.remove(request.user)
thread_numbers = []
for user in correspondence_users:
thread_numbers.append("%s%s" % (min(request.user.id, user.id), max(request.user.id, user.id)))
threads = Thread.objects.filter(id__in=set(thread_numbers))
return {'picture': picture, 'friends': friends, 'communities': communities, 'threads': threads}
@login_required
@activation_required
def comment_user(request, id):
sender = request.user
recipient = get_object_or_404(User, id=id)
thread_id = "%s%s" % (min(sender.id, recipient.id), max(sender.id, recipient.id))
content = request.POST['message']
thread = get_object_or_None(Thread, id=thread_id)
if thread:
thread.read = ''
thread.date = datetime.datetime.now()
thread.save()
else:
thread = Thread(id=thread_id, read=sender, date=datetime.datetime.now())
thread.save()
Message.objects.create(sender=sender, recipient=recipient, content=content, date=datetime.datetime.now(), read=False, thread=thread)
return HttpResponseRedirect("/accounts/profile/%s/" % id)
# my_profile.html
<div>
<br /><br />
<b>Messages</b>:<br />
{% for thread in threads %}
{% ifequal thread.message_set.all.0.sender request.user %}
<b><a href="{% url other-profile thread.message_set.all.0.recipient.id %}">{{ thread.message_set.all.0.recipient.first_name }} {{ thread.message_set.all.0.recipient.last_name }}</a></b><br />
{% else %}
<b><a href="{% url other-profile thread.message_set.all.0.sender.id %}">{{ thread.message_set.all.0.sender.first_name }} {{ thread.message_set.all.0.sender.last_name }}</a></b><br />
{% endifequal %}
{% for message in thread.message_set.all %}
<b>{{ message.sender.first_name }} {{ message.sender.last_name }}</b>:<br />
{{ message.content }}<br />
{% endfor %}
<br /><br />
{% endfor %}
</div>