Source

beerfingers / bf / hops / models.py

Full commit
#HOPS model
import datetime
from django.db import models
from django.contrib.auth.models import User
from django.template.defaultfilters import slugify
from django.contrib.localflavor.us.models import USStateField
from countries import COUNTRIES
from autoslug import AutoSlugField


class Breweries(models.Model):
	breweryname = models.CharField(max_length=200,unique=True, verbose_name="Brewery")
	address1 = models.CharField(max_length=150, blank=True, verbose_name="Address 1")
	address2 = models.CharField(max_length=150, blank=True, verbose_name="Address 2")
	city = models.CharField(max_length=135)
	state = USStateField(blank=True, default='')
	zipcode = models.CharField(max_length=135, blank=True, verbose_name="Zip Code")
	country = models.CharField(max_length=3, choices=COUNTRIES)
	description = models.TextField(blank=True)
	createdate = models.DateTimeField(auto_now_add=True)
	user = models.ForeignKey(User)
	website = models.URLField(verify_exists=True,blank=True, null=True)
	brewery_slug = AutoSlugField(populate_from='breweryname', always_update=True, verbose_name='Brewery URL', max_length=45,unique=True)
	logo = models.ImageField(blank=True, upload_to="upload")
	#authorized_groups = models.ManyToManyField('BreweryGroup', null=True, blank=True, related_name='brewery_groups')
	
	def __unicode__(self):
		return self.breweryname
	
	#def save(self):
	#	self.brewery_slug = '%s' % ( slugify(self.breweryname))
	#	super(Breweries, self).save()
	
	class Meta:
		ordering = ('breweryname',)
		verbose_name = "Brewery"
		verbose_name_plural = "Breweries"
		permissions  = (
			("can_brewery","Can add or change breweries."),
		)
	
		
	@models.permalink
	def get_absolute_url(self):
		return ('hops.views.brewery_detail', None, { 'brewery_slug': self.brewery_slug })
		
class Categories(models.Model):
	category = models.CharField(max_length=135,unique=True)
	createdate = models.DateTimeField(auto_now_add=True)
	def __unicode__(self):
		return self.category
	class Meta:
		ordering = ('category',)
		verbose_name_plural = "Categories"	
		
class BeerStyle(models.Model):
	stylename = models.CharField(max_length=135,unique=True)
	stylecat = models.ForeignKey(Categories)
	createdate = models.DateTimeField(auto_now_add=True)
	def __unicode__(self):
		return self.stylename
	class Meta:
		ordering = ('stylename',)
		
class Beers(models.Model):
	brewery = models.ForeignKey(Breweries)
	beername = models.CharField(max_length=135,unique=True, verbose_name="Beer")
	beerstyle = models.ForeignKey(BeerStyle,verbose_name='Style')
	abv = models.DecimalField(decimal_places=2, null=True, max_digits=6, blank=True, verbose_name="ABV")    
	ibu = models.DecimalField(decimal_places=2, null=True, max_digits=6, blank=True, verbose_name="IBU")
	plato = models.DecimalField(decimal_places=2, null=True, max_digits=6, blank=True)
	description = models.TextField(blank=True)    
	createdate = models.DateTimeField(auto_now_add=True)
	user = models.ForeignKey(User)
	beer_slug =  AutoSlugField(populate_from='beername', always_update=True, verbose_name='Beer URL', max_length=45,unique=True)
	label = models.ImageField(blank=True,upload_to="upload")
	#authorized_groups = models.ManyToManyField('BeerGroup', null=True, blank=True, related_name='beer_groups')
	
	@models.permalink
	def get_absolute_url(self):
		return ('hops.views.beer_detail', None, { 'brewery_slug': self.brewery.brewery_slug ,'beer_slug': self.beer_slug })
	
	def __unicode__(self):
		return self.beername

	class Meta:
		ordering = ('beername',)
		verbose_name = "Beer"
		verbose_name_plural = "Beers"
		permissions = (
			("can_beers","Can add or change beers."),
		)