1
0
mirror of https://github.com/creyD/asiimov.git synced 2026-06-12 00:52:23 +02:00

Profile and Settings Work

- Added logout button + url
- Added redirect urls
- Fixed bug where the steam name wouldn't show in template
- Added tradeurl to model
- Added simple profile
- Added ability to change own trade url
This commit is contained in:
2020-01-19 11:14:34 +01:00
parent 1f8aaa7277
commit 9d32b12b05
14 changed files with 167 additions and 19 deletions

View File

@@ -27,7 +27,6 @@ ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'core',
'django.contrib.admin',
@@ -71,7 +70,6 @@ WSGI_APPLICATION = 'asiimov.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
@@ -82,7 +80,6 @@ DATABASES = {
# Password validation
# https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
@@ -101,7 +98,6 @@ AUTH_PASSWORD_VALIDATORS = [
# Internationalization
# https://docs.djangoproject.com/en/3.0/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
@@ -119,3 +115,10 @@ STATICFILES_DIRS = [
# Steam API variables
STEAM_API_KEY = os.environ['STEAM_API_KEY']
# Redirect for users to login
LOGIN_URL = 'signup'
# Redirect after login
LOGIN_REDIRECT_URL = 'me'
# Redirect after logout
LOGOUT_REDIRECT_URL = 'home'

View File

@@ -15,8 +15,10 @@ Including another URLconf
"""
from django.contrib import admin
from django.urls import path, include
from django.contrib.auth.views import LogoutView
urlpatterns = [
path('', include('core.urls')),
path('admin/', admin.site.urls)
path('admin/', admin.site.urls),
path('logout/', LogoutView.as_view(template_name='logout.html'), name='logout')
]

View File

@@ -0,0 +1,3 @@
.content_block{
text-align: center;
}

View File

@@ -13,6 +13,7 @@
<link rel="stylesheet" href="{% static 'css/master.css' %}">
<!-- JQuery for easier HTML manipulation -->
<script type="text/javascript" src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
{% block header %}{% endblock %}
</head>
<body>
@@ -22,7 +23,7 @@
</div>
<div id="right_nav" class="nav_box">
{% if user.is_authenticated %}
<a href="{% url 'offer_create' %}">Add Offer</a> | <a href="{% url 'me_settings' %}">settings</a> | Welcome, <a href="{% url 'me' %}">{{ user.gamer.display_name }}</a>
<a href="{% url 'offer_create' %}">Add Offer</a> | <a href="{% url 'me_settings' %}">Settings</a> | Welcome, <a href="{% url 'me' %}">{{ user.gamer.personaname }} <a href="{% url 'logout' %}">(Logout)</a></a>
{% else %}
<a href="{% url 'signup' %}"><img src="{% static 'pic/steam_sign_in.png' %}" /></a>
{% endif %}

9
src/core/forms.py Normal file
View File

@@ -0,0 +1,9 @@
# Import forms for creating custom template forms
from django import forms
from .models import Gamer
class ChangeTradeUrl(forms.ModelForm):
class Meta:
model = Gamer
fields = ('tradeurl',)

View File

@@ -0,0 +1,18 @@
# Generated by Django 3.0.2 on 2020-01-19 09:15
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('core', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='gamer',
name='tradeurl',
field=models.URLField(max_length=256, null=True),
),
]

View File

@@ -0,0 +1,18 @@
# Generated by Django 3.0.2 on 2020-01-19 09:34
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('core', '0002_gamer_tradeurl'),
]
operations = [
migrations.AddField(
model_name='gamer',
name='offer_count',
field=models.IntegerField(default=0),
),
]

View File

@@ -91,11 +91,14 @@ class Gamer(models.Model):
timecreated = models.IntegerField(null=True) # Only visible if user has profile visibility
loccountrycode = models.CharField(max_length=2, null=True) # 2 char ISO country code
tradeurl = models.URLField(max_length=256, null=True)
# Asiimov specific information
inventory = models.ManyToManyField(ItemInstance) # Temporary storage of items for improving site performance
API_KEY = models.CharField(max_length=32, null=True) # Optionally use the API KEY of the user
badges = models.ManyToManyField(Badge)
system_user = models.OneToOneField(User, on_delete=models.CASCADE)
offer_count = models.IntegerField(default=0)
class Offer(models.Model):

View File

@@ -15,7 +15,7 @@ FLOAT_SERVER = 'https://api.csgofloat.com/?url='
def getUserInfo(steamID, API_KEY=settings.STEAM_API_KEY):
QUERY = STEAM_SERVER + USER_METHOD + '/?key=' + str(API_KEY) + '&format=json&steamids=' + str(steamID)
player_object = json.load(urllib.request.urlopen(QUERY))
return player_object
return player_object['response']['players'][0]
# Get the CS:GO inventory of a gamer

View File

@@ -1 +0,0 @@
{{ url }}

View File

@@ -0,0 +1,43 @@
{% extends 'master.html' %}
{% load static %}
{% block header %}
<!-- Adding custom style sheet for profile-->
<link rel="stylesheet" href="{% static 'css/profile.css' %}">
{% endblock %}
{% block content %}
<div class="content_block">
<p><img src="{{ gamer.avatar }}" /></p>
<h2>{{ gamer.personaname }} {% if request.user.gamer == gamer %}(Your Profile){% endif %}</h2>
<h3>Links</h3>
<ul>
<a href="{{ gamer.profileurl }}" target="_blank"><li>Steam</li></a>
<a href="http://csgo.exchange/id/{{ gamer.steamid }}"><li>csgo.exchange</li></a>
{% if gamer.tradeurl %}<a href="{{ gamer.tradeurl }}" target="_blank"><li>Trade</li></a>{% endif %}
</ul>
<h3>Info</h3>
<p>Steam ID: {{ gamer.steamid }}</p>
<p>Profile: {% if gamer.profilestate %}Public Profile{% else %}Private Profile{% endif %}</p>
<p>Comments: {% if gamer.commentpermission %}Allowed{% else %}Denied{% endif %}</p>
<p>Locale: {% if gamer.loccountrycode %}{{ gamer.loccountrycode }}{% else %}Not provided{% endif %}</p>
{% if gamer.API_KEY %}<p style="color: red">This is an API Key Sponsor!</p>{% endif %}
<h3>Badges</h3>
<p>
{% for badge in gamer.badges.all %}
<img src="{{ badge.icon }}" placeholder="{{ badge.name }}">
{% empty %}
No badges earned yet.
{% endfor %}
</p>
<h3>Statistics</h3>
<p>Offers created: {{ gamer.offer_count }}</p>
<p>Live Offers: {{ live_offers }}</p>
<p>Member since: {{ gamer.system_user.date_joined }}</p>
<p>Last Login: {{ gamer.system_user.last_login }}</p>
</div>
{% endblock %}

View File

@@ -0,0 +1,21 @@
{% extends 'master.html' %}
{% load static %}
{% block header %}
<!-- Adding custom style sheet for profile-->
<link rel="stylesheet" href="{% static 'css/profile.css' %}">
{% endblock %}
{% block content %}
<div class="content_block">
<h2>Settings</h2>
<a href="http://steamcommunity.com/id/me/tradeoffers/privacy#trade_offer_access_url" target="_blank">Find your trade URL!</a>
{% if gamer.API_KEY %}<a href="{% url 'profile_update' gamer.steamid %}">Synchronize profile from Steam</a>{% endif %}
<form role="from" method="post" action="{% url 'me_settings' %}">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Set Trade URL</button>
</form>
</div>
{% endblock %}

View File

@@ -11,6 +11,7 @@ urlpatterns = [
path('offers/create', views.offer_create, name='offer_create'),
path('search/<str:filter>', views.search, name='search'),
path('profile/<int:steamID>', views.profile, name='profile'),
path('profile/<int:steamID>/update', views.profile_update, name='profile_update'),
path('profile/me', views.me, name='me'),
path('profile/settings', views.me_settings, name='me_settings'),

View File

@@ -17,6 +17,8 @@ from .steam_api import getUserInfo
# Import for manually logging in user after creation
from django.contrib.auth import login
from .forms import ChangeTradeUrl
# HELPER
def validate_steam_login(params):
@@ -92,14 +94,14 @@ def signup_confirm(request):
Gamer.objects.create(
steamid=claimed_id,
system_user=new_user,
communityvisibilitystate=(True if info['response']['players'][0]['communityvisibilitystate'] == 3 else False),
profilestate=info['response']['players'][0]['profilestate'],
personaname=info['response']['players'][0]['personaname'],
profileurl=info['response']['players'][0]['profileurl'],
avatar=info['response']['players'][0]['avatar'],
commentpermission=info['response']['players'][0]['commentpermission'],
timecreated=info['response']['players'][0]['timecreated'] or None,
loccountrycode=info['response']['players'][0]['loccountrycode'] or None
communityvisibilitystate=(True if info['communityvisibilitystate'] == 3 else False),
profilestate=info['profilestate'],
personaname=info['personaname'],
profileurl=info['profileurl'],
avatar=info['avatar'],
commentpermission=info['commentpermission'],
timecreated=info['timecreated'] or None,
loccountrycode=info['loccountrycode'] or None
)
login(request, new_user)
return redirect(me)
@@ -132,16 +134,41 @@ def offer_create(request):
@login_required
def profile(request, steamID):
dude = get_object_or_404(Gamer, steamid=steamID)
return render(request, 'core/profile.html', {'gamer': dude})
return render(request, 'profile/profile.html', {'gamer': dude, 'live_offers': Offer.objects.filter(offeror=dude).count()})
@login_required
def profile_update(request, steamID):
if (request.user.steamid == steamID and request.user.gamer.API_KEY) or request.user.is_staff:
the_gamer = get_object_or_404(Gamer, steamid=steamID)
info = getUserInfo(steamID, API_KEY=request.user.gamer.API_KEY or None)
the_gamer.communityvisibilitystate = (True if info['communityvisibilitystate'] == 3 else False)
the_gamer.profilestate = info['profilestate']
the_gamer.personaname = info['personaname']
the_gamer.profileurl = info['profileurl']
the_gamer.avatar = info['avatar']
the_gamer.commentpermission = info['commentpermission']
if 'timecreated' in info:
the_gamer.timecreated = info['timecreated']
if 'loccountrycode' in info:
the_gamer.loccountrycode = info['loccountrycode']
the_gamer.save()
return redirect(profile, steamID=steamID)
else:
return HttpResponseForbidden()
# PRIVATE AREA
@login_required
def me(request):
return render(request, 'core/profile.html', {'gamer': Gamer.objects.get(system_user=request.user)})
dude = get_object_or_404(Gamer, steamid=request.user.gamer.steamid)
return render(request, 'profile/profile.html', {'gamer': dude, 'live_offers': Offer.objects.filter(offeror=dude).count()})
@login_required
def me_settings(request):
dude = get_object_or_404(Gamer, system_user=request.user)
return render(request, 'core/settings.html', {'gamer': dude})
trade_form = ChangeTradeUrl(request.POST or None, instance=dude)
if trade_form.is_valid() and request.method == 'POST':
dude.tradeurl = trade_form.cleaned_data['trade_url']
dude.save()
return render(request, 'profile/settings.html', {'gamer': dude, 'form': trade_form})