diff --git a/khaikang/admin.py b/khaikang/admin.py new file mode 100644 index 0000000..db11e31 --- /dev/null +++ b/khaikang/admin.py @@ -0,0 +1,7 @@ +from django.contrib import admin +from django.contrib.auth.admin import UserAdmin +from .models import User, Post, Following + +admin.site.register(User, UserAdmin) +admin.site.register(Post) +admin.site.register(Following) diff --git a/khaikang/models.py b/khaikang/models.py index fde83cf..0c27bf2 100644 --- a/khaikang/models.py +++ b/khaikang/models.py @@ -1,12 +1,14 @@ from django.db import models -from django.contrib.auth.models import AbstractBaseUser +from django.contrib.auth.models import AbstractUser +from django.contrib.auth.models import UserManager +from django.utils import timezone -class User(AbstractBaseUser): +class User(AbstractUser): """customized user field.""" username = models.CharField(max_length=30, unique=True) # max to 30 char USERNAME_FIELD = 'username' # setting username field to "id" - created_time = models.DateTimeField() + created_time = models.DateTimeField(default=timezone.now(), blank=True) shown_name = models.CharField(max_length=50) # max to 50 char url = models.URLField(max_length=200) # max to 200 char @@ -27,10 +29,14 @@ class User(AbstractBaseUser): choices=GROUP_CLASSES, default='general', ) + is_staff = models.BooleanField(default=False) + is_superuser = models.BooleanField(default=False) email = models.EmailField(max_length=200) # max to 200 char - REQUIRED_FIELDS = ['id', 'shown_name', 'classfication', 'email'] + objects = UserManager() # for admin user use + + REQUIRED_FIELDS = ['shown_name', 'classfication', 'email', 'is_staff', 'is_superuser'] class Following(models.Model): follower = models.ForeignKey(User, on_delete=models.CASCADE, related_name="follower") @@ -40,7 +46,7 @@ class Following(models.Model): class Post(models.Model): text = models.TextField() poster = models.ForeignKey(User, on_delete=models.CASCADE) - post_time = models.DateTimeField() + post_time = models.DateTimeField(default=timezone.now()) GROUP_PRIVILAGES = [ ('public', 'public'), # post to public timeline diff --git a/khaikang/settings.py b/khaikang/settings.py index 18377a9..e2cf6d6 100644 --- a/khaikang/settings.py +++ b/khaikang/settings.py @@ -125,4 +125,10 @@ STATIC_ROOT = '/home/kiantin1/public_html/khaikang/static/' DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' # automatically redirect to home page (/home) after login. -LOGIN_REDIRECT_URL = '/home' \ No newline at end of file +LOGIN_REDIRECT_URL = '/home' + +# custom users +AUTH_USER_MODEL = 'khaikang.User' + +# timezone +USE_TZ = True \ No newline at end of file diff --git a/khaikang/urls.py b/khaikang/urls.py index 9f14fc4..7ea2584 100644 --- a/khaikang/urls.py +++ b/khaikang/urls.py @@ -20,6 +20,8 @@ from . import views urlpatterns = [ path('admin/', admin.site.urls), - path('accounts/', include('django.contrib.auth.urls')), + path('account/', include('django.contrib.auth.urls')), path('home/', views.home, name='home'), + path('signup/', views.signup), + path('api/post', views.api_post), ] diff --git a/khaikang/views.py b/khaikang/views.py index 59ae0ec..e56c555 100644 --- a/khaikang/views.py +++ b/khaikang/views.py @@ -1,16 +1,71 @@ - +import json from django.http import HttpResponse from django.template import loader from .models import User, Post +from django.utils import timezone +from django.contrib.auth.forms import UserCreationForm +from django.shortcuts import redirect +from django.contrib.auth.models import AnonymousUser + +def api_post(request): + if request.method == 'POST': + post_body_orig = request.body.decode('utf-8') + post_body_json = json.loads(post_body_orig) + post_text = post_body_json["text"] + post_privilage = post_body_json["privilage"] + print(f"結果:{post_privilage}, {post_text}") + current_user = request.user + print(current_user.id) + + current_user_object = User.objects.get(id=int(current_user.id)) + + a_post = Post(privilage = post_privilage, text = post_text, poster = current_user_object, + post_time = timezone.now()) + a_post.save() + return HttpResponse(200, str(post_text)) + +def signup(request): + + class KhaikangUserCreationForm(UserCreationForm): + def save(self, commit=True): + user = super(KhaikangUserCreationForm, self).save(commit=False) + user.shown_name = self.cleaned_data["username"] + if commit: + user.save() + return user + + class Meta(UserCreationForm.Meta): + model = User + fields = UserCreationForm.Meta.fields + ('email',) + + form = KhaikangUserCreationForm() + + if request.method == "POST": + form = KhaikangUserCreationForm(request.POST) + if form.is_valid(): + form.fields['shown_name'] = form.fields['username'] + print(form.fields['shown_name']) + form.save() + return redirect('/account/login') #redirect to login + + if request.user != AnonymousUser(): + return redirect('/home') # redirect to main page + + form = KhaikangUserCreationForm() + + template = loader.get_template('signup.html') + return HttpResponse(template.render({'form': form}, request)) def home(request): - public_timeline_list = Post.objects.filter(privilage = 'public')[:10] + + public_timeline_list = Post.objects.filter(privilage = 'public').order_by('-id')[:10] + print(public_timeline_list) template = loader.get_template('index.html') context = { - 'latest_question_list': public_timeline_list, + 'public_timeline_list': public_timeline_list, } return HttpResponse(template.render(context, request)) \ No newline at end of file diff --git a/templates/index.html b/templates/index.html index 13dd62f..cfc3e75 100644 --- a/templates/index.html +++ b/templates/index.html @@ -1,12 +1,94 @@ {% extends "base_generic.html" %} +{% load tz %} +{% get_current_timezone as TIME_ZONE %} {% block content %}
{% csrf_token %}
- + +
- + + {% csrf_token %} + + + + + +
+ +
+ {% for public_post in public_timeline_list %} + +
{{public_post.poster.shown_name}} + at {{public_post.post_time|date:"Y-m-d H:i:s"}}+0000
+ {{public_post.text}}
+ {% endfor %} +
+ + + {% endblock %} \ No newline at end of file diff --git a/templates/signup.html b/templates/signup.html new file mode 100644 index 0000000..2d45b76 --- /dev/null +++ b/templates/signup.html @@ -0,0 +1,10 @@ +{% extends 'base_generic.html' %} + +{% block content %} +

Sign up

+
+ {% csrf_token %} + {{ form.as_p }} + +
+{% endblock %} \ No newline at end of file