add functionality

This commit is contained in:
Tan, Kian-ting 2022-11-13 03:02:51 +08:00
parent 7da0318e40
commit 20d7284d9c
7 changed files with 180 additions and 12 deletions

7
khaikang/admin.py Normal file
View file

@ -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)

View file

@ -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

View file

@ -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'
LOGIN_REDIRECT_URL = '/home'
# custom users
AUTH_USER_MODEL = 'khaikang.User'
# timezone
USE_TZ = True

View file

@ -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),
]

View file

@ -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))

View file

@ -1,12 +1,94 @@
{% extends "base_generic.html" %}
{% load tz %}
{% get_current_timezone as TIME_ZONE %}
{% block content %}
<form method="POST" id="posting-form">
{% csrf_token %}
<div class="posting-form-group">
<label>Post</label>
<input type="text" class="form-post" id="post_text" placeholder="What do you want to post?">
<textarea id="post_text" name="post_text" placeholder="What do you want to post?"
maxlength="500" style="resize: none;" oninput="auto_expand(this)"></textarea>
</div>
<button type="submit_post" class="btn">Post!</button>
{% csrf_token %}
<label for="privilege">Privileges:</label>
<select name="privilege" id="privil_choosing">
<option value="public" selected>Public Timeline</option>
<option value="unpublic">Not in Public Timeline</option>
<option value="private">Private</option>
</select>
<button id="submit_post" type="button" class="btn">Post!</button>
</form>
<div id="public_timeline">
{% for public_post in public_timeline_list %}
<div id="post-{{public_post.id}}" class="post"><a href="user/{{public_post.poster}}">{{public_post.poster.shown_name}}</a>
at <a href="post/{{public_post.id}}" class="post-time">{{public_post.post_time|date:"Y-m-d H:i:s"}}+0000</a><br/>
{{public_post.text}}</div>
{% endfor %}
</div>
<script>
function timezoneChanging(){
document.querySelectorAll(".post-time").forEach(
(x)=>{var date= new Date(x.innerHTML);
var year = date.getFullYear().toString();
var month = (date.getMonth()+1).toString().padStart(2, '0');
var day = date.getDate().toString().padStart(2, '0');
var hour = date.getHours().toString().padStart(2, '0');
var min = date.getMinutes().toString().padStart(2, '0');
var sec = date.getSeconds().toString().padStart(2, '0');
local_date_string = `${year}-${month}-${day} ${hour}:${min}:${sec}`;
x.innerHTML=local_date_string;}
);
}
$name = (x) => document.getElementsByName(x);
$ = (x) => document.getElementById(x);
var httpRequest;
$("submit_post").addEventListener('click', make_req);
async function make_req() {
post_text = $('post_text').value;
post_privilage = $('privil_choosing').value;
await fetch('/api/post', {
method: 'POST',
headers: {
"X-CSRFToken": "{{ csrf_token }}",
'Content-Type': 'application/json',
},
body: JSON.stringify({
"privilage" : post_privilage,
"text": post_text,
})
});
}
function auto_expand(element) {
element.style.height = 6+"em";
element.style.height = (element.scrollHeight)+"px";
}
timezoneChanging();
function adjust_post_text(){
var post_text = $("post_text");
post_text.style.height = 6+"em";
post_text.style.height = (post_text.scrollHeight)+"px";
}
adjust_post_text();
</script>
{% endblock %}

10
templates/signup.html Normal file
View file

@ -0,0 +1,10 @@
{% extends 'base_generic.html' %}
{% block content %}
<h2>Sign up</h2>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Sign up</button>
</form>
{% endblock %}