# Path: app/api/cruds/utils.py
# def get_errors(e):
#     # transform django errors to redux errors
#     # django: {"key1": [value1], {"key2": [value2]}}
#     # redux: ["key1", "value1", "key2", "value2"]
#     fields = e.message_dict.keys()
#     messages = ['; '.join(m) for m in e.message_dict.values()]
#     errors = [i for pair in zip(fields, messages) for i in pair]
#     return errors
# 
# def get_object(object_name, relayId, otherwise=None):
#     try:
#         return object_name.objects.get(pk=from_global_id(relayId)[1])
#     except:
#         return otherwise
# 
# def load_object(instance, args, exception=['id']):
#     if instance:
#         [setattr(instance, key, value) for key, value in args.items() if key not in exception]
# 
#     return instance
# 
# Path: app/accounts/models.py
# class UserProfile(models.Model):
#     """
#     Models extra information about users that are not built into Django's default user model.
# 
#     Example of extra information can be google user id, facebook user id, etc.
#     """
# 
#     user = models.OneToOneField(User, on_delete=models.CASCADE, related_name="profile")
#     custom_auth_id = models.CharField(max_length=255, blank=True)
#     facebook_oauth_id = models.CharField(max_length=255, blank=True)
#     google_oauth_id = models.CharField(max_length=255, blank=True)
#     twitter_oauth_id = models.CharField(max_length=255, blank=True)
# 
#     def __str__(self):
#         return self.user.username
# 
# Path: app/api/cruds/inputs.py
# class ProfileInput(graphene.InputObjectType):
#     custom_auth_id = graphene.String(required=False)
#     facebook_oauth_id = graphene.String(required=False)
#     google_oauth_id = graphene.String(required=False)
#     twitter_oauth_id = graphene.String(required=False)
# 
# class UserCreateInput(graphene.InputObjectType):
#     username = graphene.String(required=True)
#     first_name = graphene.String(required=False)
#     last_name = graphene.String(required=False)
#     email = graphene.String(required=False)
#     is_staff = graphene.Boolean(required=False)
#     is_active = graphene.Boolean(required=False)
#     password = graphene.String(required=True)
# 
# class UserUpdateInput(graphene.InputObjectType):
#     id = graphene.String(required=True)
#     username = graphene.String(required=False)
#     first_name = graphene.String(required=False)
#     last_name = graphene.String(required=False)
#     email = graphene.String(required=False)
#     is_staff = graphene.Boolean(required=False)
#     is_active = graphene.Boolean(required=False)
#     password = graphene.String(required=False)


from django.contrib.auth.models import User
from django.core.exceptions import ValidationError
from graphene_django import DjangoObjectType
from django_filters import OrderingFilter, FilterSet
from .utils import get_errors, get_object, load_object
from app.accounts.models import UserProfile
from .inputs import ProfileInput, UserCreateInput, UserUpdateInput
import graphene




def process_user(instance, args):
    user_data = args.get('user')
    user = load_object(instance, user_data, ['id', 'password', 'profile',
                                             'is_staff', 'is_active'])
    user.is_staff = user_data.get('is_staff', False)
    user.is_active = user_data.get('is_active', False)
    if user_data.get('password'):
        user.set_password(user_data.get('password'))
    user.full_clean()
    user.save()
    profile_data = args.get('profile')
    if profile_data:

# Path: app/api/cruds/utils.py
# def get_errors(e):
#     # transform django errors to redux errors
#     # django: {"key1": [value1], {"key2": [value2]}}
#     # redux: ["key1", "value1", "key2", "value2"]
#     fields = e.message_dict.keys()
#     messages = ['; '.join(m) for m in e.message_dict.values()]
#     errors = [i for pair in zip(fields, messages) for i in pair]
#     return errors
# 
# def get_object(object_name, relayId, otherwise=None):
#     try:
#         return object_name.objects.get(pk=from_global_id(relayId)[1])
#     except:
#         return otherwise
# 
# def load_object(instance, args, exception=['id']):
#     if instance:
#         [setattr(instance, key, value) for key, value in args.items() if key not in exception]
# 
#     return instance
# 
# Path: app/accounts/models.py
# class UserProfile(models.Model):
#     """
#     Models extra information about users that are not built into Django's default user model.
# 
#     Example of extra information can be google user id, facebook user id, etc.
#     """
# 
#     user = models.OneToOneField(User, on_delete=models.CASCADE, related_name="profile")
#     custom_auth_id = models.CharField(max_length=255, blank=True)
#     facebook_oauth_id = models.CharField(max_length=255, blank=True)
#     google_oauth_id = models.CharField(max_length=255, blank=True)
#     twitter_oauth_id = models.CharField(max_length=255, blank=True)
# 
#     def __str__(self):
#         return self.user.username
# 
# Path: app/api/cruds/inputs.py
# class ProfileInput(graphene.InputObjectType):
#     custom_auth_id = graphene.String(required=False)
#     facebook_oauth_id = graphene.String(required=False)
#     google_oauth_id = graphene.String(required=False)
#     twitter_oauth_id = graphene.String(required=False)
# 
# class UserCreateInput(graphene.InputObjectType):
#     username = graphene.String(required=True)
#     first_name = graphene.String(required=False)
#     last_name = graphene.String(required=False)
#     email = graphene.String(required=False)
#     is_staff = graphene.Boolean(required=False)
#     is_active = graphene.Boolean(required=False)
#     password = graphene.String(required=True)
# 
# class UserUpdateInput(graphene.InputObjectType):
#     id = graphene.String(required=True)
#     username = graphene.String(required=False)
#     first_name = graphene.String(required=False)
#     last_name = graphene.String(required=False)
#     email = graphene.String(required=False)
#     is_staff = graphene.Boolean(required=False)
#     is_active = graphene.Boolean(required=False)
#     password = graphene.String(required=False)


# Path: app/api/cruds/user_crud.py
from django.contrib.auth.models import User
from django.core.exceptions import ValidationError
from graphene_django import DjangoObjectType
from django_filters import OrderingFilter, FilterSet
from .utils import get_errors, get_object, load_object
from app.accounts.models import UserProfile
from .inputs import ProfileInput, UserCreateInput, UserUpdateInput
import graphene





def process_user(instance, args):
    user_data = args.get('user')
    user = load_object(instance, user_data, ['id', 'password', 'profile',
                                             'is_staff', 'is_active'])
    user.is_staff = user_data.get('is_staff', False)
    user.is_active = user_data.get('is_active', False)
    if user_data.get('password'):
        user.set_password(user_data.get('password'))
    user.full_clean()
    user.save()
    profile_data = args.get('profile')
    if profile_data:

        profile = load_object(user.profile, profile_data)