<commit_before>from .models import User
from tastypie.resources import Resource, ModelResource, ALL, ALL_WITH_RELATIONS
from tastypie import fields
from tastypie.authentication import ApiKeyAuthentication
from django.conf.urls import url
from api.authentication import ApiKeyPlusWebAuthentication
from api.authorization import DateaBaseAuthorization
from tastypie.cache import SimpleCache
from tastypie.throttle import BaseThrottle

#from campaign.models import Campaign
#from campaign.resources import CampaignResource
#from follow.models import Follow
#from follow.resources import FollowResource
#from vote.models import Vote
#from vote.resources import VoteResource
import json
from django.contrib.auth import authenticate
from django.contrib.auth.forms import PasswordResetForm
from tastypie.utils import trailing_slash
from utils import getOrCreateKey, getUserByKey, make_social_username
from status_codes import *
'''
from oauth2 import Client as OAuthClient, Consumer as OAuthConsumer, Token
from datea.settings import TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET
from social_auth.backends.twitter import TWITTER_CHECK_AUTH
from social_auth.models import UserSocialAuth
'''
from registration.models import RegistrationProfile
from registration import signals
from django.contrib.sites.models import RequestSite
from django.contrib.sites.models import Site

from pprint import pprint

END_POINT_NAME = 'account'

class AccountResource(ModelResource):

    class Meta:
        allowed_methods = ['post']
        #resource_name = END_POINT_NAME

    def prepend_urls(self):
        
        #twitter
        '''
        url(r"^(?P<resource_name>%s)/twitter%s$" %
        (END_POINT_NAME, trailing_slash()),
        self.wrap_view('twitter'), name="api_twitter_account"),
        ]'''

        return [
            #register datea account
            url(r"^(?P<resource_name>%s)/register%s$" %
            (END_POINT_NAME, trailing_slash()), 
            self.wrap_view('register'), name="api_register_datea_account"), 

            #activate datea account
            url(r"^(?P<resource_name>%s)/activate%s$" %
            (END_POINT_NAME, trailing_slash()), 
            self.wrap_view('activate'), name="api_activate_datea_account"), 
            
            #login to datea account
            url(r"^(?P<resource_name>%s)/login%s$" %
            (END_POINT_NAME, trailing_slash()),
            self.wrap_view('login'), name="api_login_datea_account"),

            #password reset
            url(r"^(?P<resource_name>%s)/api_reset_password%s$" %
            (END_POINT_NAME, trailing_slash()),
            self.wrap_view('reset_password'), name="api_password_reset")
            ]


    def register(self, request, **kwargs):
        #print "@ create account"
        self.method_check(request, allowed=['post'])
        
        backend = DefaultBackend()
        postData = json.loads(request.raw_post_data)

        username = postData['username']
        email = postData['email']
        password = postData['password']
        
        if User.objects.filter(email=postData['email']).count() > 0:
            return self.create_response(request,{
                    'status': SYSTEM_ERROR,
                    'error': 'duplicate email'})
        
        #print "post data"
        #print args
        #print "trying to create account"
        if Site._meta.installed:
            site = Site.objects.get_current()
        else:
            site = RequestSite(request)
        new_user = RegistrationProfile.objects.create_inactive_user(username, email,
                                                                    password, site)   
        #print "user created"
        if newUser:
            return self.create_response(request,{'status': OK,
                'message': 'Please check your email !!'})
        else:
            return self.create_response(request,{'status': SYSTEM_ERROR,
                                'error': 'Something is wrong >:/ '})
        

    def activate(self, request, **kwargs):

        self.method_check(request, allowed=['post'])
        postData = json.loads(request.raw_post_data)

        if 'activation_key' in postData:
            activation_key = postData['activation_key']
        else:
            return self.create_response(request,{'status': BAD_REQUEST,
                                'error': 'Something is wrong >:/ '})

        activated_user = RegistrationProfile.objects.activate_user(activation_key)
        if activated_user:
            signals.user_activated.send(sender=self.__class__,
                                        user=activated_user,
                                        request=request)
            return self.create_response(request,{'status': OK,
                'message': 'Your account has been activated!'})
        else:
            return self.create_response(request,{'status': SYSTEM_ERROR,
                                'error': 'Your activation key has expired.'})


    def login(self, request, **kwargs):
        pprint(kwargs)
        pprint(request.POST)
        self.method_check(request, allowed=['post'])
        
        

        postData = json.loads(request.raw_post_data)
        username = postData['username']
        password = <PASSWORD>['password']

        

        user = authenticate(username= username,
                            password= password)<commit_msg>debug login and cors 10<commit_after>from .models import User
