Commit 4ee7a6dd authored by okhin's avatar okhin

Merge branch 'api_fix' into 'master'

Api fix

Add photo url on /contacts api.

Limit list on admin views to related organisations, group type, etc

Fix some i18n issues

See merge request !7
parents ddae0222 c03e92f3
from django.contrib import admin from django.contrib import admin
from picampaign.campaign.models import (Campaign, CampaignContact, from picampaign.campaign.models import Campaign, CampaignContact, Argumentary
Argumentary) from picampaign.organization.models import Organization
class InlineContact(admin.TabularInline): class InlineContact(admin.TabularInline):
...@@ -17,6 +17,15 @@ class CampaignAdmin(admin.ModelAdmin): ...@@ -17,6 +17,15 @@ class CampaignAdmin(admin.ModelAdmin):
user_orgs = [x.id for x in request.user.organizations.all()] user_orgs = [x.id for x in request.user.organizations.all()]
return qs.filter(organization__in=user_orgs) return qs.filter(organization__in=user_orgs)
def formfield_for_foreignkey(self, db_field, request, **kwargs):
if db_field.name == "organization":
query = {'id__in': [x.id
for x in request.user.organizations.all()]}
kwargs["queryset"] = Organization.objects.filter(**query)
return super(CampaignAdmin, self).formfield_for_foreignkey(db_field,
request,
**kwargs)
class CampaignContactAdmin(admin.ModelAdmin): class CampaignContactAdmin(admin.ModelAdmin):
...@@ -37,6 +46,15 @@ class ArgumentaryAdmin(admin.ModelAdmin): ...@@ -37,6 +46,15 @@ class ArgumentaryAdmin(admin.ModelAdmin):
user_orgs = [x.id for x in request.user.organizations.all()] user_orgs = [x.id for x in request.user.organizations.all()]
return qs.filter(campaign__organization__in=user_orgs) return qs.filter(campaign__organization__in=user_orgs)
def formfield_for_foreignkey(self, db_field, request, **kwargs):
if db_field.name == "campaign":
orgs = request.user.organizations.all()
query = {'organization__in': orgs}
kwargs["queryset"] = Campaign.objects.filter(**query)
return super(ArgumentaryAdmin, self).formfield_for_foreignkey(db_field,
request,
**kwargs)
admin.site.register(Campaign, CampaignAdmin) admin.site.register(Campaign, CampaignAdmin)
admin.site.register(CampaignContact, CampaignContactAdmin) admin.site.register(CampaignContact, CampaignContactAdmin)
......
...@@ -35,8 +35,8 @@ class Argumentary(models.Model): ...@@ -35,8 +35,8 @@ class Argumentary(models.Model):
text = models.TextField(null=True) text = models.TextField(null=True)
def __unicode__(self): def __unicode__(self):
return _('Argumentary in %(lang) for %(title)') % {'lang': self.lang, args = {'lang': self.lang, 'title': self.campaign.title}
'title': self.campaign.title} return _('Argumentary in %(lang)s for %(title)s') % args
class CampaignContact(models.Model): class CampaignContact(models.Model):
...@@ -47,8 +47,8 @@ class CampaignContact(models.Model): ...@@ -47,8 +47,8 @@ class CampaignContact(models.Model):
weight = models.IntegerField(default=0) weight = models.IntegerField(default=0)
def __unicode__(self): def __unicode__(self):
return _('Contact %(ccontact) on %(title)') % {'contact': self.contact, args = {'contact': self.contact, 'title': self.campaign.title}
'title': self.campaign.title} return _('Contact %(contact)s on %(title)s') % args
def all_groups(self): def all_groups(self):
return [x.name for x in self.contact.groups.all()] return [x.name for x in self.contact.groups.all()]
...@@ -25,8 +25,9 @@ class CampaignContactSerializer(serializers.HyperlinkedModelSerializer): ...@@ -25,8 +25,9 @@ class CampaignContactSerializer(serializers.HyperlinkedModelSerializer):
phone = serializers.Field(source='contact.phone') phone = serializers.Field(source='contact.phone')
groups = GroupSerializer(many=True, source='contact.groups') groups = GroupSerializer(many=True, source='contact.groups')
contact_id = serializers.Field(source='contact.id') contact_id = serializers.Field(source='contact.id')
photo = serializers.Field(source='contact.get_photo_url')
class Meta: class Meta:
model = CampaignContact model = CampaignContact
fields = ('id', 'weight', 'contact_id', fields = ('id', 'weight', 'contact_id',
'first_name', 'last_name', 'phone', 'groups') 'first_name', 'last_name', 'phone', 'groups', 'photo')
...@@ -13,5 +13,10 @@ class Contact(models.Model): ...@@ -13,5 +13,10 @@ class Contact(models.Model):
photo = models.ImageField(upload_to='contacts/photos', blank=True) photo = models.ImageField(upload_to='contacts/photos', blank=True)
def __unicode__(self): def __unicode__(self):
return _('%(firstname) %(lastname)') % {'firstname': self.first_name, return _('%(firstname)s %(lastname)s') % {'firstname': self.first_name,
'lastname': self.last_name} 'lastname': self.last_name}
def get_photo_url(self):
if self.photo:
return self.photo.url
return u''
...@@ -11,6 +11,6 @@ class Feedback(models.Model): ...@@ -11,6 +11,6 @@ class Feedback(models.Model):
comment = models.CharField(max_length=512, blank=True) comment = models.CharField(max_length=512, blank=True)
def __unicode__(self): def __unicode__(self):
return _('feedback for %(callee contact) on %(campaign title)') % \ return _('feedback for %(callee contact)s on %(campaign title)s') % \
{'callee contact': self.callee.contact, {'callee contact': self.callee.contact,
'campaign title': self.callee.campaign.title} 'campaign title': self.callee.campaign.title}
...@@ -21,6 +21,15 @@ class GroupTypeAdmin(admin.ModelAdmin): ...@@ -21,6 +21,15 @@ class GroupTypeAdmin(admin.ModelAdmin):
user_orgs = [x.id for x in request.user.organizations.all()] user_orgs = [x.id for x in request.user.organizations.all()]
return qs.filter(organization__in=user_orgs) return qs.filter(organization__in=user_orgs)
def formfield_for_foreignkey(self, db_field, request, **kwargs):
if db_field.name == "organization":
orgs = request.user.organizations.all()
query = {'id__in': orgs}
kwargs["queryset"] = Organization.objects.filter(**query)
return super(GroupTypeAdmin, self).formfield_for_foreignkey(db_field,
request,
**kwargs)
class GroupAdmin(admin.ModelAdmin): class GroupAdmin(admin.ModelAdmin):
def get_queryset(self, request): def get_queryset(self, request):
...@@ -30,6 +39,19 @@ class GroupAdmin(admin.ModelAdmin): ...@@ -30,6 +39,19 @@ class GroupAdmin(admin.ModelAdmin):
user_orgs = [x.id for x in request.user.organizations.all()] user_orgs = [x.id for x in request.user.organizations.all()]
return qs.filter(organization__in=user_orgs) return qs.filter(organization__in=user_orgs)
def formfield_for_foreignkey(self, db_field, request, **kwargs):
if db_field.name == "organization":
orgs = request.user.organizations.all()
query = {'id__in': orgs}
kwargs["queryset"] = Organization.objects.filter(**query)
elif db_field.name == "type":
orgs = request.user.organizations.all()
query = {'organization__in': orgs}
kwargs["queryset"] = GroupType.objects.filter(**query)
return super(GroupAdmin, self).formfield_for_foreignkey(db_field,
request,
**kwargs)
class CategoryAdmin(admin.ModelAdmin): class CategoryAdmin(admin.ModelAdmin):
def get_queryset(self, request): def get_queryset(self, request):
...@@ -39,6 +61,15 @@ class CategoryAdmin(admin.ModelAdmin): ...@@ -39,6 +61,15 @@ class CategoryAdmin(admin.ModelAdmin):
user_orgs = [x.id for x in request.user.organizations.all()] user_orgs = [x.id for x in request.user.organizations.all()]
return qs.filter(organization__in=user_orgs) return qs.filter(organization__in=user_orgs)
def formfield_for_foreignkey(self, db_field, request, **kwargs):
if db_field.name == "organization":
orgs = request.user.organizations.all()
query = {'id__in': orgs}
kwargs["queryset"] = Organization.objects.filter(**query)
return super(CategoryAdmin, self).formfield_for_foreignkey(db_field,
request,
**kwargs)
admin.site.register(Organization, OrganizationAdmin) admin.site.register(Organization, OrganizationAdmin)
admin.site.register(GroupType, GroupTypeAdmin) admin.site.register(GroupType, GroupTypeAdmin)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment