Skip to content

Instantly share code, notes, and snippets.

View NurElHuda's full-sized avatar

Nour Tine NurElHuda

View GitHub Profile
@NurElHuda
NurElHuda / user.py
Created March 13, 2022 06:46
User.py
from api.managers import BasicManager
from api.utils import soft_delete_related_objects
from django.contrib.admin.utils import NestedObjects
from django.contrib.auth.models import AbstractUser
from django.core.validators import RegexValidator
from django.db import DEFAULT_DB_ALIAS, models
from django.db.models.fields import DateField
from django.urls import reverse
from django.utils import timezone
from django.utils.translation import gettext_lazy as _
@NurElHuda
NurElHuda / validators.py
Created March 13, 2022 06:44
Validators.py
import re
from rest_framework import serializers
class DzPhoneFormatValidator(object):
def __call__(self, phone):
PHONE_REGEXP = re.compile("(0|\+213|00213){1}([1-9]){1}([0-9]{8})")
if not PHONE_REGEXP.match(phone):
message = "Invalid phone format"
@NurElHuda
NurElHuda / sort_requirements.py
Created August 22, 2021 21:23 — forked from michaelBenin/sort_requirements.py
Sort requirements pip files or requirements.txt for python dependencies
requirements_file = 'base.pip'
requirements = open(requirements_file, 'r')
content = requirements.read().splitlines()
content = list(set(content))
content.sort(key=lambda y: y.lower())
content = '\n'.join(content)
file = open(requirements_file, 'w')
file.write(content)
@NurElHuda
NurElHuda / cron_regex.py
Created August 6, 2021 18:52
Cron regex
CRON_REGEX = r"((((\d+,)+\d+|((\d+|\*)(\/|-)\d+)|\d+|\*) ?){5,7})"
@NurElHuda
NurElHuda / django_drf_generic_update_function.py
Created February 27, 2021 15:46
Django serializer: generic update function.
def update(self, instance, validated_data):
for key, value in validated_data.items():
setattr(instance, key, value)
instance.save()
return instance
@NurElHuda
NurElHuda / django_algerian_phone_field_utilities.py
Created February 24, 2021 01:06
Django Algerian phone field utilities.
from django.core.validators import RegexValidator
from django.db import models
from django.utils.crypto import get_random_string
def get_random_phone():
return '07'+ get_random_string(length=8, allowed_chars='123456789')
phone_pattern_validator = RegexValidator('(0|\+213|00213){1}([1-9]){1}([0-9]{8})', message='Invalid phone format')
@NurElHuda
NurElHuda / algerian_phone_number_regex.py
Created February 24, 2021 00:52
Algerian phone number RegExp
phone_regex = r'(0|\+213|00213){1}([1-9]){1}([0-9]{8})'
@NurElHuda
NurElHuda / gist:f4db576fb04ad43b21c6a1963b171476
Created February 16, 2021 22:30 — forked from kipanshi/gist:3859962
Django refresh and update model instance helpers
def refresh(instance):
"""Select and return instance from database.
Usage: ``instance = refresh(instance)``
"""
return instance.__class__.objects.get(pk=instance.pk)
def update(instance, **data):
@NurElHuda
NurElHuda / gist:998baef282bd30b63645aa0040078381
Created September 12, 2020 13:39
one_line_python_script_random_key_generation_in_bash.sh
python -c 'import random;import string; print("".join(random.SystemRandom().choice(string.digits + string.ascii_letters + string.punctuation) for _ in range(50)))'
@NurElHuda
NurElHuda / views.py
Created May 25, 2020 01:56
django_rest_url_with_query_paramaters
class BookCover(generics.RetrieveAPIView, generics.CreateAPIView):
lookup_url_kwarg = 'book_id'
def get_object(self):
try:
book = Book.objects.get(pk=self.kwargs.get('book_id'))
except Book.DoesNotExist:
raise exceptions.NotFound
return book