from tastypie.resources import Resource, ModelResource, ALL, ALL_WITH_RELATIONS
from tastypie import fields
from tastypie.authentication import ApiKeyAuthentication
from django.conf.urls import url
from api.authentication import ApiKeyPlusWebAuthentication
from api.authorization import DateaBaseAuthorization
from tastypie.cache import SimpleCache
from tastypie.throttle import BaseThrottle

#from campaign.models import Campaign
#from campaign.resources import CampaignResource
#from follow.models import Follow
#from follow.resources import FollowResource
#from vote.models import Vote
#from vote.resources import VoteResource
import json
from django.contrib.auth import authenticate
from django.contrib.auth.forms import PasswordResetForm
from tastypie.utils import trailing_slash
from utils import getOrCreateKey, getUserByKey, make_social_username
from status_codes import *
'''
from oauth2 import Client as OAuthClient, Consumer as OAuthConsumer, Token
from datea.settings import TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET
from social_auth.backends.twitter import TWITTER_CHECK_AUTH
from social_auth.models import UserSocialAuth
'''
from registration.models import RegistrationProfile
from registration import signals
from django.contrib.sites.models import RequestSite
from django.contrib.sites.models import Site

from pprint import pprint

END_POINT_NAME = 'account'

class AccountResource(ModelResource):

    class Meta:
        allowed_methods = ['post']
        #resource_name = END_POINT_NAME

    def prepend_urls(self):
        
        #twitter
        '''
        url(r"^(?P<resource_name>%s)/twitter%s$" %
        (END_POINT_NAME, trailing_slash()),
        self.wrap_view('twitter'), name="api_twitter_account"),
        ]'''

        return [
            #register datea account
            url(r"^(?P<resource_name>%s)/register%s$" %
            (END_POINT_NAME, trailing_slash()), 
            self.wrap_view('register'), name="api_register_datea_account"), 

            #activate datea account
            url(r"^(?P<resource_name>%s)/activate%s$" %
            (END_POINT_NAME, trailing_slash()), 
            self.wrap_view('activate'), name="api_activate_datea_account"), 
            
            #login to datea account
            url(r"^(?P<resource_name>%s)/login%s$" %
            (END_POINT_NAME, trailing_slash()),
            self.wrap_view('login'), name="api_login_datea_account"),

            #password reset
            url(r"^(?P<resource_name>%s)/api_reset_password%s$" %
            (END_POINT_NAME, trailing_slash()),
            self.wrap_view('reset_password'), name="api_password_reset")
            ]


    def register(self, request, **kwargs):
        #print "@ create account"
        self.method_check(request, allowed=['post'])
        
        backend = DefaultBackend()
        postData = json.loads(request.raw_post_data)

        username = postData['username']
        email = postData['email']
        password = <PASSWORD>['password']
        
        if User.objects.filter(email=postData['email']).count() > 0:
            return self.create_response(request,{
                    'status': SYSTEM_ERROR,
                    'error': 'duplicate email'})
        
        #print "post data"
        #print args
        #print "trying to create account"
        if Site._meta.installed:
            site = Site.objects.get_current()
        else:
            site = RequestSite(request)
        new_user = RegistrationProfile.objects.create_inactive_user(username, email,
                                                                    password, site)   
        #print "user created"
        if newUser:
            return self.create_response(request,{'status': OK,
                'message': 'Please check your email !!'})
        else:
            return self.create_response(request,{'status': SYSTEM_ERROR,
                                'error': 'Something is wrong >:/ '})
        

    def activate(self, request, **kwargs):

        self.method_check(request, allowed=['post'])
        postData = json.loads(request.raw_post_data)

        if 'activation_key' in postData:
            activation_key = postData['activation_key']
        else:
            return self.create_response(request,{'status': BAD_REQUEST,
                                'error': 'Something is wrong >:/ '})

        activated_user = RegistrationProfile.objects.activate_user(activation_key)
        if activated_user:
            signals.user_activated.send(sender=self.__class__,
                                        user=activated_user,
                                        request=request)
            return self.create_response(request,{'status': OK,
                'message': 'Your account has been activated!'})
        else:
            return self.create_response(request,{'status': SYSTEM_ERROR,
                                'error': 'Your activation key has expired.'})


    def login(self, request, **kwargs):
        pprint(kwargs)
        pprint(request)
        self.method_check(request, allowed=['post'])
        
        

        postData = json.loads(request.raw_post_data)
        username = postData['username']
        password = postData['password']

        

        user = authenticate(username= username,
                            password= password)
{'fraction_non_alphanumeric': 0.09623740408616067, 'fraction_numerical': 0.0005546824443006379, 'mean_word_length': 2.482936252414681, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2537528', 'n_tokens_mistral': 2884, 'n_tokens_neox': 2790, 'n_words': 641}