Skip to content

Instantly share code, notes, and snippets.

View NurElHuda's full-sized avatar

Nour Tine NurElHuda

View GitHub Profile
NurElHuda /
Created March 13, 2022 06:46
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 /
Created March 13, 2022 06:44
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 /
Created August 22, 2021 21:23 — forked from michaelBenin/
Sort requirements pip files or requirements.txt for python dependencies
requirements_file = 'base.pip'
requirements = open(requirements_file, 'r')
content =
content = list(set(content))
content.sort(key=lambda y: y.lower())
content = '\n'.join(content)
file = open(requirements_file, 'w')
NurElHuda /
Created August 6, 2021 18:52
Cron regex
CRON_REGEX = r"((((\d+,)+\d+|((\d+|\*)(\/|-)\d+)|\d+|\*) ?){5,7})"
NurElHuda /
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)
return instance
NurElHuda /
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 /
Created February 24, 2021 00:52
Algerian phone number RegExp
phone_regex = r'(0|\+213|00213){1}([1-9]){1}([0-9]{8})'
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(
def update(instance, **data):
NurElHuda / gist:998baef282bd30b63645aa0040078381
Created September 12, 2020 13:39
python -c 'import random;import string; print("".join(random.SystemRandom().choice(string.digits + string.ascii_letters + string.punctuation) for _ in range(50)))'
NurElHuda /
Created May 25, 2020 01:56
class BookCover(generics.RetrieveAPIView, generics.CreateAPIView):
lookup_url_kwarg = 'book_id'
def get_object(self):
book = Book.objects.get(pk=self.kwargs.get('book_id'))
except Book.DoesNotExist:
raise exceptions.NotFound
return book