cc1  v2.1
CC1 source code docs
 All Classes Namespaces Files Functions Variables Pages
message.py
Go to the documentation of this file.
1 # -*- coding: utf-8 -*-
2 # @COPYRIGHT_begin
3 #
4 # Copyright [2010-2014] Institute of Nuclear Physics PAN, Krakow, Poland
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18 # @COPYRIGHT_end
19 
20 ##
21 # @package src.clm.models.message
22 # @author Tomek SoĊ›nicki <tom.sosnicki@gmail.com>
23 #
24 
25 
26 from django.db import models
27 from clm.models.user import User
28 import json
29 from clm.utils.exception import CLMException
30 from datetime import datetime
31 
32 
33 ##
34 #
35 # @model{MESSAGE}
36 #
37 # Message is an entity representing user-targeted communicates. Message has
38 # its level and content. Web interface renders Message's content based on
39 # it's code and params.
40 #
41 class Message(models.Model):
42  ## User whom this Message is targetted to @field
43  user = models.ForeignKey(User)
44  ## Code of this Message's template @field
45  code = models.CharField(max_length=128)
46  ## Params to insert into Message template @field
47  params = models.CharField(max_length=2048)
48  ## Message's creation date @field
49  creation_date = models.DateTimeField(default=datetime.now)
50  ## Importance level of the message (@seealso{src.common.states.message_levels}) @field
51  level = models.IntegerField()
52 
53  class Meta:
54  app_label = 'clm'
55 
56  ##
57  #
58  # @returns{string} Message id
59  #
60  def __unicode__(self):
61  return str(self.id)
62 
63  @property
64  ##
65  #
66  # @returns{dict} Message's data
67  # \n fields:
68  # @dictkey{message_id,int} id of this Message
69  # @dictkey{params} params of this Message
70  # @dictkey{creation_date,datetime.datetime} creation date of this Message
71  # @dictkey{level,int} this Message's importance level importance,
72  # @seealso{src.common.states.message_levels}
73  # @dictkey{code,string} short code of the Message (locales translate
74  # short code into human-readable message, which is filled with data from \c params)
75  #
76  def dict(self):
77  d = {}
78  d['message_id'] = self.id
79  d['params'] = json.loads(self.params)
80  d['creation_date'] = self.creation_date
81  d['level'] = self.level
82  d['code'] = self.code
83  return d
84 
85  @staticmethod
86  ##
87  #
88  # @parameter{msg_id,int} id of the requested Message
89  #
90  # @returns{Message} instance of the requested Message
91  #
92  # @raises{message_get,CLMException} no such Message
93  #
94  def get(msg_id):
95  try:
96  m = Message.objects.get(pk=msg_id)
97  except:
98  raise CLMException('message_get')
99 
100  return m
101 
102  @staticmethod
103  ##
104  #
105  # @parameter{data,dict}
106  # \n fields:
107  # @dictkey{user_id,int} User id of the new messages creator
108  # @dictkey{level,int} importance level of the new message, @seealso{src.common.states.message_levels}
109  # @dictkey{code,string} short code of the new message (locales translate
110  # short code into human-readable message, which is filled with data from \c params)
111  # @dictkey{params} params to fill Message with (optional)
112  #
113  # Method creates and returns new instance of Message.
114  #
115  # @returns{Message} instance of the newly created Message.
116  #
117  def create(data):
118  m = Message()
119  m.user_id = data['user_id']
120  m.level = data['level']
121  m.code = data['code']
122  m.params = json.dumps(data.get('params', ''))
123  return m
124