Skip to content

Instantly share code, notes, and snippets.

View ihfazhillah's full-sized avatar

IbnuAmin ihfazhillah

View GitHub Profile
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
@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.

@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)
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()
class Question(models.Model):
text = models.TextField()
quiz = models.ForeignKey(Quiz, related_name='+')
# 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 #}
from django.db import models
class Teacher(models.Model):
name = models.CharField()
class Student(models.Model):
name = models.CharField()
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.conf import settings
def index(request):
"""
simple render index.html
"""
return render(request, 'index.html', {'site_key': settings.RECAPTCHA_SITE_KEY})
RECAPTCHA_SITE_KEY = "your site key"
RECAPTCHA_SECRET_KEY = "your secret key"