from django import forms
from django.contrib.auth.models import User
from rango.models import Page, Category, UserProfile

class CategoryForm(forms.ModelForm):
	name = forms.CharField(max_length=128, help_text="Enter the category name")
	views = forms.IntegerField(widget=forms.HiddenInput(), initial=0)
	likes = forms.IntegerField(widget=forms.HiddenInput(), initial=0)
	slug = forms.CharField(widget=forms.HiddenInput(), required=False)

	#An inline class to provide additional information on the form
	class Meta:
		#Provide association between the ModelForm and a model
		model = Category
		fields = ('name',)

class PageForm(forms.ModelForm):
	title = forms.CharField(max_length=128, help_text="Please enter page title")
	url = forms.URLField(max_length=200, help_text="Please enter the URL")
	views = forms.IntegerField(widget=forms.HiddenInput(), initial=0)

	def clean(self):
		cleaned_data = self.cleaned_data
		url = cleaned_data.get('url')

		#If url is empty and dosen't start with http:// prepend it
		if url and not url.startswith('http://'):
			url = 'http://' + url
			cleaned_date['url'] = url

		return cleaned_data


	#Inline class for proving more details on form
	class Meta:
		#Provide an association between the ModelForm and the Model
		model = Page

		# what fields do we want to include in our form?
		# we can either exclude the category field from the form
		# or specify the fields to include
		exclude = ('category',)

class UserForm(forms.ModelForm):
	password = forms.CharField(widget=forms.PasswordInput())

	class Meta:
		model = User
		fields = ('username', 'email', 'password')

class UserProfileForm(forms.ModelForm):
	class Meta:
		model = UserProfile
		fields = ('website', 'picture')
