mirror of
https://github.com/nikdoof/test-auth.git
synced 2025-12-14 06:42:16 +00:00
Decoupled the "reddit" application from the main program
This commit is contained in:
41
sso/forms.py
41
sso/forms.py
@@ -4,9 +4,10 @@ from django import forms
|
||||
from django.contrib.auth.models import User
|
||||
from django.conf import settings
|
||||
|
||||
from utils import installed
|
||||
|
||||
from eve_api.models import EVEAccount, EVEPlayerCharacter, EVEPlayerCorporation
|
||||
from sso.models import ServiceAccount, Service
|
||||
from reddit.models import RedditAccount
|
||||
from registration.forms import RegistrationForm
|
||||
|
||||
class RegistrationFormUniqueEmailBlocked(RegistrationForm):
|
||||
@@ -107,50 +108,20 @@ class ServiceAccountResetForm(forms.Form):
|
||||
self.fields['password'] = self.password
|
||||
|
||||
|
||||
class RedditAccountForm(forms.Form):
|
||||
""" Reddit Account Form """
|
||||
|
||||
username = forms.CharField(label = u'User ID', max_length=64)
|
||||
|
||||
def clean(self):
|
||||
try:
|
||||
eaccount = RedditAccount.objects.get(username=self.cleaned_data['username'])
|
||||
except RedditAccount.DoesNotExist:
|
||||
return self.cleaned_data
|
||||
else:
|
||||
raise forms.ValidationError("This User ID is already registered")
|
||||
|
||||
class UserLookupForm(forms.Form):
|
||||
""" User Lookup Form """
|
||||
|
||||
choices = [ (1, "Auth Username"),
|
||||
(2, "Character"),
|
||||
(3, "Reddit ID"),
|
||||
(4, "Email Address"), ]
|
||||
|
||||
type = forms.ChoiceField(label = u'Search type', choices = choices)
|
||||
username = forms.CharField(label = u'User ID', max_length=64)
|
||||
|
||||
def clean(self):
|
||||
|
||||
if self.cleaned_data['type'] == 1:
|
||||
try:
|
||||
acc = User.objects.filter(username=self.cleaned_data['username'])
|
||||
except User.DoesNotExist:
|
||||
raise forms.ValidationError("User doesn't exist")
|
||||
elif self.cleaned_data['type'] == 2:
|
||||
try:
|
||||
acc = EVEPlayerCharacter.filter(name=self.cleaned_data['username'])
|
||||
except User.DoesNotExist:
|
||||
raise forms.ValidationError("Character doesn't exist")
|
||||
elif self.cleaned_data['type'] == 3:
|
||||
try:
|
||||
acc = RedditAccount.filter(name=self.cleaned_data['username'])
|
||||
except User.DoesNotExist:
|
||||
raise forms.ValidationError("Account doesn't exist")
|
||||
|
||||
return self.cleaned_data
|
||||
|
||||
def __init__(self):
|
||||
if installed('reddit'):
|
||||
self.choices.append((3, "Reddit ID"))
|
||||
forms.Form.__init__(self)
|
||||
|
||||
class APIPasswordForm(forms.Form):
|
||||
|
||||
|
||||
@@ -10,7 +10,6 @@ from django.utils import simplejson as json
|
||||
|
||||
from jsonfield.fields import JSONField
|
||||
from eve_api.models import EVEAccount, EVEPlayerCorporation, EVEPlayerAlliance, EVEPlayerCharacter
|
||||
from reddit.models import RedditAccount
|
||||
|
||||
from services import get_api
|
||||
|
||||
|
||||
@@ -13,9 +13,6 @@ urlpatterns = patterns('',
|
||||
(r'^profile/del/service/(?P<serviceid>\d+)/$', views.service_del),
|
||||
(r'^profile/reset/service/(?P<serviceid>\d+)/$', views.service_reset),
|
||||
(r'^profile/reset/service/(?P<serviceid>\d+)/(?P<accept>\d+)$', views.service_reset),
|
||||
(r'^profile/add/reddit', views.reddit_add),
|
||||
(r'^profile/del/reddit/$', views.reddit_del),
|
||||
(r'^profile/del/reddit/(?P<redditid>\d+)/$', views.reddit_del),
|
||||
(r'^profile/refresh/eveapi/(?P<userid>\d+)/$', views.eveapi_refresh),
|
||||
(r'^profile/log/eveapi/(?P<userid>\d+)/$', views.eveapi_log),
|
||||
(r'^profile/characters$', views.characters),
|
||||
|
||||
50
sso/views.py
50
sso/views.py
@@ -14,15 +14,15 @@ from django.template import RequestContext
|
||||
from django.core import serializers
|
||||
from django.conf import settings
|
||||
|
||||
from utils import installed
|
||||
|
||||
from eve_api.models import EVEAccount, EVEPlayerCharacter
|
||||
from eve_api.tasks import import_apikey, import_apikey_result
|
||||
|
||||
from eve_proxy.models import ApiAccessLog
|
||||
|
||||
from sso.models import ServiceAccount, Service, SSOUser, ExistingUser, ServiceError
|
||||
from sso.forms import EveAPIForm, UserServiceAccountForm, ServiceAccountResetForm, RedditAccountForm, UserLookupForm, APIPasswordForm
|
||||
|
||||
from reddit.models import RedditAccount
|
||||
from sso.forms import EveAPIForm, UserServiceAccountForm, ServiceAccountResetForm, UserLookupForm, APIPasswordForm
|
||||
|
||||
|
||||
def index(request):
|
||||
@@ -244,47 +244,6 @@ def service_reset(request, serviceid=0):
|
||||
|
||||
return redirect('sso.views.profile')
|
||||
|
||||
@login_required
|
||||
def reddit_add(request):
|
||||
""" Add a Reddit account to a user's account """
|
||||
|
||||
if request.method == 'POST':
|
||||
form = RedditAccountForm(request.POST)
|
||||
if form.is_valid():
|
||||
acc = RedditAccount()
|
||||
acc.user = request.user
|
||||
acc.username = form.cleaned_data['username']
|
||||
try:
|
||||
acc.api_update()
|
||||
except RedditAccount.DoesNotExist:
|
||||
messages.add_message(request, messages.ERROR, "Error, user %s does not exist on Reddit" % acc.username )
|
||||
return render_to_response('sso/redditaccount.html', locals(), context_instance=RequestContext(request))
|
||||
acc.save()
|
||||
|
||||
messages.add_message(request, messages.INFO, "Reddit account %s successfully added." % acc.username)
|
||||
return redirect('sso.views.profile') # Redirect after POST
|
||||
else:
|
||||
defaults = { 'username': request.user.username, }
|
||||
form = RedditAccountForm(defaults) # An unbound form
|
||||
|
||||
return render_to_response('sso/redditaccount.html', locals(), context_instance=RequestContext(request))
|
||||
|
||||
@login_required
|
||||
def reddit_del(request, redditid=0):
|
||||
""" Delete a Reddit account from a user's account """
|
||||
|
||||
if redditid > 0 :
|
||||
try:
|
||||
acc = RedditAccount.objects.get(id=redditid)
|
||||
except RedditAccount.DoesNotExist:
|
||||
return redirect('sso.views.profile')
|
||||
|
||||
if acc.user == request.user:
|
||||
acc.delete()
|
||||
messages.add_message(request, messages.INFO, "Reddit account successfully deleted.")
|
||||
|
||||
return redirect('sso.views.profile')
|
||||
|
||||
@login_required
|
||||
def user_view(request, username=None):
|
||||
""" View a user's profile as a admin """
|
||||
@@ -301,7 +260,6 @@ def user_view(request, username=None):
|
||||
is_admin = request.user.is_staff
|
||||
if is_admin:
|
||||
services = ServiceAccount.objects.select_related('service').filter(user=user).only('service__name', 'service_uid', 'active')
|
||||
reddits = RedditAccount.objects.filter(user=user).all()
|
||||
characters = EVEPlayerCharacter.objects.select_related('corporation').filter(eveaccount__user=user).only('id', 'name', 'corporation__name')
|
||||
|
||||
return render_to_response('sso/lookup/user.html', locals(), context_instance=RequestContext(request))
|
||||
@@ -323,7 +281,7 @@ def user_lookup(request):
|
||||
uid = EVEAccount.objects.filter(characters__name__icontains=form.cleaned_data['username']).values('user')
|
||||
for u in uid: uids.append(u['user'])
|
||||
users = User.objects.filter(id__in=uids).only('username')
|
||||
elif form.cleaned_data['type'] == '3':
|
||||
elif installed('reddit') and form.cleaned_data['type'] == '3':
|
||||
uid = RedditAccount.objects.filter(username__icontains=form.cleaned_data['username']).values('user')
|
||||
for u in uid: uids.append(u['user'])
|
||||
users = User.objects.filter(id__in=uids).only('username')
|
||||
|
||||
Reference in New Issue
Block a user