і приклад:
from django.template.defaultfilters import slugify
import unidecode
from unidecode import unidecode
#...
task_object.slug = slugify(unidecode(u'%s' % name))
from django.template.defaultfilters import slugify
import unidecode
from unidecode import unidecode
#...
task_object.slug = slugify(unidecode(u'%s' % name))
class Task(models.Model):
location = models.ForeignKey(Locations, related_name='Task_location')
buyer = models.ForeignKey(UserProfile, related_name='Task_buyer')
class Locations(models.Model):
user = models.ForeignKey('registration.UserProfile', \
verbose_name=_('user'), related_name="user_locations")
class UserProfile(User):
class TaskForm(ModelForm):
class Meta:
model = Task
fields = ('location', 'name', 'description')
@login_required
@render_to('')
def task_add(request, template="tasks/add.html"):
if request.method == 'POST':
form = TaskForm(request.POST, user=request.user)
if form.is_valid():
task = form.save(request.user, 1)
TaskStatus.objects.create(task=task, task_status=1)
return redirect("home")
else:
form = TaskForm(user=request.user)
return {'form': form,}, template
class TaskForm(ModelForm):
class Meta:
model = Task
fields = ('category', 'location', 'name', 'description', 'price')
def __init__(self, *args, **kwargs):
self.user = kwargs.pop('user', None)
super(TaskForm, self).__init__(*args, **kwargs)
self.fields['location'].queryset=Locations.objects.filter(user=self.user)
def save(self, user=None, task_status=None, commit=True):
task_object = super(TaskForm, self).save(commit=False)
task_object.buyer = user
task_object.task_status = task_status
if commit:
task_object.save()
return task_object
{% load thumbnail %}
{% thumbnail r4.from_user.avatar "50x50" crop="center" as im %}
<img src="{{ im.url }}" width="{{ im.width }}" height="{{ im.height }}">
{% endthumbnail %}
class Task(models.Model):
name = models.CharField(max_length=120)
category = models.ForeignKey('TaskCategory')
description = models.TextField()
price = models.DecimalField(max_digits=5, decimal_places=2, blank=True)
buyer = models.ForeignKey(Profile, related_name='buyer')
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Meta:
verbose_name = _('Task')
verbose_name_plural = _('Tasks')
ordering = ['created_at']
def __unicode__(self):
return self.name
def current_status_str(self):
task_status = TaskStatus.objects.filter(task=self)
current_status = task_status.aggregate(Max('task_status'))
return TaskStatus.TASK_STATUS_VALUES[current_status['task_status__max']-1][1]
def current_status_int(self):
task_status = TaskStatus.objects.filter(task=self)
current_status = task_status.aggregate(Max('task_status'))
return current_status['task_status__max']
def get_absolute_url(self):
return reverse('tasks_details', args=[self.pk])
class TaskStatus(models.Model):
POSTED, ASSIGNED, COMPLETED, CLOSED = range(1,5)
TASK_STATUS_VALUES = (
(POSTED, 'Posted'),
(ASSIGNED, 'Assigned'),
(COMPLETED, 'Completed'),
(CLOSED, 'Closed'),
)
task = models.ForeignKey(Task)
task_status = models.IntegerField(choices=TASK_STATUS_VALUES, blank=True, null=True)
provider = models.ForeignKey(User, blank=True, null=True)
created_at = models.DateTimeField(auto_now_add=True)
def timesince(self, now=None):
return timesince_(self.created_at, now)
def __unicode__(self):
return unicode(self.task)
{% for task in tasks %}
{{ task }}
{{ task.current_status_str }}
{% empty %}
no tasks yet
{% endfor %}
from django.utils.timesince import timesince as timesince_
class Review(models.Model):
to_user = models.ForeignKey('users.Profile', verbose_name=_('from user'), \
related_name="review_to_user")
from_user = models.ForeignKey('users.Profile', verbose_name=_('to user'), \
related_name="review_from_user")
timestamp = models.DateTimeField(_('timestamp'), auto_now_add=True)
class Meta:
verbose_name = _('rating')
verbose_name_plural = _('ratings')
ordering = ['timestamp']
def timesince(self, now=None):
return timesince_(self.timestamp, now)
def __unicode__(self):
return unicode("Review to %s from %s %s ago" % \
(self.to_user, self.from_user, self.timesince()))
def get_absolute_url(self):
return reverse('reviews_reviews', args=[self.to_user])
class TaskStatus(models.Model):
POSTED, ASSIGNED, COMPLETED, CLOSED = range(1,5)
TASK_STATUS_VALUES = (
(POSTED, 'Posted'),
(ASSIGNED, 'Assigned'),
(COMPLETED, 'Completed'),
(CLOSED, 'Closed'),
)
task = models.ForeignKey(Task)
task_status = models.IntegerField(choices=TASK_STATUS_VALUES, blank=True, null=True)
{% for ts in task_status %}
{{ ts.get_task_status_display}}
{% endfor %}
import pprint
assert False, pprint.pformat(object)
assert False, pprint.pformat(object.__dict__)
assert False, pprint.pformat(object.__dict__.keys())
assert False, pprint.pformat(object.__dict__.values())
class Task(models.Model):
name = models.CharField(max_length=120)
buyer = models.ForeignKey(Profile, related_name='buyer')
class TaskStatus(models.Model):
POSTED, ASSIGNED, COMPLETED, CLOSED = range(1,5)
TASK_STATUS_VALUES = (
(POSTED, 'Posted'),
(ASSIGNED, 'Assigned'),
(COMPLETED, 'Completed'),
(CLOSED, 'Closed'),
)
task = models.ForeignKey(Task)
task_status = models.IntegerField(choices=TASK_STATUS_VALUES, blank=True, null=True)
TaskStatus.objects.filter(task__buyer=request.user)
[
{
"pk": 64,
"model": "auth.permission",
"fields": {
"codename": "add_attachment",
"name": "Can add attachment",
"content_type": 22
}
},
{
"pk": 65,
"model": "auth.permission",
"fields": {
"codename": "change_attachment",
"name": "Can change attachment",
"content_type": 22
}
},
{
"pk": 66,
"model": "auth.permission",
"fields": {
"codename": "delete_attachment",
"name": "Can delete attachment",
"content_type": 22
}
},
{
"pk": 67,
"model": "auth.permission",
"fields": {
"codename": "delete_foreign_attachments",
"name": "Can delete foreign attachments",
"content_type": 22
}
},
{
"pk": 1,
"model": "auth.group",
"fields": {
"name": "attachments",
"permissions": [64, 65, 66, 67]
}
},
]
class ReviewForm(ModelForm):
RATING_CHOICES = ((1, '1'), (2, '2'), (3, '3'), (4, '4'), (5, '5'),)
rate = forms.TypedChoiceField(choices=RATING_CHOICES, \
widget=forms.RadioSelect, coerce=int)
def clean_rate(self):
if self.cleaned_data['rate'] >= 1 and self.cleaned_data['rate'] <= 5:
return self.cleaned_data['rate']
else:
raise forms.ValidationError(_("Rating error"))
class Meta:
model = Reviews
fields = ('rate', 'message', 'content_type', 'object_id')
widgets = {
'content_type' : forms.HiddenInput(),
'object_id' : forms.HiddenInput()
}
class Reviews(models.Model):
object_id = models.PositiveIntegerField(_('object id'))
content_type = models.ForeignKey(ContentType, verbose_name=_('content type'))
object = GenericForeignKey('content_type', 'object_id')
rate = models.DecimalField(decimal_places=1, max_digits=2)
user = models.ForeignKey(User, verbose_name=_('user'))
message = models.TextField(_('message'))
timestamp = models.DateTimeField(_('timestamp'), auto_now_add=True)
def __unicode__(self):
return unicode(self.user)
class ReviewForm(ModelForm):
class Meta:
model = Reviews
fields = ('rate', 'message', 'content_type', 'object_id')
widgets = {
'content_type' : forms.HiddenInput(),
'object_id' : forms.HiddenInput()
}
if request.method == "POST":
form = ReviewForm(request.POST)
if form.is_valid():
r = form.save(commit=False)
r.user = request.user
r.save()
return redirect('home')
else:
ct = ContentType.objects.get_for_model(user)
form = ReviewForm(initial={'content_type': ct.pk, 'object_id': user.id})
class Task(models.Model):
name = models.CharField(max_length=120)
description = models.TextField()
price = models.DecimalField(max_digits=5, decimal_places=2)
buyer = models.ForeignKey(User, related_name='buyer')
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __unicode__(self):
return self.name
class TaskStatus(models.Model):
POSTED, ASSIGNED, COMPLETED, CLOSED = range(1,5)
TASK_STATUS_VALUES = (
(POSTED, 'Posted'),
(ASSIGNED, 'Assigned'),
(COMPLETED, 'Completed'),
(CLOSED, 'Closed'),
)
task = models.ForeignKey(Task)
task_status = models.IntegerField(choices=TASK_STATUS_VALUES, blank=True, null=True)
provider = models.ForeignKey(User, blank=True, null=True)
created_at = models.DateTimeField(auto_now_add=True)
def __unicode__(self):
return self.task
def __unicode__(self):
return unicode(self.task)