fix: user validaton and community member name issue
This commit is contained in:
@@ -11,15 +11,15 @@
|
||||
"email",
|
||||
"enabled",
|
||||
"column_break_4",
|
||||
"role",
|
||||
"short_intro",
|
||||
"username",
|
||||
"email_preference",
|
||||
"section_break_7",
|
||||
"bio",
|
||||
"section_break_9",
|
||||
"username",
|
||||
"role",
|
||||
"photo",
|
||||
"column_break_12",
|
||||
"email_preference",
|
||||
"short_intro",
|
||||
"route",
|
||||
"abbr"
|
||||
],
|
||||
@@ -77,8 +77,10 @@
|
||||
"unique": 1
|
||||
},
|
||||
{
|
||||
"allow_in_quick_entry": 1,
|
||||
"fieldname": "username",
|
||||
"fieldtype": "Data",
|
||||
"in_list_view": 1,
|
||||
"label": "User Name",
|
||||
"unique": 1
|
||||
},
|
||||
@@ -111,10 +113,9 @@
|
||||
"read_only": 1
|
||||
}
|
||||
],
|
||||
"has_web_view": 1,
|
||||
"index_web_pages_for_search": 1,
|
||||
"links": [],
|
||||
"modified": "2021-04-16 10:22:46.837311",
|
||||
"modified": "2021-04-28 11:22:35.402217",
|
||||
"modified_by": "Administrator",
|
||||
"module": "Community",
|
||||
"name": "Community Member",
|
||||
|
||||
@@ -4,12 +4,12 @@
|
||||
|
||||
from __future__ import unicode_literals
|
||||
import frappe
|
||||
from frappe.website.website_generator import WebsiteGenerator
|
||||
import re
|
||||
from frappe import _
|
||||
from frappe.model.rename_doc import rename_doc
|
||||
from frappe.model.document import Document
|
||||
import random
|
||||
|
||||
class CommunityMember(WebsiteGenerator):
|
||||
class CommunityMember(Document):
|
||||
|
||||
def validate(self):
|
||||
self.validate_username()
|
||||
@@ -18,6 +18,9 @@ class CommunityMember(WebsiteGenerator):
|
||||
self.route = self.username
|
||||
|
||||
def validate_username(self):
|
||||
if not self.username:
|
||||
self.username = create_username_from_email(self.email)
|
||||
|
||||
if self.username:
|
||||
if len(self.username) < 4:
|
||||
frappe.throw(_("Username must be atleast 4 characters long."))
|
||||
@@ -26,12 +29,29 @@ class CommunityMember(WebsiteGenerator):
|
||||
self.username = self.username.lower()
|
||||
|
||||
def create_member_from_user(doc, method):
|
||||
if ( doc.username and username_exists(doc.username)) or not doc.username:
|
||||
username = create_username_from_email(doc.email)
|
||||
if len(doc.username) < 4:
|
||||
username = adjust_username(doc.username)
|
||||
if username_exists(username):
|
||||
username = username + str(random.randint(0,9))
|
||||
|
||||
member = frappe.get_doc({
|
||||
"doctype": "Community Member",
|
||||
"full_name": doc.full_name,
|
||||
"username": doc.username if len(doc.username) > 3 else ("").join([ s for s in doc.full_name.split() ]),
|
||||
"username": username,
|
||||
"email": doc.email,
|
||||
"route": doc.username,
|
||||
"owner": doc.email
|
||||
})
|
||||
member.save(ignore_permissions=True)
|
||||
|
||||
def username_exists(username):
|
||||
return frappe.db.exists("Community Member", dict(username=username))
|
||||
|
||||
def create_username_from_email(email):
|
||||
string = email.split("@")[0]
|
||||
return ''.join(e for e in string if e.isalnum())
|
||||
|
||||
def adjust_username(username):
|
||||
return username.ljust(4, str(random.randint(0,9)))
|
||||
@@ -7,4 +7,6 @@ from __future__ import unicode_literals
|
||||
import unittest
|
||||
|
||||
class TestCommunityMember(unittest.TestCase):
|
||||
pass
|
||||
|
||||
def test_member_created_from_user():
|
||||
pass
|
||||
|
||||
@@ -7,7 +7,6 @@ def create_members_from_users():
|
||||
doc = frappe.get_doc("User", {"email": user.email})
|
||||
username = doc.username if doc.username and len(doc.username) > 3 else ("").join([ s for s in doc.full_name.split() ])
|
||||
if not frappe.db.exists("Community Member", username):
|
||||
print(doc.email, username)
|
||||
member = frappe.new_doc("Community Member")
|
||||
member.full_name = doc.full_name
|
||||
member.username = username
|
||||
|
||||
@@ -58,7 +58,7 @@
|
||||
],
|
||||
"index_web_pages_for_search": 1,
|
||||
"links": [],
|
||||
"modified": "2021-04-26 16:11:20.142164",
|
||||
"modified": "2021-04-28 10:17:57.618127",
|
||||
"modified_by": "Administrator",
|
||||
"module": "LMS",
|
||||
"name": "LMS Message",
|
||||
@@ -80,5 +80,6 @@
|
||||
"quick_entry": 1,
|
||||
"sort_field": "modified",
|
||||
"sort_order": "DESC",
|
||||
"title_field": "author",
|
||||
"track_changes": 1
|
||||
}
|
||||
@@ -7,10 +7,11 @@ import frappe
|
||||
from frappe.model.document import Document
|
||||
from frappe import _
|
||||
from frappe.utils import add_days, nowdate
|
||||
from community.www.courses.utils import get_batch_members
|
||||
|
||||
class LMSMessage(Document):
|
||||
def after_insert(self):
|
||||
self.send_email()
|
||||
""" def after_insert(self):
|
||||
self.send_email() """
|
||||
|
||||
def send_email(self):
|
||||
membership = frappe.get_all("LMS Batch Membership", {"batch": self.batch}, ["member"])
|
||||
@@ -63,38 +64,41 @@ def send_daily_digest():
|
||||
)
|
||||
|
||||
def publish_message(doc, method):
|
||||
print(frappe.session.user)
|
||||
email = frappe.db.get_value("Community Member", doc.author, "email")
|
||||
session_user = True if email == frappe.session.user else False
|
||||
message = get_message_template(doc, session_user)
|
||||
template = get_message_template()
|
||||
message = frappe._dict()
|
||||
message.author_name = doc.author_name
|
||||
message.message_time = frappe.utils.pretty_date(doc.creation)
|
||||
message.message = frappe.utils.md_to_html(doc.message)
|
||||
|
||||
js = """
|
||||
$(".msger-input").val("");
|
||||
$(".message-section").append(`{0}`);
|
||||
""".format(message)
|
||||
var template = `{0}`;
|
||||
var message = {1};
|
||||
var session_user = ("{2}" == frappe.session.user) ? true : false;
|
||||
message.author_name = session_user ? "You" : message.author_name
|
||||
message.is_author = session_user;
|
||||
template = frappe.render_template(template, {{
|
||||
"message": message
|
||||
}})
|
||||
$(".message-section").append(template);
|
||||
""".format(template, message, email)
|
||||
|
||||
frappe.publish_realtime(event="eval_js", message=js, after_commit=True)
|
||||
|
||||
def get_message_template(message, session_user):
|
||||
if session_user:
|
||||
message.author_name = "You"
|
||||
message.is_author = True
|
||||
|
||||
message.message_time = frappe.utils.pretty_date(message.creation)
|
||||
template = """ <div class="discussion {% if message.is_author %} is-author {% endif %}">
|
||||
<div class="d-flex justify-content-between">
|
||||
<div class="font-weight-bold">
|
||||
{{ message.author_name }}
|
||||
</div>
|
||||
<div class="text-muted">
|
||||
{{ message.message_time }}
|
||||
</div>
|
||||
</div>
|
||||
<div class="mt-5">
|
||||
{{ message.message }}
|
||||
</div>
|
||||
</div>"""
|
||||
template = frappe.render_template(template, {
|
||||
"message": message
|
||||
})
|
||||
return template
|
||||
def get_message_template():
|
||||
return """
|
||||
<div class="discussion {% if message.is_author %} is-author {% endif %}">
|
||||
<div class="d-flex justify-content-between">
|
||||
<div class="font-weight-bold">
|
||||
{{ message.author_name }}
|
||||
</div>
|
||||
<div class="text-muted">
|
||||
{{ message.message_time }}
|
||||
</div>
|
||||
</div>
|
||||
<div class="mt-5">
|
||||
{{ message.message }}
|
||||
</div>
|
||||
</div>
|
||||
"""
|
||||
@@ -12,6 +12,6 @@ def get_context(context):
|
||||
|
||||
def get_member(username):
|
||||
try:
|
||||
frappe.get_doc("Community Member", {"username":username})
|
||||
return frappe.get_doc("Community Member", {"username":username})
|
||||
except frappe.DoesNotExistError:
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user