Skip to content

Instantly share code, notes, and snippets.

View hemanth-sp's full-sized avatar

hemanth sp hemanth-sp

View GitHub Profile
@hemanth-sp
hemanth-sp / models.py
Last active January 3, 2020 05:33
Comment Model design
class Comment(models.Model):
''' Main comment model'''
user = models.ForeignKey(User, related_name='comments', on_delete=models.CASCADE)
comment = models.TextField(validators=[MinLengthValidator(150)])
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def get_total_likes(self):
return self.likes.users.count()
@hemanth-sp
hemanth-sp / models.py
Created January 3, 2020 05:39
like_dislike_models
class Like(models.Model):
''' like comment '''
comment = models.OneToOneField(Comment, related_name="likes", on_delete=models.CASCADE)
users = models.ManyToManyField(User, related_name='requirement_comment_likes')
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __str__(self):
return str(self.comment.comment)[:30]
@hemanth-sp
hemanth-sp / forms.py
Created January 3, 2020 05:47
comment form
class CommentForm(forms.ModelForm):
class Meta:
model = my_models.Comment
fields = ('comment',)
widgets = {
'comment': forms.Textarea(attrs={'rows':5, 'cols':40}),
}
@hemanth-sp
hemanth-sp / views.py
Last active January 3, 2020 06:28
Comment views
from django.contrib.auth.mixins import LoginRequiredMixin
class Requirement(View):
form_class = my_forms.CommentForm
template_name = 'ktu/comment.html'
def get(self, request, *args, **kwargs):
form = self.form_class()
comment = my_models.Comment.objects.all()
@hemanth-sp
hemanth-sp / urls.py
Created January 3, 2020 06:29
comment urls
path('comments', my_views.Requirement.as_view(), name='requirements'),
path('requirement/<int:comment_id>/<str:opition>', my_views.UpdateCommentVote.as_view(), name='requirement_comment_vote'),
@hemanth-sp
hemanth-sp / comment.html
Last active January 3, 2020 06:32
like and dislike comment with tool tip html
<div class="container-1 left">
<div class="f-card content">
<h2>{{page_obj.created_at}}</h2>
<p>{{page_obj.comment}}.</p>
<div class="text-left">
<span class="pointer">
{% if request.user in page_obj.likes.users.all%}
<!-- already liked-->
<a href={% url 'requirement_comment_vote' comment_id=page_obj.pk opition='like' %}> <i
data-toggle="tooltip" data-placement="bottom" title="Unlike"
@hemanth-sp
hemanth-sp / comment.html
Last active January 4, 2020 03:05
like and dislike comment with tool tip html
<div class="container-1 left">
<div class="f-card content">
<h2>{{page_obj.created_at}}</h2>
<p>{{page_obj.comment}}.</p>
<div class="text-left">
<span class="pointer">
{% if request.user in page_obj.likes.users.all%}
<!--Start of already liked-->
<a href={% url 'comment_vote' comment_id=page_obj.pk opition='like' %}>
@hemanth-sp
hemanth-sp / .py
Created February 26, 2020 17:42
views.py
from django.contrib.auth.models import User
from rest_framework import generics
from rest_framework.permissions import IsAdminUser, AllowAny
from .serializers import *
from .models import *
from rest_framework.renderers import JSONRenderer
from rest_framework.utils import encoders, json
"""
@hemanth-sp
hemanth-sp / views.py
Created February 3, 2021 16:55
Function-based curd
from curd.serializers import StudentSerializers
from curd.models import Student
from rest_framework import status
from rest_framework.response import Response
from rest_framework.decorators import api_view
from django.shortcuts import get_object_or_404
@api_view(['GET', 'POST'])
def students_list_or_create(request):
@hemanth-sp
hemanth-sp / view.py
Created February 3, 2021 17:01
class-based API view curd
from rest_framework.views import APIView
from curd.serializers import StudentSerializers
from curd.models import Student
from rest_framework import status, permissions
from rest_framework.response import Response
from django.shortcuts import get_object_or_404
class StudentCurd(APIView):
permission_classes = [permissions.AllowAny]