1 from django.db import models
2
3 from utils import cached_property, mixin_property, mixin_method, mixin
4
5
6 """
7 @summary:
8 The B{search} module will contain the general functionality for
9 whatever full-text searching solution we end up implementing. For
10 right now, it's just a shell, providing empty I{#search} methods
11 on searchable models.
12
13 @author: Daniel Ring
14 @organization: ThoughtAndMemory.org
15 @copyright: ThoughtAndMemoyr.org
16 @contact: lucy@thoughtandmemory.org, dfring@gmail.com
17
18 @version: 0.22a
19
20 """
21
22
23
25 """
26 @summary:
27 A B{Source} is a document or periodical containing B{Articles} by one or many B{Authors},
28 such as the New York Times.
29
30 @ivar name: The name of the document or periodical.
31 @type name: string
32 @ivar url: A link to the homepage of the periodical or its publisher.
33 @type url: string
34 @ivar description: An additional information about the source.
35 @type description: string
36 """
37
38 name = models.CharField(max_length=255)
39 description = models.TextField()
40 url = models.URLField(blank=True, verify_exists=False)
41
42 @classmethod
43 - def make(klass, name=None, description="", url=""):
50
51 @cached_property
53 """
54 @summary: Returns a QuerySet containing all L{Article}s from this B{Source}
55 @rtype: QuerySet
56 """
57 return Article.by_source(self)
58
59 @cached_property
61 """
62 @summary: Returns a QuerySet containing all L{Citation}s citing this B{Source}.
63 @rtype: QuerySet
64 """
65 return Citation.find(article_id__in="SELECT article.id FROM db_article AS article WHERE article.source_id = %s" % (self.id,))
66
67 @cached_property
69 """
70 @summary: Returns a QuerySet containing all L{Author}s paired with this
71 B{Source} through one or more L{Article}s.
72 """
73 return Author.find(id__in="SELECT DISTINCT article.author_id FROM db_article AS article WHERE article.source_id = %s" % (self.id,))
74
79
81 """
82 @summary:
83 An B{Author} is the human composer associated with (potentially many)
84 B{Articles}.
85
86 @ivar name: The name of the B{Author}
87 @type name: string
88 @ivar description: Any additional information about the source.
89 @type description: string
90 @ivar url: A url, if available.
91 @type url: string
92 """
93
94 name = models.CharField(max_length=255)
95 description = models.TextField()
96 url = models.URLField(blank=True, verify_exists=False)
97
98 @classmethod
99 - def make(klass, name, description="", url=""):
100 """
101 @see: L{Author} for parameter information.
102 @return: Instantiated, but not saved, a new B{Author} object.
103 @rtype: Author
104 """
105 return Author(name=name, description=description, url=url)
106
107 @cached_property
108 - def articles(self):
109 """
110 @summary: Hm
111 @return: Returns a QuerySet of L{Articles} by this B{Author}.
112 @rtype: QuerySet
113 """
114 return Article.by_author(self)
115
116 @cached_property
118 """
119 @return: Returns a QuerySet of L{Sources} paired with this B{Author} by an L{Article}.
120 @rtype: QuerySet
121 """
122 return Source.find(id__in="SELECT DISTINCT article.source_id FROM db_article AS article WHERE article.author_id = %s" % (self.id,))
123
124 @cached_property
126 """
127 @return: Returns a QuerySet containing all L{Citations} mentioning this source.
128 @rtype: QuerySet
129 """
130 return Citation.objects.filter(article__in="SELECT article.id FROM db_article AS article WHERE article.author_id = %s" % (self.id,))
131
136
137 -class Article(models.Model):
138 """
139 @summary:
140 An article is a single entry from a B{Source}.
141
142 @ivar title: The title of this B{Article}
143 @type title: string
144 @ivar author: A reference to an B{Author} object who wrote this
145 article.
146 @type author: string
147 @ivar source: A reference to the B{Source} object from which the
148 article was taken.
149 @type source: Source
150 @ivar url: A link at which the original article can be viewed.
151 @type url: string
152 """
153
154 title = models.CharField(max_length=255)
155 url = models.URLField(blank=True, verify_exists=False)
156
157 source = models.ForeignKey(Source)
158 author = models.ForeignKey(Author)
159
160 @classmethod
161 - def make(klass, title, source, author, url=""):
162 """
163 @summary: Instantiates, but does not save, a new B{Article} object.
164 @see: L{Article} for parameter information.
165 @return: The new Article
166 @rtype: Article
167 """
168 return Article(title=title, url=url, source=source, author=author)
169
170 @classmethod
171 - def by_source(klass, source):
172 """
173 @return: Returns the QuerySet of B{Articles} citing the specified L{Source}.
174 @rtype: QuerySet
175 """
176 return Article.objects.filter(source=source)
177
178 @classmethod
179 - def by_author(klass, author):
180 """
181 @return: Returns the QuerySet of B{Articles} citing the specified L{Author}.
182 @rtype: QuerySet
183 """
184 return Author.objects.filter(author=author)
185
186 - def citations(self):
187 """
188 @return: Returns the QuerySet of L{Citation}s citing this B{Article}.
189 @rtype: QuerySet
190 """
191 return self.citation_set.filter()
192
193 - def __unicode__(self):
197
198
200 """
201 @summary:
202 This is a single reference to a B{Source}, B{Article}, B{Author} set, reference from an
203 B{Argument}
204
205 @ivar article: A foreign key to B{Article}, e.g. the B{Article} referenced from this B{Citation}
206 @type article: Article
207 @ivar passage: Text describing where in the I{article} the citation is pointing.
208 @type passage: string
209 @ivar argument: The B{Argument} by which this citation is used.
210 @type argument: Argument
211 """
212
213 CITABLE_MODELS = []
214
215 @classmethod
219
220 article = models.ForeignKey(Article)
221 passage = models.CharField(max_length=255)
222
223
224
225 @classmethod
226 - def make(klass, article, argument, passage=""):
234
235 @classmethod
236 - def by_article(klass, article):
237 """
238 @summary: Returns the QuerySet of B{Citations} citing the specified L{Article}.
239 @rtype: QuerySet
240 """
241 return Citation.objects.filter(article=article)
242
243 @classmethod
245 """
246 @summary: Returns the QuerySet of B{Citation}s used by the specified L{Argument}.
247 @rtype: QuerySet
248 """
249 return Citation.objects.filter(argument=argument)
250
255
256
257
258
259
260
261
262
263
264
265