Skip to content

Instantly share code, notes, and snippets.

View ihfazhillah's full-sized avatar

IbnuAmin ihfazhillah

View GitHub Profile
from django.conf import settings
def index(request):
"""
simple render index.html
"""
return render(request, 'index.html', {'site_key': settings.RECAPTCHA_SITE_KEY})
secret_key = settings.RECAPTCHA_SECRET_KEY
# captcha verification
data = {
'response': data.get('g-recaptcha-response'),
'secret': secret_key
}
resp = requests.post('https://www.google.com/recaptcha/api/siteverify', data=data)
result_json = resp.json()
from django.db import models
class Teacher(models.Model):
name = models.CharField()
class Student(models.Model):
name = models.CharField()
# views.py
def detail_quiz(request, pk):
quiz = get_object_or_404(Quiz, pk=pk)
return render_template(request, templatename, {'quiz': quix})
# in template
{# to show questions #}
class Question(models.Model):
text = models.TextField()
quiz = models.ForeignKey(Quiz, related_name='+')
def detail_quiz(request, pk):
quiz = get_object_or_404(Quiz, pk=pk)
context = {
'quiz': quiz
}
allowed_see_questions = any([
request.user == quiz.created_by,
request.user in quiz.students.all()
@ihfazhillah
ihfazhillah / serializers.py
Created August 19, 2019 13:48
uuid relation
class UUIDRelatedField(serializers.RelatedField):
def to_representation(self, instance):
return instance.uuid
def to_internal_value(self, data):
try:
return self.get_queryset().get(uuid=data)
except ObjectDoesNotExist:
self.fail('does_not_exist', uuid=data)
@ihfazhillah
ihfazhillah / ._README.md
Last active January 6, 2020 01:33 — forked from twidi/._README.md
Pycharm and virtualenvwrapper, with postactivate

The problem

PyCharm has support for virtual environment.

But there are two problems:

  1. the virtual environment is not activated in the terminal (it is only in the python console)
  2. it doesn't support virtualenvwrapper and so doesn't run the postactivate file.

This gist is the solution I found to resolve these two problems.

from django.contrib import messages
from django.core.mail import EmailMultiAlternatives
from django.shortcuts import redirect
from django.template.loader import render_to_string
from gradebook import celery_app
from mygrades.models import Student, StudentAssignment
@celery_app.task
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.