Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1import logging 

2 

3from datetime import timedelta 

4 

5from datacite import DataCiteMDSClient, schema41 

6from datacite.errors import DataCiteServerError 

7 

8from django.conf import settings 

9from django.template.loader import render_to_string 

10from django.utils import timezone 

11from django.core.mail import send_mail 

12 

13logger = logging.getLogger(__name__) 

14 

15 

16class AttributeProcessor: 

17 """Create a callable `AttributeProcessor` to process SHIB_HEADER attribute values. 

18 

19 This processor is used in the Shibboleth attribute map configuration and then called by the 

20 ShibbolethRemoteUserMiddleware while parsing the attributes. 

21 

22 Usage: 

23 :: 

24 SHIBBOLETH_ATTRIBUTE_MAP = { 

25 "HTTP_REMOTE_USER": (True, "username", AttributeProcessor()), 

26 } 

27 

28 """ 

29 

30 def __call__(self, x): 

31 return x.encode("ISO8859-1").decode("utf-8") 

32 

33 

34def qs_to_str(qs, label_field): 

35 obj_list = list() 

36 for elem in qs: 

37 obj_list.append(getattr(elem, label_field)) 

38 return ", ".join(obj_list) 

39 

40 

41def generate_discuss_data_doi(dataset): 

42 

43 creators = list() 

44 for creator in dataset.get_creators(): 

45 if creator.creator_type == creator.PERSON: 

46 creators.append( 

47 { 

48 "nameType": "Personal", 

49 "creatorName": "{}, {}".format(creator.name, creator.first_name), 

50 } 

51 ) 

52 if creator.creator_type == creator.INSTITUTION: 

53 creators.append({"nameType": "Organizational", "creatorName": creator.name}) 

54 

55 logger.debug(creators) 

56 

57 data = { 

58 "identifier": { 

59 "identifier": str(settings.DC_PREFIX) + "/" + str(dataset.uuid), 

60 "identifierType": "DOI", 

61 }, 

62 "creators": creators, 

63 "titles": [{"title": str(dataset.title)}], 

64 "publisher": str(dataset.owner), 

65 "publicationYear": str(dataset.publication_date.year), 

66 "resourceType": {"resourceTypeGeneral": "Dataset"}, 

67 } 

68 

69 logger.debug(data) 

70 logger.debug(schema41.validate(data)) 

71 

72 if schema41.validate(data): 

73 doc = schema41.tostring(data) 

74 else: 

75 doc = None 

76 

77 logger.debug(str(doc)) 

78 # Initialize the MDS client. 

79 d = DataCiteMDSClient( 

80 username=str(settings.DC_USER), 

81 password=str(settings.DC_PWD), 

82 url=str(settings.DC_URL), 

83 prefix=str(settings.DC_PREFIX), 

84 ) 

85 

86 logger.debug(str(dataset.get_absolute_url())) 

87 response = d.metadata_post(doc) 

88 if response.startswith("OK"): 

89 doi = response[response.find("(") + 1 : response.find(")")] 

90 try: 

91 d.doi_post(str(doi), str(dataset.get_absolute_url())) 

92 except DataCiteServerError as e: 

93 logger.error(e) 

94 

95 return doi 

96 

97 

98def weekly_td(): 

99 # week timedelta 

100 startdate = timezone.now() 

101 enddate = startdate - timedelta(days=7) 

102 return startdate, enddate 

103 

104 

105def daily_td(): 

106 # day timedelta 

107 startdate = timezone.now() 

108 enddate = startdate - timedelta(hours=24) 

109 return startdate, enddate 

110 

111 

112def send_update_email(subject, message, email_to): 

113 email_from = "info@discuss-data.net" 

114 subject_prefixed = "[Discuss Data] {}".format(subject,) 

115 

116 message_text = render_to_string( 

117 "core/email.html", {"message": message, "subject": subject, "html": False} 

118 ) 

119 # message_html = render_to_string( 

120 # "core/email.html", {"message": message, "subject": subject, "html": True, } 

121 # ) 

122 try: 

123 send_mail( 

124 subject_prefixed, 

125 message_text, 

126 email_from, 

127 email_to, 

128 fail_silently=False, 

129 # html_message=message_html 

130 ) 

131 except Exception as e: 

132 logger.error(e)