Skip to content

Commit

Permalink
Add search for example and example on admin anyway
Browse files Browse the repository at this point in the history
  • Loading branch information
pydanny committed Nov 4, 2012
1 parent 8c3b564 commit 22b0aaf
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 35 deletions.
7 changes: 7 additions & 0 deletions package/admin.py
Expand Up @@ -35,8 +35,15 @@ class CommitAdmin(admin.ModelAdmin):

class VersionLocalAdmin(admin.ModelAdmin):
search_fields = ("package__title",)

class PackageExampleAdmin(admin.ModelAdmin):

list_display = ("title", )
search_fields = ("title",)


admin.site.register(Category, VersionAdmin)
admin.site.register(Package, PackageAdmin)
admin.site.register(Commit, CommitAdmin)
admin.site.register(Version, VersionLocalAdmin)
admin.site.register(PackageExample, PackageExampleAdmin)
13 changes: 8 additions & 5 deletions profiles/admin.py
Expand Up @@ -4,17 +4,20 @@

from profiles.models import Profile


def username(obj):
return (obj.user.username)
username.short_description="User username"
username.short_description = "User username"


def user_email(obj):
return (obj.user.email)
username.short_description="User email"
username.short_description = "User email"


class ProfileAdmin(VersionAdmin):
search_fields = ("user__username","github_account", "user__email", "email")

search_fields = ("user__username", "github_account", "user__email", "email")
list_display = ("github_account", "email", username, user_email)

admin.site.register(Profile, ProfileAdmin)
admin.site.register(Profile, ProfileAdmin)
4 changes: 2 additions & 2 deletions profiles/context_processors.py
@@ -1,14 +1,14 @@
from django.conf import settings
from django.utils.functional import lazy, memoize, SimpleLazyObject


def lazy_profile(request):
"""
Returns context variables required by templates that assume a profile
on each request
"""

def get_user_profile():
if hasattr(request,'profile'):
if hasattr(request, 'profile'):
return request.profile
else:
return request.user.get_profile()
Expand Down
6 changes: 2 additions & 4 deletions profiles/forms.py
@@ -1,17 +1,15 @@
from django import forms
from django.contrib.auth.models import User
from django.utils.translation import ugettext_lazy as _

from profiles.models import Profile


class ProfileForm(forms.ModelForm):

def __init__(self, *args, **kwargs):
super(ProfileForm, self).__init__(*args, **kwargs)

class Meta:

fields = (
'bitbucket_url',
'google_code_url',
Expand Down
21 changes: 10 additions & 11 deletions profiles/models.py
Expand Up @@ -5,53 +5,52 @@

from core.models import BaseModel

from package.models import Package

class Profile(BaseModel):
user = models.OneToOneField(User)
user = models.OneToOneField(User)

# Note to coders: The '_url' fields below need to JUST be the name of the account.
# Examples:
# github_url = 'pydanny'
# bitbucket_url = 'pydanny'
# google_code_url = 'pydanny'
# google_code_url = 'pydanny'
github_account = models.CharField(_("Github account"), null=True, blank=True, max_length=40)
github_url = models.CharField(_("Github account"), null=True, blank=True, max_length=100, editable=False)
bitbucket_url = models.CharField(_("Bitbucket account"), null=True, blank=True, max_length=100)
google_code_url = models.CharField(_("Google Code account"), null=True, blank=True, max_length=100)
email = models.EmailField(_("Email"), null=True, blank=True)

def __unicode__(self):
if not self.github_account:
return self.user.username
return self.github_account

def save(self, **kwargs):
""" Override save to always populate email changes to auth.user model
"""
if self.email is not None:

email = self.email.strip()
user_obj = User.objects.get(username=self.user.username)
user_obj.email = email
user_obj.save()

super(Profile,self).save(**kwargs)
super(Profile, self).save(**kwargs)

def url_for_repo(self, repo):
"""Return the profile's URL for a given repo.
If url doesn't exist return None.
"""
url_mapping = {
'Github': self.github_account,
'BitBucket': self.bitbucket_url,
'Google Code': self.google_code_url}
return url_mapping.get(repo.title)

def my_packages(self):
"""Return a list of all packages the user contributes to.
List is sorted by package name.
"""
from package.repos import get_repo, supported_repos
Expand Down
8 changes: 3 additions & 5 deletions profiles/urls.py
@@ -1,11 +1,9 @@
from django.conf.urls.defaults import patterns, url
from django.db.models import Count

from profiles import views

urlpatterns = patterns("",
url(r"^edit/$", views.profile_edit, name="profile_edit"),
url(r"^$", views.profile_list, name="profile_list"),
url(r"^(?P<github_account>[-\w]+)$", views.profile_detail, name="profile_detail"),

url(r"^edit/$", views.profile_edit, name="profile_edit"),
url(r"^$", views.profile_list, name="profile_list"),
url(r"^(?P<github_account>[-\w]+)$", views.profile_detail, name="profile_detail"),
)
20 changes: 12 additions & 8 deletions profiles/views.py
Expand Up @@ -14,12 +14,14 @@
from profiles.forms import ProfileForm
from profiles.models import Profile


def profile_detail(request, github_account, template_name="profiles/profile.html"):

profile = get_object_or_404(Profile, github_account=github_account)

return render(request, template_name,
{"local_profile": profile, "user":profile.user},)
{"local_profile": profile, "user": profile.user},)


def profile_list(request, template_name="profiles/profiles.html"):

Expand All @@ -33,6 +35,7 @@ def profile_list(request, template_name="profiles/profiles.html"):
"users": users
})


@login_required
def profile_edit(request, template_name="profiles/profile_edit.html"):

Expand All @@ -43,15 +46,15 @@ def profile_edit(request, template_name="profiles/profile_edit.html"):
form.save()
msg = 'Profile edited'
messages.add_message(request, messages.INFO, msg)
return HttpResponseRedirect(reverse("profile_detail", kwargs={"github_account":profile.github_account }))
return HttpResponseRedirect(reverse("profile_detail", kwargs={"github_account": profile.github_account}))

# TODO - move this to a template
github_account = """
<div
id="div_id_github_account"
<div
id="div_id_github_account"
class="ctrlHolder"><label for="id_github_account" >Github account</label><strong>{0}</strong></div>
""".format(profile.github_account)

helper = FormHelper()
helper.form_class = "profile-edit-form"
helper.layout = Layout(
Expand All @@ -64,15 +67,16 @@ def profile_edit(request, template_name="profiles/profile_edit.html"):
ButtonHolder(
Submit('edit', 'Edit', css_class="awesome forestgreen"),
)
)
)

return render(request, template_name,
{
"profile": profile,
"form": form,
"helper":helper,
"helper": helper,
})


def github_user_update(sender, user, response, details, **kwargs):
profile_instance, created = Profile.objects.get_or_create(user=user)
profile_instance.github_account = details['username']
Expand Down

0 comments on commit 22b0aaf

Please sign in to comment.