2016-03-01 57 views
4

我很苦惱。我創建了一個應用程序,管理員創建用戶,並將詳細信息發送到註冊的電子郵件。但是現在我的客戶希望由admin創建的用戶保存在firebase中。我從來沒有進入firebase,也做過研究,但找不到可以解決我的問題的任何資源。使用django註冊用戶數據到firebase

代碼之前是

from __future__ import unicode_literals 
from django.contrib.auth.models import User 
from django.conf import settings 
from django.db.models.signals import post_save 
from django.core.mail import EmailMessage 
from django.utils import timezone 
from django.dispatch import receiver 
from django.db import models 


class UserProfile(models.Model): 
    user = models.ForeignKey(User,null=True) 
    first_name = models.CharField(max_length=100,blank=True,null=True,help_text=("enter the first name of user")) 
    last_name = models.CharField(max_length=100,blank=True,null=True,help_text=("enter the last name")) 
    address = models.CharField(max_length=300,blank=True,null=True,help_text=("enter the address")) 
    contact = models.CharField(max_length=100,blank=True,null=True,help_text=("enter the contact")) 
    email = models.EmailField(max_length=100,blank=True,null=True,help_text=("enter the email")) 
    username = models.CharField(max_length=100,blank=True,null=True,help_text=("enter the username")) 
    password = models.CharField(max_length=100,blank=True,null=True,help_text=("enter the strong password")) 
    creation_date = models.DateTimeField(editable=False,null=True) 
    last_modified = models.DateTimeField(editable=False,null=True) 

    def save(self, *args, **kwargs): 
    if not self.creation_date: 
     self.creation_date = timezone.now() 
    self.last_modified = timezone.now() 
    return super(UserProfile, self).save(*args, **kwargs) 


    def __str__(self): 
    return (self.username) 


@receiver(post_save,sender=UserProfile) 
def send_user_data_when_created_by_admin(sender, instance, **kwargs): 
     first_name = instance.first_name 
     print('first name is',first_name) 
     last_name = instance.last_name 
     address = instance.address 
     print('address is',address) 
     contact = instance.contact 
     email = instance.email 
     username = instance.username 
     password = instance.password 
     html_content = "your first name:%s <br> last name:%s <br> address:%s <br> contact:%s <br> email:%s <br> username:%s <br> password:%s" 
     from_email = settings.DEFAULT_FROM_EMAIL 
     message=EmailMessage('welcome',html_content %(first_name,last_name,address,contact,email,username,password),from_email,[email]) 
     message.content_subtype='html' 
     message.send() 

我如何保存用戶數據火力每當管理員創建一個從管理面板的新用戶?

+0

既然你已經有一個'post_save'信號處理程序,你可以簡單地添加所需的代碼出現。這裏解釋過於寬泛,但你可以使用[python-firebase](https://github.com/ozgur/python-firebase)。 – Selcuk

+0

你能提供一個與此相關的鏈接嗎?我從來沒有使用過Firebase,我完全空白。 – pri

回答

2

這是我將數據保存到firebase的方式。

第一:

進口

from firebase import firebase 

連接到用戶數據庫

db = firebase.FirebaseApplication("https://yourdb.firebaseio.com/users", None) 

讓您的火力祕密和驗證您的連接(祕密是一個獨特的密鑰你分貝) (這是一個假鑰匙只是爲了例子)

secret="adfkIDKDKkklff3777dlkdkLDLdd77RA" 
auth=firebase.FirebaseAuthentication(secret,"","",True) 
db.authentication = auth 

我建議你做你的send_user_data_when_created_by_admin功能,因爲它要保存的信息已創建message.send()後執行以下操作:

#let say the username is jonh45, the next line of code 
#will save the value of the variable first_name in 
#https://yourdb.firebaseio.com/users/auth_info/jonh45/first_name 
db.put("auth_info/"+username, "first_name", first_name) 

#this will be saved to /users/auth_info/jonh45/email 
db.put("auth_info/"+username, "email", email) 

等等.. 。

讓我知道如果你需要任何進一步的幫助

+0

所有代碼應該在哪裏?我是否必須將這些全部寫入models.py? – pri

+0

嘗試將它寫入您的send_user_data_when_created_by_admin,以便在它發送message.send()時它也將數據保存到您的firebase – evaldez89