Files
@ fbae06748b1e
Branch filter:
Location: django-shaker/djangoshaker/models.py
fbae06748b1e
8.7 KiB
text/x-python
Added the database file to list of files to ignore. Added symlink in the project directory to the djangoshaker application.
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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 | from django.db import models
from django.db.models import F
import datetime
class Measure(models.Model):
"""
Models a measure. A measure is anything which can be used to represent some
amount of ingredient, for example - pinch, litre, pound, ounce.
Some measures are also convertible to others through the MeasureConversion
module.
Fields:
name - Name of measure. E.g. 'litre'.
marking - A short marking used for the measure. E.g. 'l'.
description - Measure description.
"""
name = models.CharField(max_length = 64, unique = True)
marking = models.CharField(max_length = 24, unique = True)
description = models.TextField(blank = True)
def __unicode__(self):
"""
Returns unicode representation of a measure.
"""
return "%s (%s)" % (self.name, self.marking)
class Ingredient(models.Model):
"""
Models an ingredient of a recipe. Ingredient is not bound to a particular
brand.
Fields:
name - Ingredient name. E.g. 'white rum', 'orange juice'.
description - Ingredient description.
"""
name = models.CharField(max_length = 64, unique = True)
description = models.TextField(blank = True)
def __unicode__(self):
"""
Returns a unicode representation of an ingredient.
"""
return self.name
class Brand(models.Model):
"""
Models a brand of some ingredient. The brand can be either specific and
actually corresponding to some particular company, or generic, like tap
water or milk.
Fields:
name - Name of a brand. E.g. 'Milton Milk'.
description - Brand description.
ingredient - Foreign key to an ingredient that this brand provides.
created - Date when the brand was created in the database.
modified - Last date of change of the brand.
"""
name = models.CharField(max_length = 100, unique = True)
description = models.TextField(blank = True)
ingredient = models.ForeignKey(Ingredient)
created = models.DateTimeField(editable = False)
modified = models.DateTimeField(editable = False)
def save(self, *args, **kwargs):
"""
Overrides the default save operation in order to properly take care of
the created and modified fields (they should be set to same value on
initial creation of an object).
"""
cur_date = datetime.datetime.today()
if not self.id:
self.created = cur_date
self.modified = cur_date
super(Brand, self).save(*args, **kwargs)
def __unicode__(self):
"""
Returns a unicode representation of a brand.
"""
return "%s (%s)" % (self.name, self.ingredient.name)
@staticmethod
def latest_additions(count=5):
"""
Returns a queryset of last added brands.
Arguments:
count - Number of last added entries to return. Default is 5.
"""
return Brand.objects.order_by('-created')[:count]
@staticmethod
def latest_changes(count=5):
"""
Returns a queryset of last modified brands.
Arguments:
count - Number of last modified entries to return. Default is 5.
"""
return Brand.objects.filter(modified__gt=F('created')).order_by('-modified')[:count]
class MeasureConversion(models.Model):
"""
Models conversion ratios between different measures. Allows the user to
select related measures when presenting recipes.
Fields:
from_measure - Foreign key to a measure from which the conversion is made.
to_measure - Foreign key to a measure to which the conversion is made.
ratio - A number by which the from_measure is multiplied with in order to
get the amount in to_measure.
@TODO: Implement some checks that conversions aren't added in both (a, b)
and (b, a).
"""
from_measure = models.ForeignKey(Measure, related_name = 'conversion_from')
to_measure = models.ForeignKey(Measure, related_name = 'conversion_to')
ratio = models.FloatField()
def __unicode__(self):
"""
Returns a unicode representation of measure conversion.
"""
return "1 %s amounts to %d %s" % (self.from_measure.marking, self.ratio, self.to_measure.marking)
class Recipe(models.Model):
"""
Models basic recipe information. The recipe ingredients are kept in a
separate model.
Fields:
name - Name of a recipe.
description - Description of a recipe. This is not instructions. Those are
kept in a separate field.
instructions - Instructions for recipe preparation.
created - Date when the recipe was created in the database.
modified - Last date of change of the recipe.
published - Keeps track whether the recipe has been published or
not. Unpublished recipes are not shown in any list, and their modification
time is kept same as the creatio one.
ingredients = models.ManyToMany(Ingredient, through = "RecipeIngredients")
"""
name = models.CharField(max_length = 128, unique = True)
description = models.TextField(blank = True)
instructions = models.TextField()
created = models.DateTimeField(editable = False)
modified = models.DateTimeField(editable = False)
published = models.BooleanField(default = False, editable = False)
def __init__(self, *args, **kwargs):
"""
Overriden constructor that allows the instance to remember its original
value for the published field.
"""
super(Recipe, self).__init__(*args, **kwargs)
self.__original_published = self.published
def __unicode__(self):
"""
Returns a unicode representation of a recipe.
@TODO: See what the preferred output might be for this one. Currently
listing all ingredients.
"""
result = self.name + "("
for ing in self.recipeingredient_set.all():
result += str(ing) + ";"
result += ")"
return result
def publish(self):
"""
Publishes the recipe.
"""
self.published = True
self.save()
def save(self, *args, **kwargs):
"""
Overrides the default save operation in order to properly take care of
the created and modified fields (they should be set to same value on
initial creation of an object).
It also takes care of updating the modified data _only_ while the recipe
is published.
"""
cur_date = datetime.datetime.today()
if not self.id:
self.created = cur_date
self.modified = cur_date
elif self.published and self.__original_published:
self.modified = cur_date
elif not self.published and self.__original_published:
raise Exception("Unpublishing is not permitted.")
super(Recipe, self).save(*args, **kwargs)
def is_published(self):
"""
Returns whether the recipe was published or not.
"""
return self.published
published.short_description = "Published?"
@staticmethod
def latest_additions(count=5):
"""
Returns a queryset of last added recipes.
Arguments:
count - Number of last added recipes to return. Default is 5.
"""
return Recipe.objects.filter(published = True).order_by('-created')[:count]
@staticmethod
def latest_changes(count=5):
"""
Returns a queryset of last modified recipes.
Arguments:
count - Number of last modified entries to return. Default is 5.
"""
return Recipe.objects.filter(modified__gt=F('created'), published = True).order_by('-modified')[:count]
class RecipeIngredient(models.Model):
"""
Models the recipe ingredients. Connects the actualy recipe to a number of
different ingredients.
Fields:
recipe - Foreign key to which an ingredient is assigned.
ingredient - Foreign key to ingredient being used in a recipe.
amount - Quantity of ingredient required for the recipe.
"""
recipe = models.ForeignKey(Recipe)
ingredient = models.ForeignKey(Ingredient)
amount = models.FloatField()
measure = models.ForeignKey(Measure)
def __unicode__(self):
"""
Returns a unicode representation of an ingredient for the recipe
(stating which ingredient, how much of it etc).
"""
return "%d %s of %s" % (self.amount, self.measure.marking, self.ingredient)
def save(self, *args, **kwargs):
"""
Overrides the default save method to update the modification time of a
parent recipe (if applicable).
"""
if self.recipe.published:
self.recipe.save()
super(RecipeIngredient, self).save(*args, **kwargs)
|